SteamWar/BauSystem2.0
Archiviert
12
0

Update WarpListener
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2022-02-02 11:24:43 +01:00
Ursprung 3a5e0efa26
Commit d2bbcc2d98

Datei anzeigen

@ -29,6 +29,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.player.*; import org.bukkit.event.player.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.util.ArrayList; import java.util.ArrayList;
@ -42,31 +43,40 @@ public class WarpListener implements Listener {
private Map<Player, List<AbstractWarpEntity>> warpsShown = new HashMap<>(); private Map<Player, List<AbstractWarpEntity>> warpsShown = new HashMap<>();
private Map<Player, List<Location>> selected = new HashMap<>(); private Map<Player, List<Location>> 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 @EventHandler
public void onPlayerMove(PlayerMoveEvent event) { public void onPlayerMove(PlayerMoveEvent event) {
createStuff(event, event.getPlayer().isSneaking()); for (Player p : Bukkit.getOnlinePlayers()) {
createStuff(p, p.getInventory().getItemInMainHand().getType(), p.isSneaking());
}
} }
@EventHandler @EventHandler
public void onPlayerToggleSneak(PlayerToggleSneakEvent event) { 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) { private void createStuff(Player p, Material material, boolean sneaking) {
if (warpsShown.containsKey(event.getPlayer())) { if (warpsShown.containsKey(p)) {
warpsShown.get(event.getPlayer()).forEach(warp -> { warpsShown.get(p).forEach(warp -> {
warp.hide(event.getPlayer()); warp.hide(p);
}); });
warpsShown.remove(event.getPlayer()); warpsShown.remove(p);
} }
if (event.getPlayer().getInventory().getItemInMainHand().getType() != Material.COMPASS) { if (material != Material.COMPASS) {
return; return;
} }
selected.remove(event.getPlayer()); selected.remove(p);
List<AbstractWarpEntity> warps = warpsShown.getOrDefault(event.getPlayer(), new ArrayList<>()); List<AbstractWarpEntity> warps = warpsShown.getOrDefault(p, new ArrayList<>());
Vector current = event.getPlayer().getLocation().clone().add(event.getPlayer().getLocation().getDirection().multiply(5)).toVector(); Vector current = p.getLocation().clone().add(p.getLocation().getDirection().multiply(5)).toVector();
current.setY(event.getPlayer().getLocation().getY() - 1); current.setY(p.getLocation().getY() - 1);
Map<String, Location> locations = new HashMap<>(); Map<String, Location> locations = new HashMap<>();
if (sneaking) { if (sneaking) {
@ -80,25 +90,25 @@ public class WarpListener implements Listener {
} }
locations.forEach((name, location) -> { 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) { if (vector.getX() * vector.getX() + vector.getZ() * vector.getZ() < 25) {
return; return;
} }
vector.setY(0); vector.setY(0);
Vector position = event.getPlayer().getLocation().toVector().clone().add(vector.normalize().multiply(5)); Vector position = p.getLocation().toVector().clone().add(vector.normalize().multiply(5));
position.setY(event.getPlayer().getLocation().getY() - 1); position.setY(p.getLocation().getY() - 1);
if (position.distanceSquared(current) < 0.2) { if (position.distanceSquared(current) < 0.2) {
name = "§a§l" + name; 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 -> { warps.forEach(warp -> {
warp.display(event.getPlayer()); warp.display(p);
}); });
} }