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

171 lines
7.2 KiB
Java
Raw Normal View History

2023-04-17 16:15:50 +00:00
package lol.pyr.znpcsplus;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
2023-04-24 21:31:48 +00:00
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
2023-04-17 16:15:50 +00:00
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder;
2023-04-17 16:15:50 +00:00
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.NPCPath;
2023-04-24 21:31:48 +00:00
import io.github.znetworkw.znpcservers.npc.NPCSkin;
import io.github.znetworkw.znpcservers.npc.interaction.InteractionPacketListener;
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.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;
2023-04-24 16:12:50 +00:00
import lol.pyr.znpcsplus.entity.PacketLocation;
import lol.pyr.znpcsplus.npc.NPC;
2023-04-24 21:31:48 +00:00
import lol.pyr.znpcsplus.npc.NPCProperty;
2023-04-24 16:12:50 +00:00
import lol.pyr.znpcsplus.npc.NPCRegistry;
import lol.pyr.znpcsplus.npc.NPCType;
import lol.pyr.znpcsplus.tasks.NPCVisibilityTask;
import lol.pyr.znpcsplus.updater.UpdateChecker;
import lol.pyr.znpcsplus.updater.UpdateNotificationListener;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
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.ChatColor;
2023-04-24 16:12:50 +00:00
import org.bukkit.World;
2023-04-17 16:15:50 +00:00
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
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;
public static BukkitAudiences ADVENTURE;
2023-04-17 16:15:50 +00:00
private boolean enabled = false;
@Override
public void onLoad() {
PacketEvents.setAPI(SpigotPacketEventsBuilder.build(this));
PacketEvents.getAPI().getSettings().checkForUpdates(false);
2023-04-22 19:15:00 +00:00
PacketEvents.getAPI().load();
LOGGER = getLogger();
PLUGIN_FOLDER = getDataFolder();
PATH_FOLDER = new File(PLUGIN_FOLDER, "paths");
}
private void log(String str) {
Bukkit.getConsoleSender().sendMessage(str);
}
@Override
2023-04-17 16:15:50 +00:00
public void onEnable() {
log(ChatColor.YELLOW + " ___ __ __ __");
log(ChatColor.YELLOW + " _/ |\\ | |__) | (__` " + ChatColor.GOLD + "__|__ " + ChatColor.YELLOW + getDescription().getName() + " " + ChatColor.GOLD + "v" + getDescription().getVersion());
log(ChatColor.YELLOW + " /__ | \\| | |__ .__) " + ChatColor.GOLD + " | " + ChatColor.GRAY + "Maintained with " + ChatColor.RED + "\u2764 " + ChatColor.GRAY + " by Pyr#6969");
log("");
if (Bukkit.getPluginManager().isPluginEnabled("ServersNPC")) {
log(ChatColor.DARK_RED + " * Detected old version of ZNPCs! Disabling the plugin.");
log("");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
long before = System.currentTimeMillis();
File oldFolder = new File(PLUGIN_FOLDER.getParent(), "ServersNPC");
if (!PLUGIN_FOLDER.exists() && oldFolder.exists()) {
log(ChatColor.WHITE + " * Converting old ZNPCs files...");
try {
FileUtils.moveDirectory(oldFolder, PLUGIN_FOLDER);
} catch (IOException e) {
log(ChatColor.RED + " * Failed to convert old ZNPCs files" + (e.getMessage() == null ? "" : " due to " + e.getMessage()));
}
}
log(ChatColor.WHITE + " * Initializing Adventure...");
ADVENTURE = BukkitAudiences.create(this);
log(ChatColor.WHITE + " * Initializing PacketEvents...");
PacketEvents.getAPI().getEventManager().registerListener(new InteractionPacketListener(), PacketListenerPriority.MONITOR);
PacketEvents.getAPI().init();
PLUGIN_FOLDER.mkdirs();
PATH_FOLDER.mkdirs();
2023-04-17 16:15:50 +00:00
log(ChatColor.WHITE + " * Loading paths...");
2023-04-17 16:15:50 +00:00
loadAllPaths();
log(ChatColor.WHITE + " * Registering components...");
2023-04-17 16:15:50 +00:00
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);
log(ChatColor.WHITE + " * Starting tasks...");
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);
if (ConfigurationConstants.CHECK_FOR_UPDATES) new UpdateNotificationListener(this, new UpdateChecker(this));
enabled = true;
log(ChatColor.WHITE + " * Loading complete! (" + (System.currentTimeMillis() - before) + "ms)");
log("");
2023-04-24 16:12:50 +00:00
if (ConfigurationConstants.DEBUG_ENABLED) {
int wrap = 20;
int x = 0;
int z = 0;
World world = Bukkit.getWorld("world");
if (world == null) world = Bukkit.getWorlds().get(0);
for (NPCType type : NPCType.values()) {
2023-04-24 21:31:48 +00:00
NPC npc = new NPC(world, type, new PacketLocation(x * 3, 200, z * 3, 0, 0));
if (type.getType() == EntityTypes.PLAYER) NPCSkin.forName("Pyrbu", (skin, ex) -> npc.setProperty(NPCProperty.SKIN, skin));
NPCRegistry.register("debug_npc" + (z * wrap + x), npc);
2023-04-24 16:12:50 +00:00
if (x++ > wrap) {
x = 0;
z++;
}
}
}
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);
ADVENTURE.close();
ADVENTURE = null;
2023-04-17 16:15:50 +00:00
}
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
}
}
}