diff --git a/.idea/PhantomSMP.iml b/.idea/PhantomSMP.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/.idea/PhantomSMP.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..baaea04 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8ad78db..c00378a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.simonorj.mc.phantomsmp PhantomSMP - 1.1.0 + 1.1.1 https://github.com/SimonOrJ/PhantomSMP https://github.com/SimonOrJ/PhantomSMP/issues @@ -28,13 +28,13 @@ org.bukkit bukkit - 1.13.1-R0.1-SNAPSHOT + 1.13.2-R0.1-SNAPSHOT provided org.bstats bstats-bukkit - 1.4 + 1.5 compile diff --git a/readme.md b/readme.md index 7c60038..0c9e71f 100644 --- a/readme.md +++ b/readme.md @@ -24,7 +24,7 @@ the next sunrise. This plugin addresses this issue. Features: * Phantoms will ignore players who used the bed within last three Minecraft - days. In addition, this duration is configurable. + days. (The duration is configurable) * Phantoms will ignore or despawn on players who uses the bed or is killed. This is configurable as well. * Control if the phantoms should despawn when they try to target a rested diff --git a/src/main/java/com/simonorj/mc/phantomsmp/PhantomListener.java b/src/main/java/com/simonorj/mc/phantomsmp/PhantomListener.java index e440388..cab92d3 100644 --- a/src/main/java/com/simonorj/mc/phantomsmp/PhantomListener.java +++ b/src/main/java/com/simonorj/mc/phantomsmp/PhantomListener.java @@ -17,6 +17,7 @@ import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.world.ChunkLoadEvent; +import org.bukkit.event.world.ChunkUnloadEvent; import java.util.*; @@ -24,7 +25,7 @@ public class PhantomListener implements Listener { private static final String DISALLOW_SPAWN_PERM = "phantomsmp.disallowspawn"; private static final String IGNORE_PERM = "phantomsmp.ignore"; - private final Map> playerPhantomMap = new HashMap<>(); + private final Map> playerPhantomMap = new HashMap<>(); private final Map phantomPlayerMap = new HashMap<>(); private final Set newPhantom = new LinkedHashSet<>(); private final PhantomSMP plugin; @@ -66,29 +67,15 @@ else if (phantom.getTarget() instanceof Player) else return; - boolean ignore = p.hasPermission(IGNORE_PERM); - - // If newly spawned phantom - if (newPhantom.remove(phantom)) { - if (ignore || p.hasPermission(DISALLOW_SPAWN_PERM) || recentlyRested(p)) { - if (e != null) - e.setCancelled(true); - phantom.remove(); - return; - } - } - - // If targeting is not allowed - if (ignore || recentlyRested(p)) { + boolean newlySpawned = newPhantom.remove(phantom); + if (ignorePlayer(p, newlySpawned)) { if (e != null) e.setCancelled(true); - else - phantom.setTarget(null); - if (!ignore && plugin.removeTargetingRested && phantom.getCustomName() == null) + if (newlySpawned || !p.hasPermission(IGNORE_PERM) && plugin.removeTargetingRested && phantom.getCustomName() == null) phantom.remove(); - - return; + else + phantom.setTarget(null); } // Phantom target is legal @@ -96,6 +83,10 @@ else if (phantom.getTarget() instanceof Player) phantomPlayerMap.put(phantom, p); } + private boolean ignorePlayer(Player p, boolean newlySpawnedPhantom) { + return p.hasPermission(IGNORE_PERM) || newlySpawnedPhantom && p.hasPermission(DISALLOW_SPAWN_PERM) || recentlyRested(p); + } + private void spawned(Phantom phantom) { newPhantom.add(phantom); } @@ -115,6 +106,13 @@ private void untarget(Player p, boolean sleeping) { } } + private void removePhantom(Phantom phantom) { + Player p = phantomPlayerMap.remove(phantom); + if (p == null) + return; + playerPhantomMap.get(p).remove(phantom); + } + @EventHandler(priority = EventPriority.MONITOR) public void onPlayerJoin(PlayerJoinEvent e) { playerPhantomMap.put(e.getPlayer(), new LinkedHashSet<>()); @@ -124,7 +122,14 @@ public void onPlayerJoin(PlayerJoinEvent e) { public void onPlayerLeave(PlayerQuitEvent e) { Player p = e.getPlayer(); - for (Phantom phantom : playerPhantomMap.get(p)) { + Set phantoms = playerPhantomMap.get(p); + + if (phantoms == null) { + plugin.getLogger().warning("Phantom list for '" + p.getName() + "' was not initiated! Was there an error during login?"); + return; + } + + for (Phantom phantom : phantoms) { if (phantom.getTarget() == p) { phantom.setTarget(null); phantomPlayerMap.remove(phantom); @@ -165,10 +170,9 @@ public void onPhantomTargeting(EntityTargetLivingEntityEvent e) { targeting((Phantom) e.getEntity(), (Player) e.getTarget(), e); } - // Check phantom in loaded chunks @EventHandler public void onPhantomInLoadedChunk(ChunkLoadEvent e) { - if (e.getWorld().getEnvironment() != World.Environment.NORMAL) + if (e.isNewChunk() || e.getWorld().getEnvironment() != World.Environment.NORMAL) return; for (Entity ent : e.getChunk().getEntities()) @@ -177,16 +181,20 @@ public void onPhantomInLoadedChunk(ChunkLoadEvent e) { } @EventHandler - public void onPhantomDeath(EntityDeathEvent e) { - if (!(e.getEntity() instanceof Phantom)) + public void onPhantomInUnloadedChunk(ChunkUnloadEvent e) { + if (e.getWorld().getEnvironment() != World.Environment.NORMAL) return; - Phantom phantom = (Phantom) e.getEntity(); + for (Entity ent : e.getChunk().getEntities()) + if (ent instanceof Phantom) + removePhantom((Phantom) ent); + } - Player p = phantomPlayerMap.remove(phantom); - if (p == null) + @EventHandler + public void onPhantomDeath(EntityDeathEvent e) { + if (!(e.getEntity() instanceof Phantom)) return; - playerPhantomMap.get(p).remove(phantom); + removePhantom((Phantom) e.getEntity()); } } diff --git a/src/main/java/com/simonorj/mc/phantomsmp/PhantomSMP.java b/src/main/java/com/simonorj/mc/phantomsmp/PhantomSMP.java index e09a4f3..2287f3b 100644 --- a/src/main/java/com/simonorj/mc/phantomsmp/PhantomSMP.java +++ b/src/main/java/com/simonorj/mc/phantomsmp/PhantomSMP.java @@ -35,8 +35,8 @@ public void onEnable() { private void setupMetrics() { Metrics metrics = new Metrics(this); - metrics.addCustomChart(new Metrics.SimplePie("remove-targeting-rested", () -> String.valueOf(removeTargetingRested))); - metrics.addCustomChart(new Metrics.SimplePie("remove-targeting-rested", () -> String.valueOf(removeWhenSleeping))); + metrics.addCustomChart(new Metrics.SimplePie("removeTargetingRested", () -> String.valueOf(removeTargetingRested))); + metrics.addCustomChart(new Metrics.SimplePie("removeWhenSleeping", () -> String.valueOf(removeWhenSleeping))); } @Override