NPCLib/commons/src/main/java/net/jitse/npclib/NPCLib.java

121 lines
3.6 KiB
Java
Raw Normal View History

2018-04-26 13:52:49 +02:00
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib;
import net.jitse.npclib.api.NPC;
import net.jitse.npclib.listeners.ChunkListener;
import net.jitse.npclib.listeners.PacketListener;
import net.jitse.npclib.listeners.PlayerListener;
2019-02-21 10:19:27 +01:00
import net.jitse.npclib.logging.NPCLibLogger;
2018-04-26 13:52:49 +02:00
import net.jitse.npclib.skin.Skin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
2019-02-20 23:17:31 +01:00
import java.util.logging.Level;
2019-02-21 10:19:27 +01:00
import java.util.logging.Logger;
2018-04-26 13:52:49 +02:00
/**
* @author Jitse Boonstra
*/
public class NPCLib {
private final JavaPlugin plugin;
private final Class<?> npcClass;
2019-02-21 10:19:27 +01:00
private Logger logger;
2019-02-21 10:19:27 +01:00
public NPCLib(JavaPlugin plugin) {
2018-04-26 13:52:49 +02:00
this.plugin = plugin;
2019-02-21 10:19:27 +01:00
this.logger = new NPCLibLogger(plugin);
2018-04-26 13:52:49 +02:00
2019-02-21 10:19:27 +01:00
// TODO: Change this variable to a dynamic variable (maven file filtering?).
// logger.info("Initiating NPCLib v1.4");
2019-02-20 23:17:31 +01:00
String versionName = plugin.getServer().getClass().getPackage().getName().split("\\.")[3];
2018-04-26 13:52:49 +02:00
Class<?> npcClass = null;
try {
npcClass = Class.forName("net.jitse.npclib.nms." + versionName + ".NPC_" + versionName);
} catch (ClassNotFoundException exception) {
// Version not supported, error below.
2018-04-26 13:52:49 +02:00
}
this.npcClass = npcClass;
if (npcClass == null) {
2019-02-21 10:19:27 +01:00
logger.log(Level.SEVERE, "Failed to initiate. Your server's version ("
+ versionName + ") is not supported");
2018-04-26 13:52:49 +02:00
return;
}
2019-02-21 10:19:27 +01:00
logger.info("Enabled for MC " + versionName);
2018-04-26 13:52:49 +02:00
registerInternal();
}
private void registerInternal() {
PluginManager pluginManager = plugin.getServer().getPluginManager();
2018-04-26 13:52:49 +02:00
pluginManager.registerEvents(new PlayerListener(), plugin);
pluginManager.registerEvents(new ChunkListener(), plugin);
// Boot the according packet listener.
new PacketListener().start(plugin);
2018-04-26 13:52:49 +02:00
}
/**
* Create a new non-player character (NPC).
*
* @param skin The skin you want the NPC to have.
* @param autoHideDistance Distance from where you want to NPC to hide from the player (50 recommended).
* @param lines The text you want to sendShowPackets above the NPC (null = no text).
* @return The NPC object you may use to sendShowPackets it to players.
*/
public NPC createNPC(Skin skin, double autoHideDistance, List<String> lines) {
try {
return (NPC) npcClass.getConstructors()[0].newInstance(plugin, skin, autoHideDistance, lines);
} catch (Exception exception) {
2019-02-21 10:19:27 +01:00
logger.log(Level.SEVERE, "Failed to create NPC. Please report the following stacktrace", exception);
2018-04-26 13:52:49 +02:00
}
return null;
}
/**
* Create a new non-player character (NPC).
*
* @param skin The skin you want the NPC to have.
* @param lines The text you want to sendShowPackets above the NPC (null = no text).
* @return The NPC object you may use to sendShowPackets it to players.
*/
public NPC createNPC(Skin skin, List<String> lines) {
return createNPC(skin, 50, lines);
}
/**
* Create a new non-player character (NPC).
*
* @param skin The skin you want the NPC to have.
* @return The NPC object you may use to sendShowPackets it to players.
*/
public NPC createNPC(Skin skin) {
return createNPC(skin, 50, null);
}
/**
* Create a new non-player character (NPC).
*
* @return The NPC object you may use to sendShowPackets it to players.
*/
public NPC createNPC() {
return createNPC(null, 50, null);
}
}