Fix broken code of plugin's Phantom mechanics

This commit is contained in:
Simon Chuu 2019-03-30 02:17:08 -04:00
parent 9ecd44019d
commit 2eed3ca7d6
3 changed files with 22 additions and 23 deletions

View File

@ -2,7 +2,7 @@ package com.simonorj.mc.phantomsmp;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
class ConfigSaver { class ConfigTool {
static final String REMOVE_TARGETING_RESTED_NODE = "remove-targeting-rested"; static final String REMOVE_TARGETING_RESTED_NODE = "remove-targeting-rested";
static final String REMOVE_WHEN_SLEEPING_NODE = "remove-when-sleeping"; static final String REMOVE_WHEN_SLEEPING_NODE = "remove-when-sleeping";
static final String DISALLOW_SPAWNING_FOR_NODE = "disallow-targeting-for"; static final String DISALLOW_SPAWNING_FOR_NODE = "disallow-targeting-for";

View File

@ -51,30 +51,34 @@ public class PhantomListener implements Listener {
} }
private void targeting(Phantom phantom, Player newTarget, Cancellable e) { 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; Player p;
if (newTarget != null) if (newTarget != null)
p = newTarget; p = newTarget;
else if (phantom.getTarget() instanceof Player) { else if (phantom.getTarget() instanceof Player)
p = (Player) phantom.getTarget(); p = (Player) phantom.getTarget();
} else { else
plugin.getLogger().info("Target is null");
return; return;
}
boolean ignore = p.hasPermission(IGNORE_PERM);
// If newly spawned phantom // If newly spawned phantom
if (newPhantom.remove(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) if (e != null)
e.setCancelled(true); e.setCancelled(true);
phantom.remove(); phantom.remove();
plugin.getLogger().info("Phantom removed");
return; return;
} }
} }
// If targeting is not allowed // If targeting is not allowed
boolean ignore = p.hasPermission(IGNORE_PERM);
if (ignore || recentlyRested(p)) { if (ignore || recentlyRested(p)) {
if (e != null) if (e != null)
e.setCancelled(true); e.setCancelled(true);
@ -84,19 +88,16 @@ public class PhantomListener implements Listener {
if (!ignore && plugin.removeTargetingRested && phantom.getCustomName() == null) if (!ignore && plugin.removeTargetingRested && phantom.getCustomName() == null)
phantom.remove(); phantom.remove();
plugin.getLogger().info("Phantom targetting cancelled");
return; return;
} }
// Phantom spawn is legal // Phantom target is legal
playerPhantomMap.computeIfAbsent(p, k -> new LinkedHashSet<>()).add(phantom); playerPhantomMap.computeIfAbsent(p, k -> new LinkedHashSet<>()).add(phantom);
phantomPlayerMap.put(phantom, p); phantomPlayerMap.put(phantom, p);
plugin.getLogger().info("Phantom is now targetting");
} }
private void spawned(Phantom phantom) { private void spawned(Phantom phantom) {
newPhantom.add(phantom); newPhantom.add(phantom);
plugin.getLogger().info("New phantom spawned");
} }
private void untarget(Player p, boolean sleeping) { private void untarget(Player p, boolean sleeping) {
@ -106,13 +107,10 @@ public class PhantomListener implements Listener {
if (phantom.getTarget() == p) { if (phantom.getTarget() == p) {
phantomPlayerMap.remove(phantom); phantomPlayerMap.remove(phantom);
phantom.setTarget(null); phantom.setTarget(null);
plugin.getLogger().info("Phantom no longer targets");
} }
if (sleeping && plugin.removeWhenSleeping) { if (sleeping && plugin.removeWhenSleeping)
phantom.remove(); phantom.remove();
plugin.getLogger().info("Phantom removed");
}
i.remove(); i.remove();
} }
} }
@ -140,6 +138,7 @@ public class PhantomListener implements Listener {
return; return;
} }
if (e.getSpawnReason() == CreatureSpawnEvent.SpawnReason.NATURAL)
spawned((Phantom) e.getEntity()); spawned((Phantom) e.getEntity());
} }

View File

@ -18,12 +18,12 @@ public class PhantomSMP extends JavaPlugin {
PhantomSMP.instance = this; PhantomSMP.instance = this;
saveDefaultConfig(); saveDefaultConfig();
if (getConfig().getInt(ConfigSaver.CONFIG_VERSION_NODE) != ConfigSaver.version) if (getConfig().getInt(ConfigTool.CONFIG_VERSION_NODE) != ConfigTool.version)
saveConfig(); saveConfig();
this.removeTargetingRested = getConfig().getBoolean(ConfigSaver.REMOVE_TARGETING_RESTED_NODE, true); this.removeTargetingRested = getConfig().getBoolean(ConfigTool.REMOVE_TARGETING_RESTED_NODE, true);
this.removeWhenSleeping = getConfig().getBoolean(ConfigSaver.REMOVE_WHEN_SLEEPING_NODE, false); this.removeWhenSleeping = getConfig().getBoolean(ConfigTool.REMOVE_WHEN_SLEEPING_NODE, false);
this.disallowSpawningFor= getConfig().getInt(ConfigSaver.DISALLOW_SPAWNING_FOR_NODE, 72000); this.disallowSpawningFor= getConfig().getInt(ConfigTool.DISALLOW_SPAWNING_FOR_NODE, 72000);
this.listener = new PhantomListener(); this.listener = new PhantomListener();
getServer().getPluginManager().registerEvents(listener, this); getServer().getPluginManager().registerEvents(listener, this);
@ -42,7 +42,7 @@ public class PhantomSMP extends JavaPlugin {
try { try {
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
configFile.mkdirs(); configFile.mkdirs();
String data = ConfigSaver.saveToString(getConfig()); String data = ConfigTool.saveToString(getConfig());
try (Writer writer = new OutputStreamWriter(new FileOutputStream(configFile), Charsets.UTF_8)) { try (Writer writer = new OutputStreamWriter(new FileOutputStream(configFile), Charsets.UTF_8)) {
writer.write(data); writer.write(data);