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;
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();

@ -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();
}

@ -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

@ -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<String, NpcEntryImpl> npcMap = new HashMap<>();

@ -69,7 +69,9 @@ public class YamlStorage implements NpcStorage {
@Override
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();
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);
}