diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/smartplace/SmartPlaceListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/smartplace/SmartPlaceListener.java index 40a2bdab..00931ca9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/smartplace/SmartPlaceListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/smartplace/SmartPlaceListener.java @@ -85,29 +85,39 @@ public class SmartPlaceListener implements Plain, Listener { private static final Reflection.MethodInvoker packetExecutor = Reflection.getMethod(playerConnectionClazz, null, useItem); private static final Set SMART_PLACING = new HashSet<>(); + private static final Set WAS_EXECUTED = new HashSet<>(); public SmartPlaceListener() { - TinyProtocol.instance.addFilter(useItem, (player, object) -> { - if (!Config.getInstance().get(player).getPlainValueOrDefault("smartPlace", false)) return object; - - Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { - Block block = player.getTargetBlockExact(6); - boolean shouldSneak = !(block != null && (block.getType().isInteractable() || block.getType() == Material.NOTE_BLOCK) && !CONTAINERS.contains(block.getType()) && !IGNORED.contains(block.getType())); - - boolean sneaking = player.isSneaking(); - if (sneaking) SMART_PLACING.add(player); - player.setSneaking(shouldSneak || sneaking); - packetExecutor.invoke(playerConnection.get(getHandle.invoke(player)), object); - SMART_PLACING.remove(player); - player.setSneaking(sneaking); - }, 0); + TinyProtocol.instance.addFilter(useItem, (player, packet) -> { + if (!Config.getInstance().get(player).getPlainValueOrDefault("smartPlace", false)) return packet; + Block block = player.getTargetBlockExact(6); + boolean shouldSneak = !(block != null && (block.getType().isInteractable() || block.getType() == Material.NOTE_BLOCK) && !CONTAINERS.contains(block.getType()) && !IGNORED.contains(block.getType())); + boolean sneaking = player.isSneaking(); + run(player, packet, true, sneaking, shouldSneak); return null; }); } + private void run(Player player, Object packet, boolean first, boolean sneaking, boolean shouldSneak) { + Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { + + if (sneaking) SMART_PLACING.add(player); + player.setSneaking(shouldSneak || sneaking); + packetExecutor.invoke(playerConnection.get(getHandle.invoke(player)), packet); + SMART_PLACING.remove(player); + player.setSneaking(sneaking); + + if (!WAS_EXECUTED.contains(player) && first) { + run(player, packet, false, sneaking, shouldSneak); + } + WAS_EXECUTED.remove(player); + }, first ? 0 : 1); + } + @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) return; + WAS_EXECUTED.add(event.getPlayer()); if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; if (event.getPlayer().getGameMode() == GameMode.SPECTATOR) return; if (!event.getPlayer().isSneaking()) return;