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

270 lines
15 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.PacketEventsAPI;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder;
import lol.pyr.director.adventure.command.CommandContext;
import lol.pyr.director.adventure.command.CommandManager;
2023-05-04 04:07:07 +00:00
import lol.pyr.director.adventure.command.MultiCommand;
2023-05-11 04:41:15 +00:00
import lol.pyr.director.adventure.parse.primitive.BooleanParser;
2023-05-21 04:10:01 +00:00
import lol.pyr.director.adventure.parse.primitive.DoubleParser;
2023-05-10 15:01:14 +00:00
import lol.pyr.director.adventure.parse.primitive.IntegerParser;
import lol.pyr.director.common.message.Message;
2023-05-21 12:45:43 +00:00
import lol.pyr.znpcsplus.api.NpcApiProvider;
2023-05-28 05:32:00 +00:00
import lol.pyr.znpcsplus.api.interaction.InteractionType;
2023-05-09 10:47:43 +00:00
import lol.pyr.znpcsplus.commands.*;
2023-05-21 11:33:45 +00:00
import lol.pyr.znpcsplus.commands.action.ActionAddCommand;
import lol.pyr.znpcsplus.commands.action.ActionDeleteCommand;
2023-05-28 05:32:00 +00:00
import lol.pyr.znpcsplus.commands.action.ActionEditCommand;
2023-05-21 11:33:45 +00:00
import lol.pyr.znpcsplus.commands.action.ActionListCommand;
2023-05-10 13:43:05 +00:00
import lol.pyr.znpcsplus.commands.hologram.*;
2023-06-18 17:33:47 +00:00
import lol.pyr.znpcsplus.commands.storage.ImportCommand;
2023-05-12 07:28:10 +00:00
import lol.pyr.znpcsplus.commands.storage.LoadAllCommand;
import lol.pyr.znpcsplus.commands.storage.SaveAllCommand;
import lol.pyr.znpcsplus.config.ConfigManager;
2023-06-18 17:33:47 +00:00
import lol.pyr.znpcsplus.conversion.DataImporterRegistry;
2023-05-10 13:43:05 +00:00
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
2023-05-21 15:41:38 +00:00
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.interaction.ActionRegistry;
import lol.pyr.znpcsplus.interaction.InteractionPacketListener;
import lol.pyr.znpcsplus.metadata.*;
2023-05-21 12:45:43 +00:00
import lol.pyr.znpcsplus.npc.*;
import lol.pyr.znpcsplus.packets.*;
2023-05-24 14:07:55 +00:00
import lol.pyr.znpcsplus.parsers.*;
2023-04-29 18:42:04 +00:00
import lol.pyr.znpcsplus.scheduling.FoliaScheduler;
import lol.pyr.znpcsplus.scheduling.SpigotScheduler;
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.skin.cache.SkinCache;
import lol.pyr.znpcsplus.skin.cache.SkinCacheCleanTask;
2023-05-24 20:47:30 +00:00
import lol.pyr.znpcsplus.tasks.NpcProcessorTask;
import lol.pyr.znpcsplus.updater.UpdateChecker;
import lol.pyr.znpcsplus.updater.UpdateNotificationListener;
import lol.pyr.znpcsplus.user.UserListener;
import lol.pyr.znpcsplus.user.UserManager;
2023-06-13 21:57:43 +00:00
import lol.pyr.znpcsplus.util.*;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
2023-04-26 17:50:40 +00:00
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
2023-06-13 21:57:43 +00:00
import net.kyori.adventure.text.minimessage.MiniMessage;
2023-05-11 06:27:28 +00:00
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
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;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
2023-04-17 16:15:50 +00:00
import org.bukkit.plugin.java.JavaPlugin;
2023-06-13 21:57:43 +00:00
import java.io.Reader;
2023-05-21 11:33:45 +00:00
import java.util.ArrayList;
import java.util.HashMap;
2023-05-21 11:33:45 +00:00
import java.util.List;
2023-04-17 16:15:50 +00:00
2023-05-21 11:33:45 +00:00
public class ZNpcsPlus extends JavaPlugin {
2023-05-11 05:46:56 +00:00
private static final int PLUGIN_ID = 18244;
2023-05-21 11:33:45 +00:00
private PacketEventsAPI<Plugin> packetEvents;
private final LegacyComponentSerializer textSerializer = LegacyComponentSerializer.builder()
2023-05-11 06:27:28 +00:00
.character('&')
.hexCharacter('#')
.hexColors().build();
2023-04-17 16:15:50 +00:00
2023-05-21 11:33:45 +00:00
private final List<Runnable> shutdownTasks = new ArrayList<>();
private boolean enabled = false;
2023-04-26 17:50:40 +00:00
@Override
public void onLoad() {
packetEvents = SpigotPacketEventsBuilder.build(this);
PacketEvents.setAPI(packetEvents);
packetEvents.getSettings().checkForUpdates(false);
packetEvents.load();
}
private void log(String str) {
Bukkit.getConsoleSender().sendMessage(str);
}
@Override
2023-04-17 16:15:50 +00:00
public void onEnable() {
getDataFolder().mkdirs();
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("");
PluginManager pluginManager = Bukkit.getPluginManager();
if (pluginManager.isPluginEnabled("ServersNPC")) log(ChatColor.DARK_RED + " * Old version of znpcs detected! The plugin might not work correctly!");
long before = System.currentTimeMillis();
2023-05-21 11:33:45 +00:00
log(ChatColor.WHITE + " * Initializing libraries...");
packetEvents.init();
BukkitAudiences adventure = BukkitAudiences.create(this);
2023-05-21 11:33:45 +00:00
log(ChatColor.WHITE + " * Initializing components...");
TaskScheduler scheduler = FoliaUtil.isFolia() ? new FoliaScheduler(this) : new SpigotScheduler(this);
MetadataFactory metadataFactory = setupMetadataFactory();
ConfigManager configManager = new ConfigManager(getDataFolder());
2023-05-21 13:25:21 +00:00
SkinCache skinCache = new SkinCache(configManager);
2023-05-21 15:41:38 +00:00
EntityPropertyRegistryImpl propertyRegistry = new EntityPropertyRegistryImpl(skinCache);
2023-05-21 13:25:21 +00:00
PacketFactory packetFactory = setupPacketFactory(scheduler, metadataFactory, propertyRegistry);
BungeeConnector bungeeConnector = new BungeeConnector(this);
2023-05-21 11:33:45 +00:00
ActionRegistry actionRegistry = new ActionRegistry();
2023-05-21 15:41:38 +00:00
NpcTypeRegistryImpl typeRegistry = new NpcTypeRegistryImpl();
2023-06-18 17:33:47 +00:00
NpcRegistryImpl npcRegistry = new NpcRegistryImpl(configManager, this, packetFactory, actionRegistry,
scheduler, typeRegistry, propertyRegistry, textSerializer);
2023-05-21 11:33:45 +00:00
UserManager userManager = new UserManager();
2023-06-18 17:33:47 +00:00
DataImporterRegistry importerRegistry = new DataImporterRegistry(configManager, adventure, bungeeConnector,
scheduler, packetFactory, textSerializer, typeRegistry, getDataFolder().getParentFile(),
propertyRegistry, skinCache);
2023-05-21 11:33:45 +00:00
2023-05-21 11:53:48 +00:00
log(ChatColor.WHITE + " * Registerring components...");
2023-05-21 13:25:21 +00:00
typeRegistry.registerDefault(packetEvents, propertyRegistry);
2023-05-28 05:32:00 +00:00
actionRegistry.registerTypes(scheduler, adventure, bungeeConnector, textSerializer);
2023-05-21 11:33:45 +00:00
packetEvents.getEventManager().registerListener(new InteractionPacketListener(userManager, npcRegistry), PacketListenerPriority.MONITOR);
2023-04-17 17:36:19 +00:00
new Metrics(this, PLUGIN_ID);
pluginManager.registerEvents(new UserListener(userManager), this);
2023-05-21 11:33:45 +00:00
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
2023-06-13 21:57:43 +00:00
2023-06-18 17:33:47 +00:00
registerCommands(npcRegistry, skinCache, adventure, actionRegistry, typeRegistry, propertyRegistry, importerRegistry);
2023-05-21 11:33:45 +00:00
log(ChatColor.WHITE + " * Starting tasks...");
if (configManager.getConfig().checkForUpdates()) {
UpdateChecker updateChecker = new UpdateChecker(this.getDescription());
scheduler.runDelayedTimerAsync(updateChecker, 5L, 6000L);
pluginManager.registerEvents(new UpdateNotificationListener(this, adventure, updateChecker), this);
}
2023-05-24 20:47:30 +00:00
scheduler.runDelayedTimerAsync(new NpcProcessorTask(npcRegistry, configManager, propertyRegistry), 60L, 3L);
scheduler.runDelayedTimerAsync(new SkinCacheCleanTask(skinCache), 1200, 1200);
2023-05-21 11:33:45 +00:00
log(ChatColor.WHITE + " * Loading data...");
npcRegistry.reload();
2023-05-21 11:33:45 +00:00
shutdownTasks.add(scheduler::cancelAll);
shutdownTasks.add(userManager::shutdown);
shutdownTasks.add(adventure::close);
2023-05-21 11:53:48 +00:00
if (configManager.getConfig().autoSaveEnabled()) shutdownTasks.add(npcRegistry::save);
2023-05-21 11:33:45 +00:00
NpcApiProvider.register(this, new ZNpcsPlusApi(npcRegistry, typeRegistry, propertyRegistry));
enabled = true;
log(ChatColor.WHITE + " * Loading complete! (" + (System.currentTimeMillis() - before) + "ms)");
log("");
2023-04-24 16:12:50 +00:00
if (configManager.getConfig().debugEnabled()) {
2023-04-24 16:12:50 +00:00
World world = Bukkit.getWorld("world");
if (world == null) world = Bukkit.getWorlds().get(0);
int i = 0;
2023-06-18 22:20:18 +00:00
for (NpcTypeImpl type : typeRegistry.getAllImpl()) {
2023-05-21 12:45:43 +00:00
NpcEntryImpl entry = npcRegistry.create("debug_npc_" + i, world, type, new NpcLocation(i * 3, 200, 0, 0, 0));
2023-05-04 04:07:07 +00:00
entry.setProcessed(true);
2023-05-04 07:25:05 +00:00
NpcImpl npc = entry.getNpc();
2023-05-22 16:10:03 +00:00
npc.getHologram().addLineComponent(Component.text("Hello, World!"));
npc.setProperty(propertyRegistry.getByName("look", Boolean.class), true);
i++;
2023-04-24 16:12:50 +00:00
}
}
2023-04-17 16:15:50 +00:00
}
2023-05-21 11:33:45 +00:00
@Override
public void onDisable() {
if (!enabled) return;
2023-05-21 12:45:43 +00:00
NpcApiProvider.unregister();
2023-05-21 11:33:45 +00:00
for (Runnable runnable : shutdownTasks) runnable.run();
}
2023-05-21 15:41:38 +00:00
private PacketFactory setupPacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory, EntityPropertyRegistryImpl propertyRegistry) {
HashMap<ServerVersion, LazyLoader<? extends PacketFactory>> versions = new HashMap<>();
2023-05-21 13:25:21 +00:00
versions.put(ServerVersion.V_1_8, LazyLoader.of(() -> new V1_8PacketFactory(scheduler, metadataFactory, packetEvents, propertyRegistry)));
versions.put(ServerVersion.V_1_9, LazyLoader.of(() -> new V1_9PacketFactory(scheduler, metadataFactory, packetEvents, propertyRegistry)));
versions.put(ServerVersion.V_1_10, LazyLoader.of(() -> new V1_10PacketFactory(scheduler, metadataFactory, packetEvents, propertyRegistry)));
versions.put(ServerVersion.V_1_14, LazyLoader.of(() -> new V1_14PacketFactory(scheduler, metadataFactory, packetEvents, propertyRegistry)));
versions.put(ServerVersion.V_1_19, LazyLoader.of(() -> new V1_19PacketFactory(scheduler, metadataFactory, packetEvents, propertyRegistry)));
ServerVersion version = packetEvents.getServerManager().getVersion();
if (versions.containsKey(version)) return versions.get(version).get();
for (ServerVersion v : ServerVersion.reversedValues()) {
if (v.isNewerThan(version)) continue;
if (!versions.containsKey(v)) continue;
return versions.get(v).get();
}
throw new RuntimeException("Unsupported version!");
}
private MetadataFactory setupMetadataFactory() {
HashMap<ServerVersion, LazyLoader<? extends MetadataFactory>> versions = new HashMap<>();
versions.put(ServerVersion.V_1_8, LazyLoader.of(V1_8MetadataFactory::new));
versions.put(ServerVersion.V_1_9, LazyLoader.of(V1_9MetadataFactory::new));
versions.put(ServerVersion.V_1_10, LazyLoader.of(V1_10MetadataFactory::new));
versions.put(ServerVersion.V_1_13, LazyLoader.of(V1_13MetadataFactory::new));
versions.put(ServerVersion.V_1_14, LazyLoader.of(V1_14MetadataFactory::new));
versions.put(ServerVersion.V_1_16, LazyLoader.of(V1_16MetadataFactory::new));
versions.put(ServerVersion.V_1_17, LazyLoader.of(V1_17MetadataFactory::new));
ServerVersion version = packetEvents.getServerManager().getVersion();
if (versions.containsKey(version)) return versions.get(version).get();
for (ServerVersion v : ServerVersion.reversedValues()) {
if (v.isNewerThan(version)) continue;
if (!versions.containsKey(v)) continue;
return versions.get(v).get();
}
throw new RuntimeException("Unsupported version!");
}
2023-06-18 17:33:47 +00:00
private void registerCommands(NpcRegistryImpl npcRegistry, SkinCache skinCache, BukkitAudiences adventure,
ActionRegistry actionRegistry, NpcTypeRegistryImpl typeRegistry,
EntityPropertyRegistryImpl propertyRegistry, DataImporterRegistry importerRegistry) {
2023-05-21 11:33:45 +00:00
Message<CommandContext> incorrectUsageMessage = context -> context.send(Component.text("Incorrect usage: /" + context.getUsage(), NamedTextColor.RED));
CommandManager manager = new CommandManager(this, adventure, incorrectUsageMessage);
2023-05-21 12:45:43 +00:00
manager.registerParser(NpcTypeImpl.class, new NpcTypeParser(incorrectUsageMessage, typeRegistry));
manager.registerParser(NpcEntryImpl.class, new NpcEntryParser(npcRegistry, incorrectUsageMessage));
2023-05-21 13:25:21 +00:00
manager.registerParser(EntityPropertyImpl.class, new EntityPropertyParser(incorrectUsageMessage, propertyRegistry));
manager.registerParser(Integer.class, new IntegerParser(incorrectUsageMessage));
manager.registerParser(Double.class, new DoubleParser(incorrectUsageMessage));
manager.registerParser(Boolean.class, new BooleanParser(incorrectUsageMessage));
manager.registerParser(NamedTextColor.class, new NamedTextColorParser(incorrectUsageMessage));
2023-05-24 14:07:55 +00:00
manager.registerParser(InteractionType.class, new InteractionTypeParser(incorrectUsageMessage));
2023-05-10 15:01:14 +00:00
2023-06-14 13:30:43 +00:00
manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root"))
2023-05-21 12:45:43 +00:00
.addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry))
2023-05-21 13:25:21 +00:00
.addSubcommand("skin", new SkinCommand(skinCache, npcRegistry, typeRegistry, propertyRegistry))
.addSubcommand("delete", new DeleteCommand(npcRegistry, adventure))
.addSubcommand("move", new MoveCommand(npcRegistry))
2023-06-14 12:45:04 +00:00
.addSubcommand("property", new PropertyCommand(npcRegistry))
.addSubcommand("teleport", new TeleportCommand(npcRegistry))
.addSubcommand("list", new ListCommand(npcRegistry))
.addSubcommand("near", new NearCommand(npcRegistry))
2023-05-21 12:45:43 +00:00
.addSubcommand("type", new TypeCommand(npcRegistry, typeRegistry))
2023-06-14 13:30:43 +00:00
.addSubcommand("storage", new MultiCommand(loadHelpMessage("storage"))
.addSubcommand("save", new SaveAllCommand(npcRegistry))
2023-06-18 17:33:47 +00:00
.addSubcommand("reload", new LoadAllCommand(npcRegistry))
.addSubcommand("import", new ImportCommand(npcRegistry, importerRegistry)))
2023-06-14 13:30:43 +00:00
.addSubcommand("holo", new MultiCommand(loadHelpMessage("holo"))
.addSubcommand("add", new HoloAddCommand(npcRegistry, textSerializer))
.addSubcommand("delete", new HoloDeleteCommand(npcRegistry))
.addSubcommand("info", new HoloInfoCommand(npcRegistry))
.addSubcommand("insert", new HoloInsertCommand(npcRegistry, textSerializer))
.addSubcommand("set", new HoloSetCommand(npcRegistry, textSerializer))
2023-05-21 04:10:01 +00:00
.addSubcommand("offset", new HoloOffsetCommand(npcRegistry)))
2023-06-14 13:30:43 +00:00
.addSubcommand("action", new MultiCommand(loadHelpMessage("action"))
2023-05-28 05:32:00 +00:00
.addSubcommand("add", new ActionAddCommand(npcRegistry, actionRegistry))
2023-05-21 11:33:45 +00:00
.addSubcommand("delete", new ActionDeleteCommand(npcRegistry))
2023-05-28 05:32:00 +00:00
.addSubcommand("edit", new ActionEditCommand(npcRegistry, actionRegistry))
2023-06-07 10:25:04 +00:00
.addSubcommand("list", new ActionListCommand(npcRegistry)))
2023-05-09 10:47:43 +00:00
);
}
2023-06-14 13:30:43 +00:00
private Message<CommandContext> loadHelpMessage(String name) {
Reader reader = getTextResource("help-messages/" + name + ".txt");
if (reader == null) throw new RuntimeException(name + ".txt is missing from the help-messages folder in the ZNPCsPlus jar!");
Component component = MiniMessage.miniMessage().deserialize(FileUtil.dumpReaderAsString(reader));
return context -> context.send(component);
}
2023-04-17 16:15:50 +00:00
}