From c6beebed05c17484e43d16026ca4437735464038 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 14:38:39 +0100 Subject: [PATCH] Fix NPE --- src/de/steamwar/lobby/display/NPC.java | 4 ++- src/de/steamwar/lobby/team/TeamPlayer.java | 41 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 58b49ee..2040d5f 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -221,7 +221,9 @@ public class NPC { private void move(Player player) { TinyProtocol.instance.sendPacket(player, headRotation); - TinyProtocol.instance.sendPacket(player, move); + if (move != null) { + TinyProtocol.instance.sendPacket(player, move); + } } public void delete() { diff --git a/src/de/steamwar/lobby/team/TeamPlayer.java b/src/de/steamwar/lobby/team/TeamPlayer.java index b45ed6e..41fefb1 100644 --- a/src/de/steamwar/lobby/team/TeamPlayer.java +++ b/src/de/steamwar/lobby/team/TeamPlayer.java @@ -23,6 +23,7 @@ import de.steamwar.lobby.LobbySystem; import de.steamwar.lobby.display.NPC; import de.steamwar.lobby.listener.BasicListener; import de.steamwar.sql.SteamwarUser; +import lombok.AllArgsConstructor; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -35,12 +36,37 @@ import org.bukkit.event.entity.EntityInteractEvent; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.logging.Level; public class TeamPlayer extends BasicListener { + @AllArgsConstructor + private static class Cuboid { + private double x1; + private double y1; + private double z1; + private double x2; + private double y2; + private double z2; + + public boolean contains(Location location) { + return location.getX() >= x1 && location.getX() <= x2 && location.getY() >= y1 && location.getY() <= y2 && location.getZ() >= z1 && location.getZ() <= z2; + } + } + + private static final List cuboids = new ArrayList<>(); + static { + cuboids.add(new Cuboid(1509, 52, 1464, 1510, 58, 1469)); + cuboids.add(new Cuboid(1538, 52, 1464, 1539, 58, 1469)); + cuboids.add(new Cuboid(1518, 55, 1433, 1530, 60, 1434)); + cuboids.add(new Cuboid(1587, 52, 1471, 1588, 56, 1475)); + cuboids.add(new Cuboid(1479, 52, 1461, 1478, 56, 1463)); + } + private static final World world = Bukkit.getWorlds().get(0); private static final Map entities = new HashMap<>(); @@ -53,7 +79,7 @@ public class TeamPlayer extends BasicListener { villager.setInvulnerable(true); villager.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1, false, false, false)); villager.setCustomName(name); - villager.setProfession(Villager.Profession.NITWIT); + villager.setProfession(Villager.Profession.NONE); entities.put(name, npc); } @@ -85,12 +111,25 @@ public class TeamPlayer extends BasicListener { world.getEntitiesByClasses(Villager.class).forEach(entity -> { NPC npc = entities.get(entity.getName()); if (npc != null) { + if (illegalLocation(entity.getLocation())) { + entity.teleport(npc.getLocation()); + return; + } npc.setLocation(entity.getLocation()); } }); }, 1L, 1L); } + private boolean illegalLocation(Location location) { + for (Cuboid cuboid : cuboids) { + if (cuboid.contains(location)) { + return true; + } + } + return false; + } + @EventHandler public void onEntityInteract(EntityInteractEvent event) { if (event.getEntityType() == EntityType.VILLAGER) {