ZNPCsPlus/src/main/java/lol/pyr/znpcsplus/ZNPCsPlus.java

129 lines
4.9 KiB
Java
Raw Normal View History

2023-04-17 16:15:50 +00:00
package lol.pyr.znpcsplus;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.github.znetworkw.znpcservers.commands.list.DefaultCommand;
import io.github.znetworkw.znpcservers.configuration.Configuration;
import io.github.znetworkw.znpcservers.configuration.ConfigurationConstants;
import io.github.znetworkw.znpcservers.listeners.InventoryListener;
import io.github.znetworkw.znpcservers.listeners.PlayerListener;
import io.github.znetworkw.znpcservers.npc.NPC;
import io.github.znetworkw.znpcservers.npc.NPCModel;
import io.github.znetworkw.znpcservers.npc.NPCPath;
import io.github.znetworkw.znpcservers.npc.NPCType;
import io.github.znetworkw.znpcservers.npc.task.NPCPositionTask;
2023-04-17 16:15:50 +00:00
import io.github.znetworkw.znpcservers.npc.task.NPCSaveTask;
import io.github.znetworkw.znpcservers.npc.task.NPCVisibilityTask;
2023-04-17 16:15:50 +00:00
import io.github.znetworkw.znpcservers.user.ZUser;
import io.github.znetworkw.znpcservers.utility.BungeeUtils;
import io.github.znetworkw.znpcservers.utility.SchedulerUtils;
import io.github.znetworkw.znpcservers.utility.itemstack.ItemStackSerializer;
import io.github.znetworkw.znpcservers.utility.location.ZLocation;
import org.apache.commons.io.FileUtils;
2023-04-17 17:36:19 +00:00
import org.bstats.bukkit.Metrics;
2023-04-17 16:15:50 +00:00
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
2023-04-17 16:15:50 +00:00
import java.util.Collections;
import java.util.logging.Logger;
2023-04-17 16:15:50 +00:00
public class ZNPCsPlus extends JavaPlugin {
public static Logger LOGGER;
public static File PLUGIN_FOLDER;
public static File PATH_FOLDER;
2023-04-19 16:08:30 +00:00
public static final Gson GSON = new GsonBuilder()
2023-04-17 16:15:50 +00:00
.registerTypeAdapter(ZLocation.class, ZLocation.SERIALIZER)
.registerTypeHierarchyAdapter(ItemStack.class, new ItemStackSerializer())
.setPrettyPrinting()
.disableHtmlEscaping()
.create();
2023-04-19 16:08:30 +00:00
private static final int PLUGIN_ID = 18244;
2023-04-17 16:15:50 +00:00
public static SchedulerUtils SCHEDULER;
public static BungeeUtils BUNGEE_UTILS;
private boolean enabled = false;
2023-04-17 16:15:50 +00:00
public static NPC createNPC(int id, NPCType npcType, Location location, String name) {
NPC find = NPC.find(id);
if (find != null)
return find;
NPCModel pojo = (new NPCModel(id)).withHologramLines(Collections.singletonList(name)).withLocation(new ZLocation(location)).withNpcType(npcType);
ConfigurationConstants.NPC_LIST.add(pojo);
return new NPC(pojo, true);
}
public static void deleteNPC(int npcID) {
NPC npc = NPC.find(npcID);
2023-04-19 16:08:30 +00:00
if (npc == null) throw new IllegalStateException("can't find npc: " + npcID);
2023-04-17 16:15:50 +00:00
NPC.unregister(npcID);
ConfigurationConstants.NPC_LIST.remove(npc.getNpcPojo());
}
@Override
public void onLoad() {
LOGGER = getLogger();
PLUGIN_FOLDER = getDataFolder();
PATH_FOLDER = new File(PLUGIN_FOLDER, "paths");
}
@Override
2023-04-17 16:15:50 +00:00
public void onEnable() {
if (Bukkit.getPluginManager().isPluginEnabled("ServersNPC")) {
LOGGER.severe("Detected old version of ZNPCs! Disabling the plugin...");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
checkOldFolder();
PLUGIN_FOLDER.mkdirs();
PATH_FOLDER.mkdirs();
2023-04-17 16:15:50 +00:00
loadAllPaths();
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
2023-04-17 17:36:19 +00:00
new Metrics(this, PLUGIN_ID);
2023-04-17 16:15:50 +00:00
new DefaultCommand();
SCHEDULER = new SchedulerUtils(this);
BUNGEE_UTILS = new BungeeUtils(this);
Bukkit.getOnlinePlayers().forEach(ZUser::find);
new NPCPositionTask(this);
new NPCVisibilityTask(this);
2023-04-17 16:15:50 +00:00
new NPCSaveTask(this, ConfigurationConstants.SAVE_DELAY);
new PlayerListener(this);
new InventoryListener(this);
enabled = true;
2023-04-17 16:15:50 +00:00
}
@Override
2023-04-17 16:15:50 +00:00
public void onDisable() {
if (!enabled) return;
2023-04-17 16:15:50 +00:00
Configuration.SAVE_CONFIGURATIONS.forEach(Configuration::save);
Bukkit.getOnlinePlayers().forEach(ZUser::unregister);
}
public void loadAllPaths() {
2023-04-19 16:08:30 +00:00
File[] files = PATH_FOLDER.listFiles();
if (files == null) return;
for (File file : files) {
if (!file.getName().endsWith(".path")) continue;
NPCPath.AbstractTypeWriter abstractTypeWriter = NPCPath.AbstractTypeWriter.forFile(file, NPCPath.AbstractTypeWriter.TypeWriter.MOVEMENT);
abstractTypeWriter.load();
2023-04-17 16:15:50 +00:00
}
}
private void checkOldFolder() {
if (PLUGIN_FOLDER.exists()) return;
File oldFolder = new File(PLUGIN_FOLDER.getParent(), "ServersNPC");
if (!oldFolder.exists()) return;
LOGGER.info("Detected old ZNPCs files and no new ones present, converting...");
try {
FileUtils.moveDirectory(oldFolder, PLUGIN_FOLDER);
} catch (IOException e) {
LOGGER.severe("Failed to convert old ZNPCs files:");
e.printStackTrace();
}
}
2023-04-17 16:15:50 +00:00
}