From d9486acbd23a1c0b43e4e32eacd656953e37c2fa Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Thu, 11 May 2023 06:46:56 +0100 Subject: [PATCH] working persistance --- .../main/java/lol/pyr/znpcsplus/ZNpcsPlus.java | 15 ++++++++++----- .../java/lol/pyr/znpcsplus/config/MainConfig.java | 6 ++++++ .../interaction/types/SwitchServerAction.java | 2 +- .../lol/pyr/znpcsplus/npc/NpcRegistryImpl.java | 15 ++++++++++++++- .../pyr/znpcsplus/storage/yaml/YamlStorage.java | 6 ++++-- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index e1feea1..d0d5e3b 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -53,14 +53,16 @@ import java.io.IOException; import java.util.logging.Logger; public class ZNpcsPlus extends JavaPlugin { + private static final int PLUGIN_ID = 18244; + public static boolean PLACEHOLDERS_SUPPORTED; + public static Logger LOGGER; public static File PLUGIN_FOLDER; public static File PATH_FOLDER; - private static final int PLUGIN_ID = 18244; + public static TaskScheduler SCHEDULER; - public static BungeeUtil BUNGEE_UTILS; + public static BungeeUtil BUNGEE_UTIL; public static BukkitAudiences ADVENTURE; - public static boolean PLACEHOLDERS_SUPPORTED; private boolean enabled = false; public static final String DEBUG_NPC_PREFIX = "debug_npc"; @@ -129,7 +131,7 @@ public class ZNpcsPlus extends JavaPlugin { getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); new Metrics(this, PLUGIN_ID); SCHEDULER = FoliaUtil.isFolia() ? new FoliaScheduler(this) : new SpigotScheduler(this); - BUNGEE_UTILS = new BungeeUtil(this); + BUNGEE_UTIL = new BungeeUtil(this); Bukkit.getOnlinePlayers().forEach(User::get); registerCommands(); @@ -139,6 +141,9 @@ public class ZNpcsPlus extends JavaPlugin { new UserListener(this); if (Configs.config().checkForUpdates()) new UpdateNotificationListener(this, new UpdateChecker(this)); + log(ChatColor.WHITE+ " * Loading NPCs..."); + NpcRegistryImpl.get().reload(); + ZApiProvider.register(new ZNpcsApi()); enabled = true; log(ChatColor.WHITE + " * Loading complete! (" + (System.currentTimeMillis() - before) + "ms)"); @@ -159,7 +164,6 @@ public class ZNpcsPlus extends JavaPlugin { npc.setProperty(EntityPropertyImpl.INVISIBLE, true); } npc.setProperty(EntityPropertyImpl.GLOW, NamedTextColor.RED); - // npc.setProperty(EntityProperty.FIRE, true); npc.getHologram().addLine(Component.text("Hello, World!")); if (x++ > wrap) { x = 0; @@ -183,6 +187,7 @@ public class ZNpcsPlus extends JavaPlugin { @Override public void onDisable() { if (!enabled) return; + NpcRegistryImpl.get().save(); ZApiProvider.unregister(); Bukkit.getOnlinePlayers().forEach(User::remove); ADVENTURE.close(); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/config/MainConfig.java b/plugin/src/main/java/lol/pyr/znpcsplus/config/MainConfig.java index f34c2f0..3e66cf1 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/config/MainConfig.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/config/MainConfig.java @@ -1,5 +1,6 @@ package lol.pyr.znpcsplus.config; +import lol.pyr.znpcsplus.storage.NpcStorageType; import space.arim.dazzleconf.annote.ConfComments; import space.arim.dazzleconf.annote.ConfKey; @@ -25,4 +26,9 @@ public interface MainConfig { @ConfComments({"Should debug mode be enabled?", "This is used in development to test various things, you probably don't want to enable this"}) @DefaultBoolean(false) boolean debugEnabled(); + + @ConfKey("storage-type") + @ConfComments("The storage type to use. Available storage types: YAML") + @DefaultString("YAML") + NpcStorageType storageType(); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/types/SwitchServerAction.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/types/SwitchServerAction.java index ad2c234..f0fb0b2 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/types/SwitchServerAction.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/types/SwitchServerAction.java @@ -12,7 +12,7 @@ public class SwitchServerAction extends NpcAction { @Override public void run(Player player) { - ZNpcsPlus.BUNGEE_UTILS.sendPlayerToServer(player, argument); + ZNpcsPlus.BUNGEE_UTIL.sendPlayerToServer(player, argument); } @Override diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java index a7e1cd6..60aa9ed 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java @@ -2,6 +2,8 @@ package lol.pyr.znpcsplus.npc; import lol.pyr.znpcsplus.api.npc.NpcRegistry; import lol.pyr.znpcsplus.api.npc.NpcType; +import lol.pyr.znpcsplus.config.Configs; +import lol.pyr.znpcsplus.storage.NpcStorage; import lol.pyr.znpcsplus.util.ZLocation; import org.bukkit.World; @@ -13,13 +15,24 @@ import java.util.stream.Collectors; public class NpcRegistryImpl implements NpcRegistry { private final static NpcRegistryImpl registry = new NpcRegistryImpl(); - public static NpcRegistryImpl get() { return registry; } + private final NpcStorage STORAGE; + private NpcRegistryImpl() { if (registry != null) throw new UnsupportedOperationException("This class can only be instanciated once!"); + STORAGE = Configs.config().storageType().create(); + } + + public void reload() { + npcMap.clear(); + for (NpcEntryImpl entry : STORAGE.loadNpcs()) npcMap.put(entry.getId(), entry); + } + + public void save() { + STORAGE.saveNpcs(npcMap.values().stream().filter(NpcEntryImpl::isSave).collect(Collectors.toList())); } private final Map npcMap = new HashMap<>(); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java b/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java index 26e12e7..3c33266 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java @@ -69,7 +69,9 @@ public class YamlStorage implements NpcStorage { @Override public void saveNpcs(Collection npcs) { - for (NpcEntryImpl entry : npcs) if (entry.isSave()) try { + File[] files = npcsFolder.listFiles(); + if (files != null && files.length != 0) for (File file : files) file.delete(); + for (NpcEntryImpl entry : npcs) try { YamlConfiguration config = new YamlConfiguration(); config.set("id", entry.getId()); config.set("is-processed", entry.isProcessed()); @@ -99,7 +101,7 @@ public class YamlStorage implements NpcStorage { } config.set("action-amount", i); - config.save(new File(entry.getId() + ".yml")); + config.save(new File(npcsFolder, entry.getId() + ".yml")); } catch (IOException e) { throw new RuntimeException(e); }