NPCCommand.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package me.lethunderhawk.fluxapi.npc.command;
  2. import me.lethunderhawk.fluxapi.FluxService;
  3. import me.lethunderhawk.fluxapi.npc.abstraction.DummyNPC;
  4. import me.lethunderhawk.fluxapi.npc.abstraction.NPC;
  5. import me.lethunderhawk.fluxapi.npc.abstraction.NPCOptions;
  6. import me.lethunderhawk.fluxapi.npc.gui.NPCOptionsGUI;
  7. import me.lethunderhawk.fluxapi.npc.manager.NPCManager;
  8. import me.lethunderhawk.fluxapi.npc.util.MojangAPI;
  9. import me.lethunderhawk.fluxapi.util.command.CommandNode;
  10. import me.lethunderhawk.fluxapi.util.command.CustomCommand;
  11. import me.lethunderhawk.fluxapi.util.gui.InventoryManager;
  12. import me.lethunderhawk.fluxapi.util.interfaces.FluxAPIModule;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.entity.Player;
  15. import java.util.List;
  16. import java.util.UUID;
  17. import java.util.stream.Collectors;
  18. public class NPCCommand extends CustomCommand {
  19. public NPCCommand(FluxAPIModule module) {
  20. super(module);
  21. setRootCommand(new CommandNode("npc", "Base npc command", this::sendHelp));
  22. }
  23. @Override
  24. public void createCommands() {
  25. CommandNode create = registerCommand("create", "Test creation command", this::createNPC);
  26. create.setTabCompleter(this::createCompleter);
  27. CommandNode delete = registerCommand("delete", "Test deletion command", this::deleteNPC);
  28. delete.setTabCompleter(this::npcDeletionCompleter);
  29. CommandNode options = registerCommand("options", "Test options command", this::showOptionsMenu);
  30. options.setTabCompleter(this::npcOptionsCompleter);
  31. registerCommand("reload", "Reload this module", this::reload);
  32. }
  33. private void reload(CommandSender sender, String[] strings) {
  34. if(!sender.hasPermission("npc.reload")) return;
  35. module.reload(sender, strings);
  36. }
  37. private List<String> npcDeletionCompleter(CommandSender sender, String[] args) {
  38. if(!sender.hasPermission("npc.delete")) return null;
  39. return npcUUIDCompleter(args);
  40. }
  41. private List<String> createCompleter(CommandSender sender, String[] args) {
  42. if(!sender.hasPermission("npc.create")) return null;
  43. return List.of("1. <name>", "2. <Player name for texture>");
  44. }
  45. private List<String> npcOptionsCompleter(CommandSender sender, String[] args) {
  46. if(!sender.hasPermission("npc.edit")) return null;
  47. return npcUUIDCompleter(args);
  48. }
  49. private List<String> npcUUIDCompleter(String[] args){
  50. String partial = args[1].toLowerCase();
  51. return FluxService.get(NPCManager.class).getRegisteredNPCsUUID().stream()
  52. .map(UUID::toString)
  53. .filter(uuid -> uuid.startsWith(partial))
  54. .collect(Collectors.toList());
  55. }
  56. private void showOptionsMenu(CommandSender sender, String[] args) {
  57. if(!sender.hasPermission("npc.options")) return;
  58. if(!(sender instanceof Player player)) return;
  59. NPC npc = FluxService.get(NPCManager.class).findByUUID(args[0]);
  60. if(npc != null) {
  61. InventoryManager.openFor(player, new NPCOptionsGUI(npc));
  62. }else{
  63. sender.sendMessage("That NPC doesn't exist!");
  64. }
  65. }
  66. private void createNPC(CommandSender sender, String[] strings) {
  67. if(!(sender instanceof Player p)) return;
  68. if(strings.length < 2) return;
  69. String name = strings[0];
  70. String textureName = strings[1];
  71. String[] stringdata = new String[]{"", ""};
  72. if(textureName != null){
  73. stringdata = MojangAPI.getSkinDataFromName(textureName);
  74. }
  75. NPCOptions options = new NPCOptions(UUID.randomUUID())
  76. .setName(name)
  77. .setBukkitLocation(p.getLocation())
  78. .setTexture(stringdata[0])
  79. .setSignature(stringdata[1])
  80. .setLookingAtNearest(true);
  81. NPC npc = new DummyNPC(options);
  82. NPCManager manager = FluxService.get(NPCManager.class);
  83. manager.registerNPC(npc);
  84. manager.spawnNpc(npc);
  85. }
  86. private void deleteNPC(CommandSender sender, String[] strings) {
  87. if(!(sender instanceof Player p)) return;
  88. if(strings.length == 0) return;
  89. FluxService.get(NPCManager.class).deleteNPCByUUIDString(strings[0]);
  90. }
  91. }