From ea732105a359c008a5fd47a2f7226ba61516bbd4 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Fri, 12 May 2023 08:28:10 +0100 Subject: [PATCH] add save & load commands --- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 5 ++++ .../commands/storage/LoadAllCommand.java | 27 +++++++++++++++++++ .../commands/storage/SaveAllCommand.java | 27 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/storage/LoadAllCommand.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/storage/SaveAllCommand.java diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 02c539e..ad30d04 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -14,6 +14,8 @@ import lol.pyr.znpcsplus.commands.parsers.EntityPropertyParser; import lol.pyr.znpcsplus.commands.parsers.NamedTextColorParser; import lol.pyr.znpcsplus.commands.parsers.NpcEntryParser; import lol.pyr.znpcsplus.commands.parsers.NpcTypeParser; +import lol.pyr.znpcsplus.commands.storage.LoadAllCommand; +import lol.pyr.znpcsplus.commands.storage.SaveAllCommand; import lol.pyr.znpcsplus.config.Configs; import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.interaction.InteractionPacketListener; @@ -197,6 +199,9 @@ public class ZNpcsPlus extends JavaPlugin { .addSubcommand("teleport", new TeleportCommand()) .addSubcommand("list", new ListCommand()) .addSubcommand("near", new NearCommand()) + .addSubcommand("storage", new MultiCommand() + .addSubcommand("save", new SaveAllCommand()) + .addSubcommand("load", new LoadAllCommand())) .addSubcommand("holo", new MultiCommand() .addSubcommand("add", new HoloAddCommand()) .addSubcommand("delete", new HoloDeleteCommand()) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/storage/LoadAllCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/storage/LoadAllCommand.java new file mode 100644 index 0000000..5a500f9 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/storage/LoadAllCommand.java @@ -0,0 +1,27 @@ +package lol.pyr.znpcsplus.commands.storage; + +import lol.pyr.director.adventure.command.CommandContext; +import lol.pyr.director.adventure.command.CommandHandler; +import lol.pyr.director.common.command.CommandExecutionException; +import lol.pyr.znpcsplus.npc.NpcRegistryImpl; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +public class LoadAllCommand implements CommandHandler { + @Override + public void run(CommandContext context) throws CommandExecutionException { + CompletableFuture.runAsync(() -> { + NpcRegistryImpl.get().reload(); + context.send(Component.text("All NPCs have been re-loaded from storage", NamedTextColor.GREEN)); + }); + } + + @Override + public List suggest(CommandContext context) throws CommandExecutionException { + return Collections.emptyList(); + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/storage/SaveAllCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/storage/SaveAllCommand.java new file mode 100644 index 0000000..75bfb58 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/storage/SaveAllCommand.java @@ -0,0 +1,27 @@ +package lol.pyr.znpcsplus.commands.storage; + +import lol.pyr.director.adventure.command.CommandContext; +import lol.pyr.director.adventure.command.CommandHandler; +import lol.pyr.director.common.command.CommandExecutionException; +import lol.pyr.znpcsplus.npc.NpcRegistryImpl; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +public class SaveAllCommand implements CommandHandler { + @Override + public void run(CommandContext context) throws CommandExecutionException { + CompletableFuture.runAsync(() -> { + NpcRegistryImpl.get().save(); + context.send(Component.text("All NPCs have been saved to storage", NamedTextColor.GREEN)); + }); + } + + @Override + public List suggest(CommandContext context) throws CommandExecutionException { + return Collections.emptyList(); + } +}