mirror of
https://github.com/chuushi/PhantomSMP.git
synced 2024-11-22 01:56:09 +01:00
Fix broken code of plugin's Phantom mechanics
This commit is contained in:
parent
9ecd44019d
commit
2eed3ca7d6
@ -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";
|
@ -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
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user