From f48edc88eff92dcf6f10522a4f0ebb0c7324206a Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 3 Aug 2024 17:49:33 +0200 Subject: [PATCH] Only shows applicable positions on tab-completion --- .../command/SetNPCPositionCommand.java | 25 +++++++++----- .../property/NPCPosition.java | 33 ++++++++++++++++--- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/knarcraft/blacksmithvisuals/command/SetNPCPositionCommand.java b/src/main/java/net/knarcraft/blacksmithvisuals/command/SetNPCPositionCommand.java index 57c89c7..5aab070 100644 --- a/src/main/java/net/knarcraft/blacksmithvisuals/command/SetNPCPositionCommand.java +++ b/src/main/java/net/knarcraft/blacksmithvisuals/command/SetNPCPositionCommand.java @@ -2,6 +2,7 @@ package net.knarcraft.blacksmithvisuals.command; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.trait.Trait; import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.trait.ScrapperTrait; import net.knarcraft.blacksmithvisuals.BlacksmithVisuals; @@ -24,9 +25,6 @@ import java.util.List; */ public class SetNPCPositionCommand implements TabExecutor { - private static List npcPositionNames = null; - - @Override public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] arguments) { @@ -66,15 +64,26 @@ public class SetNPCPositionCommand implements TabExecutor { @Override public List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] arguments) { + NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender); + Class trait = null; + if (npc != null && npc.hasTrait(ScrapperTrait.class)) { + trait = ScrapperTrait.class; + } else if (npc != null && npc.hasTrait(BlacksmithTrait.class)) { + trait = BlacksmithTrait.class; + } + if (trait == null) { + return List.of(); + } + if (arguments.length == 1) { - if (npcPositionNames == null) { - List output = new ArrayList<>(); - for (NPCPosition position : NPCPosition.values()) { + List output = new ArrayList<>(); + for (NPCPosition position : NPCPosition.values()) { + if (position.isApplicable(trait) && position.getPositionName().contains( + arguments[0].toLowerCase().replace("_", "-"))) { output.add(position.getPositionName()); } - npcPositionNames = output; } - return npcPositionNames; + return output; } else { return List.of(); } diff --git a/src/main/java/net/knarcraft/blacksmithvisuals/property/NPCPosition.java b/src/main/java/net/knarcraft/blacksmithvisuals/property/NPCPosition.java index 0ccfaab..ed686d7 100644 --- a/src/main/java/net/knarcraft/blacksmithvisuals/property/NPCPosition.java +++ b/src/main/java/net/knarcraft/blacksmithvisuals/property/NPCPosition.java @@ -1,9 +1,14 @@ package net.knarcraft.blacksmithvisuals.property; +import net.citizensnpcs.api.trait.Trait; +import net.knarcraft.blacksmith.trait.BlacksmithTrait; +import net.knarcraft.blacksmith.trait.ScrapperTrait; import org.bukkit.Material; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; + /** * A definable NPC position */ @@ -12,28 +17,36 @@ public enum NPCPosition { /** * The position the NPC should be in while idle */ - IDLE("idle"), + IDLE("idle", List.of(BlacksmithTrait.class, ScrapperTrait.class)), /** * The position the NPC should be in while undoing netherite or armor trims */ - WORKING_NETHERITE("netherite-workstation"), + WORKING_NETHERITE("netherite-workstation", List.of(ScrapperTrait.class)), /** * The position the NPC should be in while reforging armor, weapons or tools */ - WORKING_REPAIRABLE("repairable-workstation"), + WORKING_REPAIRABLE("repairable-workstation", List.of(BlacksmithTrait.class, ScrapperTrait.class)), /** * The position the NPC should be in while un-crafting items */ - WORKING_CRAFTING("crafting-workstation"), + WORKING_CRAFTING("crafting-workstation", List.of(ScrapperTrait.class)), ; private final String positionName; + private final List> applicableTraits; - NPCPosition(@NotNull String positionName) { + /** + * Instantiates a new NPC position + * + * @param positionName

The user-facing name of the position

+ * @param applicableTraits

The traits the position can be applied to

+ */ + NPCPosition(@NotNull String positionName, @NotNull List> applicableTraits) { this.positionName = positionName; + this.applicableTraits = applicableTraits; } /** @@ -79,4 +92,14 @@ public enum NPCPosition { return null; } + /** + * Gets whether this position type is applicable to the given trait + * + * @param trait

The trait to check

+ * @return

True if this position is applicable

+ */ + public boolean isApplicable(@NotNull Class trait) { + return this.applicableTraits.contains(trait); + } + }