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;
|
2019-02-13 11:42:12 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2018-04-26 13:52:49 +02:00
|
|
|
import org.bukkit.Server;
|
|
|
|
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 Server server;
|
|
|
|
private final JavaPlugin plugin;
|
|
|
|
private final Class<?> npcClass;
|
|
|
|
|
2019-02-21 10:19:27 +01:00
|
|
|
private Logger logger;
|
2018-05-05 19:34:00 +02:00
|
|
|
|
2019-02-21 10:19:27 +01:00
|
|
|
public NPCLib(JavaPlugin plugin) {
|
2018-04-26 13:52:49 +02:00
|
|
|
this.plugin = plugin;
|
|
|
|
this.server = plugin.getServer();
|
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
|
|
|
|
2018-04-26 13:52:49 +02:00
|
|
|
String versionName = server.getClass().getPackage().getName().split("\\.")[3];
|
|
|
|
|
|
|
|
Class<?> npcClass = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
npcClass = Class.forName("net.jitse.npclib.nms." + versionName + ".NPC_" + versionName);
|
2018-05-05 19:34:00 +02:00
|
|
|
} 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 = server.getPluginManager();
|
|
|
|
|
|
|
|
pluginManager.registerEvents(new PlayerListener(), plugin);
|
|
|
|
pluginManager.registerEvents(new ChunkListener(), plugin);
|
|
|
|
|
2019-02-13 11:42:12 +01:00
|
|
|
// Boot the according packet listener.
|
2019-02-15 19:59:38 +01:00
|
|
|
new PacketListener().start(plugin, Bukkit.getBukkitVersion().contains("1.7"));
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|