working persistance

This commit is contained in:
Pyrbu 2023-05-11 06:46:56 +01:00
parent 3dd3bcc03e
commit d9486acbd2
5 changed files with 35 additions and 9 deletions

@ -53,14 +53,16 @@ import java.io.IOException;
import java.util.logging.Logger; import java.util.logging.Logger;
public class ZNpcsPlus extends JavaPlugin { public class ZNpcsPlus extends JavaPlugin {
private static final int PLUGIN_ID = 18244;
public static boolean PLACEHOLDERS_SUPPORTED;
public static Logger LOGGER; public static Logger LOGGER;
public static File PLUGIN_FOLDER; public static File PLUGIN_FOLDER;
public static File PATH_FOLDER; public static File PATH_FOLDER;
private static final int PLUGIN_ID = 18244;
public static TaskScheduler SCHEDULER; public static TaskScheduler SCHEDULER;
public static BungeeUtil BUNGEE_UTILS; public static BungeeUtil BUNGEE_UTIL;
public static BukkitAudiences ADVENTURE; public static BukkitAudiences ADVENTURE;
public static boolean PLACEHOLDERS_SUPPORTED;
private boolean enabled = false; private boolean enabled = false;
public static final String DEBUG_NPC_PREFIX = "debug_npc"; public static final String DEBUG_NPC_PREFIX = "debug_npc";
@ -129,7 +131,7 @@ public class ZNpcsPlus extends JavaPlugin {
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
new Metrics(this, PLUGIN_ID); new Metrics(this, PLUGIN_ID);
SCHEDULER = FoliaUtil.isFolia() ? new FoliaScheduler(this) : new SpigotScheduler(this); SCHEDULER = FoliaUtil.isFolia() ? new FoliaScheduler(this) : new SpigotScheduler(this);
BUNGEE_UTILS = new BungeeUtil(this); BUNGEE_UTIL = new BungeeUtil(this);
Bukkit.getOnlinePlayers().forEach(User::get); Bukkit.getOnlinePlayers().forEach(User::get);
registerCommands(); registerCommands();
@ -139,6 +141,9 @@ public class ZNpcsPlus extends JavaPlugin {
new UserListener(this); new UserListener(this);
if (Configs.config().checkForUpdates()) new UpdateNotificationListener(this, new UpdateChecker(this)); if (Configs.config().checkForUpdates()) new UpdateNotificationListener(this, new UpdateChecker(this));
log(ChatColor.WHITE+ " * Loading NPCs...");
NpcRegistryImpl.get().reload();
ZApiProvider.register(new ZNpcsApi()); ZApiProvider.register(new ZNpcsApi());
enabled = true; enabled = true;
log(ChatColor.WHITE + " * Loading complete! (" + (System.currentTimeMillis() - before) + "ms)"); 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.INVISIBLE, true);
} }
npc.setProperty(EntityPropertyImpl.GLOW, NamedTextColor.RED); npc.setProperty(EntityPropertyImpl.GLOW, NamedTextColor.RED);
// npc.setProperty(EntityProperty.FIRE, true);
npc.getHologram().addLine(Component.text("Hello, World!")); npc.getHologram().addLine(Component.text("Hello, World!"));
if (x++ > wrap) { if (x++ > wrap) {
x = 0; x = 0;
@ -183,6 +187,7 @@ public class ZNpcsPlus extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
if (!enabled) return; if (!enabled) return;
NpcRegistryImpl.get().save();
ZApiProvider.unregister(); ZApiProvider.unregister();
Bukkit.getOnlinePlayers().forEach(User::remove); Bukkit.getOnlinePlayers().forEach(User::remove);
ADVENTURE.close(); ADVENTURE.close();

@ -1,5 +1,6 @@
package lol.pyr.znpcsplus.config; package lol.pyr.znpcsplus.config;
import lol.pyr.znpcsplus.storage.NpcStorageType;
import space.arim.dazzleconf.annote.ConfComments; import space.arim.dazzleconf.annote.ConfComments;
import space.arim.dazzleconf.annote.ConfKey; 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"}) @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) @DefaultBoolean(false)
boolean debugEnabled(); boolean debugEnabled();
@ConfKey("storage-type")
@ConfComments("The storage type to use. Available storage types: YAML")
@DefaultString("YAML")
NpcStorageType storageType();
} }

@ -12,7 +12,7 @@ public class SwitchServerAction extends NpcAction {
@Override @Override
public void run(Player player) { public void run(Player player) {
ZNpcsPlus.BUNGEE_UTILS.sendPlayerToServer(player, argument); ZNpcsPlus.BUNGEE_UTIL.sendPlayerToServer(player, argument);
} }
@Override @Override

@ -2,6 +2,8 @@ package lol.pyr.znpcsplus.npc;
import lol.pyr.znpcsplus.api.npc.NpcRegistry; import lol.pyr.znpcsplus.api.npc.NpcRegistry;
import lol.pyr.znpcsplus.api.npc.NpcType; 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 lol.pyr.znpcsplus.util.ZLocation;
import org.bukkit.World; import org.bukkit.World;
@ -13,13 +15,24 @@ import java.util.stream.Collectors;
public class NpcRegistryImpl implements NpcRegistry { public class NpcRegistryImpl implements NpcRegistry {
private final static NpcRegistryImpl registry = new NpcRegistryImpl(); private final static NpcRegistryImpl registry = new NpcRegistryImpl();
public static NpcRegistryImpl get() { public static NpcRegistryImpl get() {
return registry; return registry;
} }
private final NpcStorage STORAGE;
private NpcRegistryImpl() { private NpcRegistryImpl() {
if (registry != null) throw new UnsupportedOperationException("This class can only be instanciated once!"); 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<String, NpcEntryImpl> npcMap = new HashMap<>(); private final Map<String, NpcEntryImpl> npcMap = new HashMap<>();

@ -69,7 +69,9 @@ public class YamlStorage implements NpcStorage {
@Override @Override
public void saveNpcs(Collection<NpcEntryImpl> npcs) { public void saveNpcs(Collection<NpcEntryImpl> 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(); YamlConfiguration config = new YamlConfiguration();
config.set("id", entry.getId()); config.set("id", entry.getId());
config.set("is-processed", entry.isProcessed()); config.set("is-processed", entry.isProcessed());
@ -99,7 +101,7 @@ public class YamlStorage implements NpcStorage {
} }
config.set("action-amount", i); config.set("action-amount", i);
config.save(new File(entry.getId() + ".yml")); config.save(new File(npcsFolder, entry.getId() + ".yml"));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }