From d2bbcc2d98a610d3273a5e0bf6508a21d4130538 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 2 Feb 2022 11:24:43 +0100 Subject: [PATCH] Update WarpListener Signed-off-by: yoyosource --- .../bausystem/features/warp/WarpListener.java | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java index a86f2ca8..587580f2 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java @@ -29,6 +29,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.*; +import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; import java.util.ArrayList; @@ -42,31 +43,40 @@ public class WarpListener implements Listener { private Map> warpsShown = new HashMap<>(); private Map> selected = new HashMap<>(); + @EventHandler + public void onPlayerItemHeld(PlayerItemHeldEvent e) { + ItemStack itemStack = e.getPlayer().getInventory().getItem(e.getNewSlot()); + Material material = itemStack == null ? Material.AIR : itemStack.getType(); + createStuff(e.getPlayer(), material, e.getPlayer().isSneaking()); + } + @EventHandler public void onPlayerMove(PlayerMoveEvent event) { - createStuff(event, event.getPlayer().isSneaking()); + for (Player p : Bukkit.getOnlinePlayers()) { + createStuff(p, p.getInventory().getItemInMainHand().getType(), p.isSneaking()); + } } @EventHandler public void onPlayerToggleSneak(PlayerToggleSneakEvent event) { - createStuff(event, event.isSneaking()); + createStuff(event.getPlayer(), event.getPlayer().getInventory().getItemInMainHand().getType(), event.isSneaking()); } - private void createStuff(PlayerEvent event, boolean sneaking) { - if (warpsShown.containsKey(event.getPlayer())) { - warpsShown.get(event.getPlayer()).forEach(warp -> { - warp.hide(event.getPlayer()); + private void createStuff(Player p, Material material, boolean sneaking) { + if (warpsShown.containsKey(p)) { + warpsShown.get(p).forEach(warp -> { + warp.hide(p); }); - warpsShown.remove(event.getPlayer()); + warpsShown.remove(p); } - if (event.getPlayer().getInventory().getItemInMainHand().getType() != Material.COMPASS) { + if (material != Material.COMPASS) { return; } - selected.remove(event.getPlayer()); - List warps = warpsShown.getOrDefault(event.getPlayer(), new ArrayList<>()); - Vector current = event.getPlayer().getLocation().clone().add(event.getPlayer().getLocation().getDirection().multiply(5)).toVector(); - current.setY(event.getPlayer().getLocation().getY() - 1); + selected.remove(p); + List warps = warpsShown.getOrDefault(p, new ArrayList<>()); + Vector current = p.getLocation().clone().add(p.getLocation().getDirection().multiply(5)).toVector(); + current.setY(p.getLocation().getY() - 1); Map locations = new HashMap<>(); if (sneaking) { @@ -80,25 +90,25 @@ public class WarpListener implements Listener { } locations.forEach((name, location) -> { - Vector vector = location.toVector().subtract(event.getPlayer().getLocation().toVector()); + Vector vector = location.toVector().subtract(p.getLocation().toVector()); if (vector.getX() * vector.getX() + vector.getZ() * vector.getZ() < 25) { return; } vector.setY(0); - Vector position = event.getPlayer().getLocation().toVector().clone().add(vector.normalize().multiply(5)); - position.setY(event.getPlayer().getLocation().getY() - 1); + Vector position = p.getLocation().toVector().clone().add(vector.normalize().multiply(5)); + position.setY(p.getLocation().getY() - 1); if (position.distanceSquared(current) < 0.2) { name = "§a§l" + name; - selected.computeIfAbsent(event.getPlayer(), player -> new ArrayList<>()).add(location); + selected.computeIfAbsent(p, player -> new ArrayList<>()).add(location); } - warps.add(createEntity(event.getPlayer(), position, name)); + warps.add(createEntity(p, position, name)); }); - warpsShown.put(event.getPlayer(), warps); + warpsShown.put(p, warps); warps.forEach(warp -> { - warp.display(event.getPlayer()); + warp.display(p); }); }