93 lines
3.5 KiB
Java
93 lines
3.5 KiB
Java
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;
|
|
import net.knarcraft.blacksmithvisuals.container.NPCData;
|
|
import net.knarcraft.blacksmithvisuals.manager.NPCDataManager;
|
|
import net.knarcraft.blacksmithvisuals.property.NPCPosition;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.command.TabExecutor;
|
|
import org.bukkit.entity.Player;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* The commands for setting an NPC's positions
|
|
*/
|
|
public class SetNPCPositionCommand implements TabExecutor {
|
|
|
|
@Override
|
|
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
|
@NotNull String[] arguments) {
|
|
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender);
|
|
if (npc == null || (!npc.hasTrait(BlacksmithTrait.class) && !npc.hasTrait(ScrapperTrait.class))) {
|
|
commandSender.sendMessage("You must select a scrapper or blacksmith NPC before executing this command");
|
|
return true;
|
|
}
|
|
|
|
if (!(commandSender instanceof Player player)) {
|
|
commandSender.sendMessage("This command must be executed by a player");
|
|
return false;
|
|
}
|
|
|
|
if (arguments.length < 1) {
|
|
return false;
|
|
}
|
|
|
|
NPCPosition position = NPCPosition.fromInput(arguments[0]);
|
|
if (position == null) {
|
|
commandSender.sendMessage("Invalid position specified!");
|
|
return false;
|
|
}
|
|
|
|
NPCDataManager manager = BlacksmithVisuals.getInstance().getNpcDataManager();
|
|
NPCData data = manager.getData(npc.getUniqueId());
|
|
if (data == null) {
|
|
data = new NPCData(new HashMap<>());
|
|
}
|
|
data.positions().put(position, player.getLocation());
|
|
manager.putData(npc.getUniqueId(), data);
|
|
commandSender.sendMessage("Position " + position.getPositionName() + " set!");
|
|
return true;
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
|
@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) {
|
|
List<String> output = new ArrayList<>();
|
|
for (NPCPosition position : NPCPosition.values()) {
|
|
if (position.isApplicable(trait) && position.getPositionName().contains(
|
|
arguments[0].toLowerCase().replace("_", "-"))) {
|
|
output.add(position.getPositionName());
|
|
}
|
|
}
|
|
return output;
|
|
} else {
|
|
return List.of();
|
|
}
|
|
}
|
|
|
|
}
|