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;
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";

View File

@ -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

View File

@ -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);