| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package me.lethunderhawk.fluxapi.npc.command;
- import me.lethunderhawk.fluxapi.FluxService;
- import me.lethunderhawk.fluxapi.npc.abstraction.DummyNPC;
- import me.lethunderhawk.fluxapi.npc.abstraction.NPC;
- import me.lethunderhawk.fluxapi.npc.abstraction.NPCOptions;
- import me.lethunderhawk.fluxapi.npc.gui.NPCOptionsGUI;
- import me.lethunderhawk.fluxapi.npc.manager.NPCManager;
- 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.FluxAPIModule;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import java.util.List;
- import java.util.UUID;
- import java.util.stream.Collectors;
- public class NPCCommand extends CustomCommand {
- public NPCCommand(FluxAPIModule module) {
- super(module);
- setRootCommand(new CommandNode("npc", "Base npc command", this::sendHelp));
- }
- @Override
- public void createCommands() {
- CommandNode create = registerCommand("create", "Test creation command", this::createNPC);
- create.setTabCompleter(this::createCompleter);
- CommandNode delete = registerCommand("delete", "Test deletion command", this::deleteNPC);
- delete.setTabCompleter(this::npcDeletionCompleter);
- CommandNode options = registerCommand("options", "Test options command", this::showOptionsMenu);
- options.setTabCompleter(this::npcOptionsCompleter);
- registerCommand("reload", "Reload this module", this::reload);
- }
- private void reload(CommandSender sender, String[] strings) {
- if(!sender.hasPermission("npc.reload")) return;
- module.reload(sender, strings);
- }
- private List<String> npcDeletionCompleter(CommandSender sender, String[] args) {
- if(!sender.hasPermission("npc.delete")) return null;
- return npcUUIDCompleter(args);
- }
- private List<String> createCompleter(CommandSender sender, String[] args) {
- if(!sender.hasPermission("npc.create")) return null;
- return List.of("1. <name>", "2. <Player name for texture>");
- }
- private List<String> npcOptionsCompleter(CommandSender sender, String[] args) {
- if(!sender.hasPermission("npc.edit")) return null;
- return npcUUIDCompleter(args);
- }
- private List<String> npcUUIDCompleter(String[] args){
- String partial = args[1].toLowerCase();
- return FluxService.get(NPCManager.class).getRegisteredNPCsUUID().stream()
- .map(UUID::toString)
- .filter(uuid -> uuid.startsWith(partial))
- .collect(Collectors.toList());
- }
- private void showOptionsMenu(CommandSender sender, String[] args) {
- if(!sender.hasPermission("npc.options")) return;
- if(!(sender instanceof Player player)) return;
- NPC npc = FluxService.get(NPCManager.class).findByUUID(args[0]);
- if(npc != null) {
- InventoryManager.openFor(player, new NPCOptionsGUI(npc));
- }else{
- sender.sendMessage("That NPC doesn't exist!");
- }
- }
- private void createNPC(CommandSender sender, String[] strings) {
- if(!(sender instanceof Player p)) return;
- if(strings.length < 2) return;
- String name = strings[0];
- String textureName = strings[1];
- String[] stringdata = new String[]{"", ""};
- if(textureName != null){
- stringdata = MojangAPI.getSkinDataFromName(textureName);
- }
- NPCOptions options = new NPCOptions(UUID.randomUUID())
- .setName(name)
- .setBukkitLocation(p.getLocation())
- .setTexture(stringdata[0])
- .setSignature(stringdata[1])
- .setLookingAtNearest(true);
- NPC npc = new DummyNPC(options);
- NPCManager manager = FluxService.get(NPCManager.class);
- manager.registerNPC(npc);
- manager.spawnNpc(npc);
- }
- private void deleteNPC(CommandSender sender, String[] strings) {
- if(!(sender instanceof Player p)) return;
- if(strings.length == 0) return;
- FluxService.get(NPCManager.class).deleteNPCByUUIDString(strings[0]);
- }
- }
|