From 2eed3ca7d69ec2dac094c7c0bcabbefe34612f6c Mon Sep 17 00:00:00 2001 From: Simon Chuu Date: Sat, 30 Mar 2019 02:17:08 -0400 Subject: [PATCH] Fix broken code of plugin's Phantom mechanics --- .../{ConfigSaver.java => ConfigTool.java} | 2 +- .../mc/phantomsmp/PhantomListener.java | 33 +++++++++---------- .../simonorj/mc/phantomsmp/PhantomSMP.java | 10 +++--- 3 files changed, 22 insertions(+), 23 deletions(-) rename src/main/java/com/simonorj/mc/phantomsmp/{ConfigSaver.java => ConfigTool.java} (99%) diff --git a/src/main/java/com/simonorj/mc/phantomsmp/ConfigSaver.java b/src/main/java/com/simonorj/mc/phantomsmp/ConfigTool.java similarity index 99% rename from src/main/java/com/simonorj/mc/phantomsmp/ConfigSaver.java rename to src/main/java/com/simonorj/mc/phantomsmp/ConfigTool.java index d6857a9..f414561 100644 --- a/src/main/java/com/simonorj/mc/phantomsmp/ConfigSaver.java +++ b/src/main/java/com/simonorj/mc/phantomsmp/ConfigTool.java @@ -2,7 +2,7 @@ package com.simonorj.mc.phantomsmp; import org.bukkit.configuration.file.FileConfiguration; -class ConfigSaver { +class ConfigTool { static final String REMOVE_TARGETING_RESTED_NODE = "remove-targeting-rested"; static final String REMOVE_WHEN_SLEEPING_NODE = "remove-when-sleeping"; static final String DISALLOW_SPAWNING_FOR_NODE = "disallow-targeting-for"; diff --git a/src/main/java/com/simonorj/mc/phantomsmp/PhantomListener.java b/src/main/java/com/simonorj/mc/phantomsmp/PhantomListener.java index 3e44475..e440388 100644 --- a/src/main/java/com/simonorj/mc/phantomsmp/PhantomListener.java +++ b/src/main/java/com/simonorj/mc/phantomsmp/PhantomListener.java @@ -51,30 +51,34 @@ public class PhantomListener implements Listener { } private void targeting(Phantom phantom, Player newTarget, Cancellable e) { - plugin.getLogger().info("Targeting triggered"); + // Clean up old target + Player old = phantomPlayerMap.remove(phantom); + if (old != null) { + playerPhantomMap.get(old).remove(phantom); + } + + // get new target Player p; if (newTarget != null) p = newTarget; - else if (phantom.getTarget() instanceof Player) { + else if (phantom.getTarget() instanceof Player) p = (Player) phantom.getTarget(); - } else { - plugin.getLogger().info("Target is null"); + else return; - } + + boolean ignore = p.hasPermission(IGNORE_PERM); // If newly spawned phantom if (newPhantom.remove(phantom)) { - if (p.hasPermission(DISALLOW_SPAWN_PERM) || recentlyRested(p)) { + if (ignore || p.hasPermission(DISALLOW_SPAWN_PERM) || recentlyRested(p)) { if (e != null) e.setCancelled(true); phantom.remove(); - plugin.getLogger().info("Phantom removed"); return; } } // If targeting is not allowed - boolean ignore = p.hasPermission(IGNORE_PERM); if (ignore || recentlyRested(p)) { if (e != null) e.setCancelled(true); @@ -84,19 +88,16 @@ public class PhantomListener implements Listener { if (!ignore && plugin.removeTargetingRested && phantom.getCustomName() == null) phantom.remove(); - plugin.getLogger().info("Phantom targetting cancelled"); return; } - // Phantom spawn is legal + // Phantom target is legal playerPhantomMap.computeIfAbsent(p, k -> new LinkedHashSet<>()).add(phantom); phantomPlayerMap.put(phantom, p); - plugin.getLogger().info("Phantom is now targetting"); } private void spawned(Phantom phantom) { newPhantom.add(phantom); - plugin.getLogger().info("New phantom spawned"); } private void untarget(Player p, boolean sleeping) { @@ -106,13 +107,10 @@ public class PhantomListener implements Listener { if (phantom.getTarget() == p) { phantomPlayerMap.remove(phantom); phantom.setTarget(null); - plugin.getLogger().info("Phantom no longer targets"); } - if (sleeping && plugin.removeWhenSleeping) { + if (sleeping && plugin.removeWhenSleeping) phantom.remove(); - plugin.getLogger().info("Phantom removed"); - } i.remove(); } } @@ -140,7 +138,8 @@ public class PhantomListener implements Listener { return; } - spawned((Phantom) e.getEntity()); + if (e.getSpawnReason() == CreatureSpawnEvent.SpawnReason.NATURAL) + spawned((Phantom) e.getEntity()); } @EventHandler diff --git a/src/main/java/com/simonorj/mc/phantomsmp/PhantomSMP.java b/src/main/java/com/simonorj/mc/phantomsmp/PhantomSMP.java index 8e8d3f3..85cd855 100644 --- a/src/main/java/com/simonorj/mc/phantomsmp/PhantomSMP.java +++ b/src/main/java/com/simonorj/mc/phantomsmp/PhantomSMP.java @@ -18,12 +18,12 @@ public class PhantomSMP extends JavaPlugin { PhantomSMP.instance = this; saveDefaultConfig(); - if (getConfig().getInt(ConfigSaver.CONFIG_VERSION_NODE) != ConfigSaver.version) + if (getConfig().getInt(ConfigTool.CONFIG_VERSION_NODE) != ConfigTool.version) saveConfig(); - this.removeTargetingRested = getConfig().getBoolean(ConfigSaver.REMOVE_TARGETING_RESTED_NODE, true); - this.removeWhenSleeping = getConfig().getBoolean(ConfigSaver.REMOVE_WHEN_SLEEPING_NODE, false); - this.disallowSpawningFor= getConfig().getInt(ConfigSaver.DISALLOW_SPAWNING_FOR_NODE, 72000); + this.removeTargetingRested = getConfig().getBoolean(ConfigTool.REMOVE_TARGETING_RESTED_NODE, true); + this.removeWhenSleeping = getConfig().getBoolean(ConfigTool.REMOVE_WHEN_SLEEPING_NODE, false); + this.disallowSpawningFor= getConfig().getInt(ConfigTool.DISALLOW_SPAWNING_FOR_NODE, 72000); this.listener = new PhantomListener(); getServer().getPluginManager().registerEvents(listener, this); @@ -42,7 +42,7 @@ public class PhantomSMP extends JavaPlugin { try { //noinspection ResultOfMethodCallIgnored configFile.mkdirs(); - String data = ConfigSaver.saveToString(getConfig()); + String data = ConfigTool.saveToString(getConfig()); try (Writer writer = new OutputStreamWriter(new FileOutputStream(configFile), Charsets.UTF_8)) { writer.write(data);