From 5b0c2a4131eff102b7e3b80075445c70e9338372 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Mon, 9 Nov 2020 11:38:02 -0500 Subject: [PATCH] Use ray-tracing based target block for 1.13.2+ (#3763) Uses new method of ray-tracing to target block instead of using BlockIterator which doesn't respect the player looking at a different y-level. Falls back to old BlockIterator method on server versions older than 1.13.2 Fixes #3756. --- Essentials/src/com/earth2me/essentials/IUser.java | 3 +++ Essentials/src/com/earth2me/essentials/User.java | 10 ++++++++++ .../com/earth2me/essentials/commands/Commandbreak.java | 2 +- .../earth2me/essentials/commands/Commandeditsign.java | 4 ++-- .../earth2me/essentials/commands/Commandlightning.java | 2 +- .../earth2me/essentials/signs/SignPlayerListener.java | 2 +- 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index 8d1a48d9c..45c629440 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -7,6 +7,7 @@ import net.ess3.api.MaxMoneyException; import net.ess3.api.events.AfkStatusChangeEvent; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.entity.Player; import java.math.BigDecimal; @@ -207,4 +208,6 @@ public interface IUser { void setLastMessageReplyRecipient(boolean enabled); Map getConfirmingPayments(); + + Block getTargetBlock(int maxDistance); } diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index 8e601099c..d8de3fbbe 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -17,6 +17,7 @@ import net.ess3.api.events.MuteStatusChangeEvent; import net.ess3.api.events.UserBalanceUpdateEvent; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.inventory.ItemStack; @@ -1009,4 +1010,13 @@ public class User extends UserData implements Comparable, IMessageRecipien public void setLastHomeConfirmationTimestamp() { this.lastHomeConfirmationTimestamp = System.currentTimeMillis(); } + + @Override + public Block getTargetBlock(int maxDistance) { + final Block block; + if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_13_2_R01) || (block = base.getTargetBlockExact(maxDistance)) == null) { + return base.getTargetBlock(null, maxDistance); + } + return block; + } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandbreak.java b/Essentials/src/com/earth2me/essentials/commands/Commandbreak.java index 755711f85..cf7342eb5 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandbreak.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandbreak.java @@ -17,7 +17,7 @@ public class Commandbreak extends EssentialsCommand { @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - final Block block = user.getBase().getTargetBlock(null, 20); + final Block block = user.getTargetBlock(20); if (block.getType() == Material.AIR) { throw new NoChargeException(); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandeditsign.java b/Essentials/src/com/earth2me/essentials/commands/Commandeditsign.java index f923406a3..b540dadd5 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandeditsign.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandeditsign.java @@ -25,7 +25,7 @@ public class Commandeditsign extends EssentialsCommand { throw new NotEnoughArgumentsException(); } - final Block target = user.getBase().getTargetBlock(null, 5); //5 is a good number + final Block target = user.getTargetBlock(5); //5 is a good number if (!(target.getState() instanceof Sign)) { throw new Exception(tl("editsignCommandTarget")); } @@ -69,7 +69,7 @@ public class Commandeditsign extends EssentialsCommand { return Lists.newArrayList("1", "2", "3", "4"); } else if (args.length == 3 && args[0].equalsIgnoreCase("set") && NumberUtil.isPositiveInt(args[1])) { final int line = Integer.parseInt(args[1]); - final Block target = user.getBase().getTargetBlock(null, 5); + final Block target = user.getTargetBlock(5); if (target.getState() instanceof Sign && line <= 4) { final Sign sign = (Sign) target.getState(); return Lists.newArrayList(FormatUtil.unformatString(user, "essentials.editsign", sign.getLine(line - 1))); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandlightning.java b/Essentials/src/com/earth2me/essentials/commands/Commandlightning.java index aca9555ee..c81da17a3 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandlightning.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandlightning.java @@ -20,7 +20,7 @@ public class Commandlightning extends EssentialsLoopCommand { public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception { if (args.length == 0 || !sender.isAuthorized("essentials.lightning.others", ess)) { if (sender.isPlayer()) { - sender.getPlayer().getWorld().strikeLightning(sender.getPlayer().getTargetBlock(null, 600).getLocation()); + sender.getPlayer().getWorld().strikeLightning(sender.getUser(ess).getTargetBlock(600).getLocation()); return; } throw new NotEnoughArgumentsException(); diff --git a/Essentials/src/com/earth2me/essentials/signs/SignPlayerListener.java b/Essentials/src/com/earth2me/essentials/signs/SignPlayerListener.java index 05dafd0c7..ff2638c7d 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignPlayerListener.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignPlayerListener.java @@ -37,7 +37,7 @@ public class SignPlayerListener implements Listener { if (event.isCancelled() && event.getAction() == Action.RIGHT_CLICK_AIR) { Block targetBlock = null; try { - targetBlock = event.getPlayer().getTargetBlock(null, 5); + targetBlock = ess.getUser(event.getPlayer()).getTargetBlock(5); } catch (final IllegalStateException ex) { if (ess.getSettings().isDebug()) { ess.getLogger().log(Level.WARNING, ex.getMessage(), ex);