Only shows applicable positions on tab-completion
All checks were successful
KnarCraft/BlacksmithVisuals/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-08-03 17:49:33 +02:00
parent 87fa2f7c64
commit f48edc88ef
2 changed files with 45 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package net.knarcraft.blacksmithvisuals.command;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.trait.ScrapperTrait; import net.knarcraft.blacksmith.trait.ScrapperTrait;
import net.knarcraft.blacksmithvisuals.BlacksmithVisuals; import net.knarcraft.blacksmithvisuals.BlacksmithVisuals;
@ -24,9 +25,6 @@ import java.util.List;
*/ */
public class SetNPCPositionCommand implements TabExecutor { public class SetNPCPositionCommand implements TabExecutor {
private static List<String> npcPositionNames = null;
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
@ -66,15 +64,26 @@ public class SetNPCPositionCommand implements TabExecutor {
@Override @Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender);
Class<? extends Trait> 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 (arguments.length == 1) {
if (npcPositionNames == null) {
List<String> output = new ArrayList<>(); List<String> output = new ArrayList<>();
for (NPCPosition position : NPCPosition.values()) { for (NPCPosition position : NPCPosition.values()) {
if (position.isApplicable(trait) && position.getPositionName().contains(
arguments[0].toLowerCase().replace("_", "-"))) {
output.add(position.getPositionName()); output.add(position.getPositionName());
} }
npcPositionNames = output;
} }
return npcPositionNames; return output;
} else { } else {
return List.of(); return List.of();
} }

View File

@ -1,9 +1,14 @@
package net.knarcraft.blacksmithvisuals.property; 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.bukkit.Material;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List;
/** /**
* A definable NPC position * A definable NPC position
*/ */
@ -12,28 +17,36 @@ public enum NPCPosition {
/** /**
* The position the NPC should be in while idle * 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 * 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 * 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 * 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 String positionName;
private final List<Class<? extends Trait>> applicableTraits;
NPCPosition(@NotNull String positionName) { /**
* Instantiates a new NPC position
*
* @param positionName <p>The user-facing name of the position</p>
* @param applicableTraits <p>The traits the position can be applied to</p>
*/
NPCPosition(@NotNull String positionName, @NotNull List<Class<? extends Trait>> applicableTraits) {
this.positionName = positionName; this.positionName = positionName;
this.applicableTraits = applicableTraits;
} }
/** /**
@ -79,4 +92,14 @@ public enum NPCPosition {
return null; return null;
} }
/**
* Gets whether this position type is applicable to the given trait
*
* @param trait <p>The trait to check</p>
* @return <p>True if this position is applicable</p>
*/
public boolean isApplicable(@NotNull Class<? extends Trait> trait) {
return this.applicableTraits.contains(trait);
}
} }