فهرست منبع

Improved saving of NPCs with the help of ConfigLoader and some renaming

Jan 3 هفته پیش
والد
کامیت
1bead990d3

+ 3 - 3
src/main/java/me/lethunderhawk/fluxapi/FluxService.java

@@ -1,6 +1,6 @@
 package me.lethunderhawk.fluxapi;
 
-import me.lethunderhawk.fluxapi.util.interfaces.BazaarFluxModule;
+import me.lethunderhawk.fluxapi.util.interfaces.FluxAPIModule;
 
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -13,7 +13,7 @@ public final class FluxService {
         services.put(type, service);
         return service;
     }
-    public static <T> void registerModule(Class<? extends BazaarFluxModule> type, T service) {
+    public static <T> void registerModule(Class<? extends FluxAPIModule> type, T service) {
         services.put(type, service);
     }
 
@@ -30,7 +30,7 @@ public final class FluxService {
         return (T) services.remove(type);
     }
 
-    public static <T> void unregisterModule(Class<? extends BazaarFluxModule> type) {
+    public static <T> void unregisterModule(Class<? extends FluxAPIModule> type) {
         services.remove(type);
     }
 }

+ 8 - 8
src/main/java/me/lethunderhawk/fluxapi/main/FluxAPI.java

@@ -1,8 +1,8 @@
 package me.lethunderhawk.fluxapi.main;
 
 import me.lethunderhawk.fluxapi.FluxService;
-import me.lethunderhawk.fluxapi.npc.NPCModule;
-import me.lethunderhawk.fluxapi.profile.ProfileModule;
+import me.lethunderhawk.fluxapi.npc.NPCAPIModule;
+import me.lethunderhawk.fluxapi.profile.ProfileAPIModule;
 import me.lethunderhawk.fluxapi.util.config.ConfigLoader;
 import org.bukkit.plugin.java.JavaPlugin;
 
@@ -15,18 +15,18 @@ public class FluxAPI extends JavaPlugin {
         ConfigLoader configLoader = new ConfigLoader(this);
         FluxService.register(ConfigLoader.class, configLoader);
 
-        ProfileModule profileModule = new ProfileModule(this);
+        ProfileAPIModule profileModule = new ProfileAPIModule(this);
         profileModule.onEnable();
-        FluxService.registerModule(ProfileModule.class, profileModule);
+        FluxService.registerModule(ProfileAPIModule.class, profileModule);
 
-        NPCModule npcModule = new NPCModule(this);
+        NPCAPIModule npcModule = new NPCAPIModule(this);
         npcModule.onEnable();
-        FluxService.registerModule(NPCModule.class, npcModule);
+        FluxService.registerModule(NPCAPIModule.class, npcModule);
     }
 
     @Override
     public void onDisable() {
-        FluxService.get(NPCModule.class).onDisable();
-        FluxService.get(ProfileModule.class).onDisable();
+        FluxService.get(NPCAPIModule.class).onDisable();
+        FluxService.get(ProfileAPIModule.class).onDisable();
     }
 }

+ 4 - 4
src/main/java/me/lethunderhawk/fluxapi/npc/NPCModule.java → src/main/java/me/lethunderhawk/fluxapi/npc/NPCAPIModule.java

@@ -10,18 +10,18 @@ import me.lethunderhawk.fluxapi.npc.listener.WorldLoadListener;
 import me.lethunderhawk.fluxapi.npc.manager.NPCManager;
 import me.lethunderhawk.fluxapi.npc.registry.NPCRegistry;
 import me.lethunderhawk.fluxapi.util.config.ConfigLoader;
-import me.lethunderhawk.fluxapi.util.interfaces.BazaarFluxModule;
+import me.lethunderhawk.fluxapi.util.interfaces.FluxAPIModule;
 import org.bukkit.Bukkit;
 import org.bukkit.configuration.serialization.ConfigurationSerialization;
 import org.bukkit.event.HandlerList;
 import org.bukkit.plugin.java.JavaPlugin;
 
