Small cleanup (#2) (PR)
* Update NPCManager.java Fixed to a more singleton perspective * Update NPCLib.java Fixed import issue
This commit is contained in:
committed by
Jitse Boonstra
parent
81a0f2a855
commit
d8a39d4c06
@@ -10,9 +10,10 @@ import net.jitse.npclib.listeners.PlayerLeaveListener;
|
|||||||
import net.jitse.npclib.listeners.PlayerMoveListener;
|
import net.jitse.npclib.listeners.PlayerMoveListener;
|
||||||
import net.jitse.npclib.skin.Skin;
|
import net.jitse.npclib.skin.Skin;
|
||||||
import net.jitse.npclib.version.Version;
|
import net.jitse.npclib.version.Version;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.bukkit.plugin.PluginManager;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -22,17 +23,19 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class NPCLib {
|
public class NPCLib {
|
||||||
|
|
||||||
|
private final Server server;
|
||||||
private final JavaPlugin plugin;
|
private final JavaPlugin plugin;
|
||||||
private final Version version;
|
private final Version version;
|
||||||
|
|
||||||
public NPCLib(JavaPlugin plugin) {
|
public NPCLib(JavaPlugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
this.server = plugin.getServer();
|
||||||
|
|
||||||
String versionName = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
String versionName = this.server.getClass().getPackage().getName().split("\\.")[3];
|
||||||
version = Version.getByName(versionName).orElse(null);
|
version = Version.getByName(versionName).orElse(null);
|
||||||
|
|
||||||
if (version == null) {
|
if (version == null) {
|
||||||
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "NPCLib failed to initiate. Your server's version ("
|
this.server.getConsoleSender().sendMessage(ChatColor.RED + "NPCLib failed to initiate. Your server's version ("
|
||||||
+ versionName + ") is not supported.");
|
+ versionName + ") is not supported.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,8 +43,10 @@ public class NPCLib {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void registerInternal() {
|
private void registerInternal() {
|
||||||
plugin.getServer().getPluginManager().registerEvents(new PlayerMoveListener(), plugin);
|
PluginManager pluginManager = this.server.getPluginManager();
|
||||||
plugin.getServer().getPluginManager().registerEvents(new PlayerLeaveListener(), plugin);
|
|
||||||
|
pluginManager.registerEvents(new PlayerMoveListener(), plugin);
|
||||||
|
pluginManager.registerEvents(new PlayerLeaveListener(), plugin);
|
||||||
|
|
||||||
new PacketListener().start(plugin);
|
new PacketListener().start(plugin);
|
||||||
}
|
}
|
||||||
@@ -58,7 +63,7 @@ public class NPCLib {
|
|||||||
try {
|
try {
|
||||||
return version.createNPC(plugin, skin, autoHideDistance, lines);
|
return version.createNPC(plugin, skin, autoHideDistance, lines);
|
||||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException exception) {
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException exception) {
|
||||||
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "NPCLib failed to create NPC. Please report this stacktrace:");
|
this.server.getConsoleSender().sendMessage(ChatColor.RED + "NPCLib failed to create NPC. Please report this stacktrace:");
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import java.util.Set;
|
|||||||
/**
|
/**
|
||||||
* @author Jitse Boonstra
|
* @author Jitse Boonstra
|
||||||
*/
|
*/
|
||||||
public class NPCManager {
|
public final class NPCManager {
|
||||||
|
|
||||||
private static Set<NPC> npcs = new HashSet<>();
|
private static Set<NPC> npcs = new HashSet<>();
|
||||||
|
|
||||||
@@ -27,4 +27,9 @@ public class NPCManager {
|
|||||||
public static void remove(NPC npc) {
|
public static void remove(NPC npc) {
|
||||||
npcs.remove(npc);
|
npcs.remove(npc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private NPCManager() {
|
||||||
|
throw new SecurityException("You cannot initialize this class.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user