-public final class NPCModule extends BazaarFluxModule {
+public final class NPCAPIModule extends FluxAPIModule {
     private final String saveLocation = "/npc/npcs.yml";
     private NPCManager npcManager;
     private NPCListener listener;
     private WorldLoadListener worldLoadListener;
-    public NPCModule(JavaPlugin plugin) {
+    public NPCAPIModule(JavaPlugin plugin) {
         super(plugin);
     }
 
@@ -31,7 +31,7 @@ public final class NPCModule extends BazaarFluxModule {
 
     @Override
     public void onEnable() {
-        FluxService.registerModule(NPCModule.class, this);
+        FluxService.registerModule(NPCAPIModule.class, this);
 
         registerBaseNPCSerializables();
         if(NPCRegistry.isRegistered()){

+ 4 - 3
src/main/java/me/lethunderhawk/fluxapi/npc/command/NPCCommand.java

@@ -10,7 +10,7 @@ import me.lethunderhawk.fluxapi.npc.util.MojangAPI;
 import me.lethunderhawk.fluxapi.util.command.CommandNode;
 import me.lethunderhawk.fluxapi.util.command.CustomCommand;
 import me.lethunderhawk.fluxapi.util.gui.InventoryManager;
-import me.lethunderhawk.fluxapi.util.interfaces.BazaarFluxModule;
+import me.lethunderhawk.fluxapi.util.interfaces.FluxAPIModule;
 import org.bukkit.command.CommandSender;
 import org.bukkit.entity.Player;
 
@@ -20,8 +20,9 @@ import java.util.stream.Collectors;
 
 public class NPCCommand extends CustomCommand {
 
-    public NPCCommand(BazaarFluxModule module) {
-        super(new CommandNode("npc", "Base npc command", null), module);
+    public NPCCommand(FluxAPIModule module) {
+        super(module);
+        setRootCommand(new CommandNode("npc", "Base npc command", this::sendHelp));
     }
 
     @Override

+ 12 - 12
src/main/java/me/lethunderhawk/fluxapi/npc/gui/NPCOptionsGUI.java

@@ -23,18 +23,6 @@ public class NPCOptionsGUI extends InventoryGUI {
     public NPCOptionsGUI(NPC npc) {
         super("Options for NPC: " + npc.getName(), 36);
         this.npc = npc;
-        buildGUI();
-    }
-
-    private void buildGUI() {
-        fillGlassPaneBackground();
-        ItemStack item = new ItemOptions(Material.OAK_SIGN)
-                .setName(Component.text("Rename this NPC",  NamedTextColor.YELLOW))
-                .setLore(
-                        LoreDesigner.createLore("Opens a new sign input which will allow you to rename the NPC's base Name", "Opens a new sign input which ", NamedTextColor.GRAY)
-                )
-                .buildItemStack();
-        setItemWithClickAction(4, item, this::editName);
     }
 
     private void editName(Player player, ClickType type) {
@@ -56,6 +44,18 @@ public class NPCOptionsGUI extends InventoryGUI {
         }
     }
 
+    @Override
+    public void buildContent() {
+        fillGlassPaneBackground();
+        ItemStack item = new ItemOptions(Material.OAK_SIGN)
+                .setName(Component.text("Rename this NPC",  NamedTextColor.YELLOW))
+                .setLore(
+                        LoreDesigner.createLore("Opens a new sign input which will allow you to rename the NPC's base Name", "Opens a new sign input which ", NamedTextColor.GRAY)
+                )
+                .buildItemStack();
+        setItemWithClickAction(4, item, this::editName);
+    }
+
     @Override
     public void update() {
 

+ 2 - 2
src/main/java/me/lethunderhawk/fluxapi/npc/registry/NPCRegistry.java

@@ -1,7 +1,7 @@
 package me.lethunderhawk.fluxapi.npc.registry;
 
 import me.lethunderhawk.fluxapi.FluxService;
-import me.lethunderhawk.fluxapi.npc.NPCModule;
+import me.lethunderhawk.fluxapi.npc.NPCAPIModule;
 import me.lethunderhawk.fluxapi.npc.abstraction.NPC;
 import org.bukkit.Bukkit;
 import org.bukkit.configuration.serialization.ConfigurationSerialization;
@@ -27,6 +27,6 @@ public final class NPCRegistry {
             Bukkit.getLogger().info("Class: " +clazz.getSimpleName() + " registered for deserialization");
         }
         registered = true;
-        FluxService.get(NPCModule.class).finalizeRegistration();
+        FluxService.get(NPCAPIModule.class).finalizeRegistration();
     }
 }

+ 13 - 0
src/main/java/me/lethunderhawk/fluxapi/profile/AnnotatedSerializable.java

@@ -0,0 +1,13 @@
+package me.lethunderhawk.fluxapi.profile;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface AnnotatedSerializable {
+    String prefix();
+
+}

+ 3 - 3
src/main/java/me/lethunderhawk/fluxapi/profile/ProfileModule.java → src/main/java/me/lethunderhawk/fluxapi/profile/ProfileAPIModule.java

@@ -3,15 +3,15 @@ package me.lethunderhawk.fluxapi.profile;
 import me.lethunderhawk.fluxapi.FluxService;
 import me.lethunderhawk.fluxapi.profile.listener.JoinListener;
 import me.lethunderhawk.fluxapi.util.config.ConfigLoader;
-import me.lethunderhawk.fluxapi.util.interfaces.BazaarFluxModule;
+import me.lethunderhawk.fluxapi.util.interfaces.FluxAPIModule;
 import org.bukkit.event.HandlerList;
 import org.bukkit.plugin.java.JavaPlugin;
 
-public class ProfileModule extends BazaarFluxModule {
+public class ProfileAPIModule extends FluxAPIModule {
     private JoinListener joinListener;
     private ProfileManager profileManager;
 
-    public ProfileModule(JavaPlugin plugin) {
+    public ProfileAPIModule(JavaPlugin plugin) {
         super(plugin);
     }
 

+ 1 - 1
src/main/java/me/lethunderhawk/fluxapi/util/MessageSender.java

@@ -5,7 +5,7 @@ import net.kyori.adventure.text.Component;
 import net.kyori.adventure.text.format.NamedTextColor;
 
 public class MessageSender {
-    public static void sendText(Audience receiver, Component message, String prefix) {
+    public static void sendModuleText(Audience receiver, Component message, String prefix) {
         receiver.sendMessage(Component.text(prefix + " ", NamedTextColor.GOLD)
                 .append(message));
     }

+ 9 - 6
src/main/java/me/lethunderhawk/fluxapi/util/command/CustomCommand.java

@@ -1,6 +1,6 @@
 package me.lethunderhawk.fluxapi.util.command;
 
-import me.lethunderhawk.fluxapi.util.interfaces.BazaarFluxModule;
+import me.lethunderhawk.fluxapi.util.interfaces.FluxAPIModule;
 import net.kyori.adventure.text.Component;
 import net.kyori.adventure.text.format.NamedTextColor;
 import org.bukkit.command.Command;
@@ -16,15 +16,18 @@ import java.util.stream.Collectors;
 
 public abstract class CustomCommand implements CommandExecutor, TabCompleter {
 
-    protected final CommandNode rootCommand;
-    protected final BazaarFluxModule module;
-    public CustomCommand(CommandNode rootCommand, BazaarFluxModule module) {
-        this.rootCommand = rootCommand;
+    protected CommandNode rootCommand;
+    protected final FluxAPIModule module;
+    public CustomCommand(FluxAPIModule module) {
         this.module = module;
         createHelpCommand();
         createCommands();
     }
 
+    public void setRootCommand(CommandNode rootCommand) {
+        this.rootCommand = rootCommand;
+    }
+
     private void createHelpCommand() {
         rootCommand.addSubCommand(new CommandNode("help", "Displays this help menu", this::sendHelp));
     }
@@ -79,7 +82,7 @@ public abstract class CustomCommand implements CommandExecutor, TabCompleter {
         targetNode.getExecutor().accept(sender, remainingArgs);
         return true;
     }
-    private void sendHelp(CommandSender sender, String[] strings) {
+    protected void sendHelp(CommandSender sender, String[] strings) {
         sendHelp(sender);
     }
     protected void sendHelp(CommandSender sender) {

+ 2 - 2
src/main/java/me/lethunderhawk/fluxapi/util/gui/ConfirmationMenu.java

@@ -16,10 +16,10 @@ public class ConfirmationMenu extends InventoryGUI {
     public ConfirmationMenu(String title, Consumer<Player> callback ) {
         super(title, 27);
         this.callback = callback;
-        setupItems();
     }
 
-    private void setupItems() {
+    @Override
+    public void buildContent() {
         fillGlassPaneBackground();
 
         ItemStack IAmSure = new ItemStack(Material.LIME_STAINED_GLASS_PANE);

+ 13 - 1
src/main/java/me/lethunderhawk/fluxapi/util/gui/InventoryGUI.java

@@ -34,6 +34,7 @@ public abstract class InventoryGUI implements InventoryHolder {
         this.title = title;
         this.size = size;
         this.inventory = Bukkit.createInventory(this, size, title);
+        buildContent();
     }
 
     public void handleClick(Player player, int slot, ClickType type) {
@@ -109,12 +110,22 @@ public abstract class InventoryGUI implements InventoryHolder {
      * Opens the previous GUI if available.
      */
     public void openPrevious(Player player) {
-        if (!previousGuis.isEmpty()) {
+        if (hasPreviousGUI()) {
             InventoryGUI previousGui = previousGuis.pop();
             previousGui.update();
             InventoryManager.openFor(player, previousGui);
         }
     }
+    public void setNextPageButton(int slot, InventoryGUI gui){
+        ItemStack item = new ItemStack(Material.ARROW);
+        ItemMeta meta = item.getItemMeta();
+        meta.displayName(Component.text("Next Page", NamedTextColor.GREEN));
+        item.setItemMeta(UnItalic.removeItalicFromMeta(meta));
+        setItemWithClickAction(slot, item, (p, type) -> {
+            openNext(p, gui);
+        });
+    }
+
     public void setBackButton(int slot){
         ItemStack item = new ItemStack(Material.ARROW);
         ItemMeta meta = item.getItemMeta();
@@ -144,6 +155,7 @@ public abstract class InventoryGUI implements InventoryHolder {
         return inventory;
     }
 
+    public abstract void buildContent();
     public abstract void update();
 
     public interface AutoCloseHandler {

+ 19 - 11
src/main/java/me/lethunderhawk/fluxapi/util/gui/PlayerHeadListGUI.java

@@ -13,6 +13,9 @@ import java.util.function.BiConsumer;
  */
 public class PlayerHeadListGUI extends InventoryGUI {
 
+    private final List<Player> players;
+    private final BiConsumer<Player, Player> onHeadClick;
+
     public PlayerHeadListGUI(
             String title,
             int size,
@@ -20,10 +23,25 @@ public class PlayerHeadListGUI extends InventoryGUI {
             BiConsumer<Player, Player> onHeadClick
     ) {
         super(title, size);
+        this.players = players;
+        this.onHeadClick = onHeadClick;
+    }
+
+
+    private ItemStack createPlayerHead(Player player) {
+        ItemStack head = new ItemStack(Material.PLAYER_HEAD);
+        SkullMeta meta = (SkullMeta) head.getItemMeta();
+        meta.setOwningPlayer(player);
+        meta.setDisplayName(player.getName());
+        head.setItemMeta(meta);
+        return head;
+    }
 
+    @Override
+    public void buildContent() {
         int slot = 0;
         for (Player target : players) {
-            if (slot >= size) break;
+            if (slot >= this.getInventory().getSize()) break;
 
             ItemStack head = createPlayerHead(target);
             int finalSlot = slot;
@@ -38,16 +56,6 @@ public class PlayerHeadListGUI extends InventoryGUI {
         fillBackground(Material.GRAY_STAINED_GLASS_PANE, " ");
     }
 
-
-    private ItemStack createPlayerHead(Player player) {
-        ItemStack head = new ItemStack(Material.PLAYER_HEAD);
-        SkullMeta meta = (SkullMeta) head.getItemMeta();
-        meta.setOwningPlayer(player);
-        meta.setDisplayName(player.getName());
-        head.setItemMeta(meta);
-        return head;
-    }
-
     @Override
     public void update() {
 

+ 4 - 3
src/main/java/me/lethunderhawk/fluxapi/util/interfaces/BazaarFluxModule.java → src/main/java/me/lethunderhawk/fluxapi/util/interfaces/FluxAPIModule.java

@@ -6,16 +6,17 @@ import net.kyori.adventure.text.Component;
 import org.bukkit.command.CommandSender;
 import org.bukkit.plugin.java.JavaPlugin;
 
-public abstract class BazaarFluxModule{
+public abstract class FluxAPIModule {
     protected JavaPlugin plugin;
-    public BazaarFluxModule(JavaPlugin plugin) {
+    public FluxAPIModule(JavaPlugin plugin) {
         this.plugin = plugin;
     }
     public abstract String getPrefix();
     public abstract void onEnable();
     public abstract void onDisable();
+
     public void sendText(Audience receiver, Component infoText){
-        MessageSender.sendText(receiver, infoText, getPrefix());
+        MessageSender.sendModuleText(receiver, infoText, getPrefix());
     }
 
     public void reload(CommandSender sender, String[] strings) {