PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/net/downwithdestruction/dwdnpc/CmdNPC.java

https://bitbucket.org/dwdgamingdev/dwdnpc
Java | 236 lines | 225 code | 10 blank | 1 comment | 57 complexity | 9df5afeff3787d1693458129e43f8cc0 MD5 | raw file
  1. package net.downwithdestruction.dwdnpc;
  2. import net.downwithdestruction.dwdnpc.npclib.entity.HumanNPC;
  3. import net.downwithdestruction.dwdnpc.npclib.entity.NPC;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.Location;
  6. import org.bukkit.command.Command;
  7. import org.bukkit.command.CommandExecutor;
  8. import org.bukkit.command.CommandSender;
  9. import org.bukkit.command.ConsoleCommandSender;
  10. import org.bukkit.command.RemoteConsoleCommandSender;
  11. import org.bukkit.entity.Player;
  12. public class CmdNPC implements CommandExecutor {
  13. private DwDNPC plugin;
  14. public CmdNPC(DwDNPC plugin) {
  15. this.plugin = plugin;
  16. }
  17. public boolean isAuthorized(final CommandSender player, final String node) {
  18. return player instanceof RemoteConsoleCommandSender ||
  19. player instanceof ConsoleCommandSender ||
  20. player.hasPermission("dwdnpc.op") ||
  21. player.hasPermission(node);
  22. }
  23. public boolean dispNoPerms(CommandSender sender) {
  24. sender.sendMessage(ChatColor.RED + "You are not permitted to use this command.");
  25. return true;
  26. }
  27. public boolean dispPlayerOnly(CommandSender sender) {
  28. sender.sendMessage(ChatColor.RED + "This command is only available to players!");
  29. return true;
  30. }
  31. public boolean dispForceDelete(CommandSender sender) {
  32. sender.sendMessage(ChatColor.RED + "Multiple NPC's found with that name!");
  33. sender.sendMessage(ChatColor.RED + "Please use the 'deleteid' function to specify which NPC to delete.");
  34. sender.sendMessage(ChatColor.RED + "Or use the '-force' flag to force deletion of ALL NPCs with this name.");
  35. return true;
  36. }
  37. private boolean showusage(CommandSender sender) {
  38. sender.sendMessage(ChatColor.RED + "Function not found! Valid functions: " +
  39. ChatColor.GRAY + "create" + ChatColor.RED + ", " +
  40. ChatColor.GRAY + "delete" + ChatColor.RED + ", " +
  41. ChatColor.GRAY + "deleteid" + ChatColor.RED + ", " +
  42. ChatColor.GRAY + "radius" + ChatColor.RED + ", " +
  43. ChatColor.GRAY + "reload" + ChatColor.RED + "."
  44. );
  45. return false;
  46. }
  47. @Override
  48. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  49. if(cmd.getName().equalsIgnoreCase("dwdnpc")) {
  50. if (args.length < 1) return showusage(sender);
  51. if (args[0].equalsIgnoreCase("create")) {
  52. if (!isAuthorized(sender, "dwdnpc.create")) return dispNoPerms(sender);
  53. if (!(sender instanceof Player)) dispPlayerOnly(sender);
  54. if (args.length < 2) {
  55. sender.sendMessage(ChatColor.RED + "You did not specify a name for this NPC!");
  56. sender.sendMessage(ChatColor.GRAY + "/dwdnpc create [name]");
  57. return true;
  58. }
  59. final Location loc = ((Player) sender).getLocation();
  60. final String name = (args[1].length() > 16) ? args[1].substring(0, 16) : args[1];
  61. final Integer id = plugin.npcManager.getID(plugin.npcManager.spawnHumanNPC(name, loc));
  62. if (id == null) {
  63. sender.sendMessage(ChatColor.RED + "Failed to create NPC");
  64. return true;
  65. }
  66. plugin.getConfig().set("npcs." + id + ".name", name);
  67. plugin.getConfig().set("npcs." + id + ".world", loc.getWorld().getName());
  68. plugin.getConfig().set("npcs." + id + ".x", loc.getX());
  69. plugin.getConfig().set("npcs." + id + ".y", loc.getY());
  70. plugin.getConfig().set("npcs." + id + ".z", loc.getZ());
  71. plugin.saveConfig();
  72. // Reload NPCs (quick fix to remove console spam)
  73. plugin.reloadConfig();
  74. plugin.loadConfig();
  75. plugin.npcManager.despawnAll();
  76. plugin.npcManager.loadNPCs();
  77. plugin.stopDelayedTask();
  78. plugin.startDelayedTask();
  79. sender.sendMessage(ChatColor.GREEN + "NPC has been created!");
  80. if(plugin.debug) plugin.log(ChatColor.GREEN + "Created NPC with ID: " + id);
  81. return true;
  82. } else if (args[0].equalsIgnoreCase("delete")) {
  83. if (!isAuthorized(sender, "dwdnpc.delete")) return dispNoPerms(sender);
  84. if (args.length < 2) {
  85. sender.sendMessage(ChatColor.RED + "You did not specify a name of an NPC to delete!");
  86. sender.sendMessage(ChatColor.GRAY + "/dwdnpc delete [name] (-force)");
  87. return true;
  88. }
  89. final String name = (args[1].length() > 16) ? args[1].substring(0, 16) : args[1];
  90. if (plugin.npcManager.getNPCCountByName(name) < 1) {
  91. sender.sendMessage(ChatColor.RED + "No NPC found by that name!");
  92. return true;
  93. }
  94. final Integer count = plugin.npcManager.getNPCCountByName(name);
  95. if (count > 1) {
  96. if (args.length > 2) {
  97. if (!args[2].equalsIgnoreCase("-force")) return dispForceDelete(sender);
  98. } else return dispForceDelete(sender);
  99. }
  100. for (NPC npc : plugin.npcManager.getNPCs()) {
  101. if (((HumanNPC) npc).getName().equalsIgnoreCase(name)) {
  102. int id = plugin.npcManager.getID(npc);
  103. plugin.getConfig().set("npcs." + id, null);
  104. plugin.saveConfig();
  105. plugin.npcManager.despawnHuman(id);
  106. if(plugin.debug) plugin.log(ChatColor.GREEN + "Deleted NPC with ID: " + id);
  107. }
  108. }
  109. sender.sendMessage(ChatColor.GREEN + "NPC" + ((count > 1) ? "s" : "") + " has been deleted.");
  110. return true;
  111. } else if (args[0].equalsIgnoreCase("deleteid")) {
  112. if (!isAuthorized(sender, "dwdnpc.delete")) return dispNoPerms(sender);
  113. if (args.length < 2) {
  114. sender.sendMessage(ChatColor.RED + "You did not specify an ID of an NPC to delete!");
  115. sender.sendMessage(ChatColor.GRAY + "/dwdnpc deleteid [id]");
  116. return true;
  117. }
  118. Integer id;
  119. try {
  120. id = Integer.valueOf(args[1]);
  121. } catch (NumberFormatException npe) {
  122. sender.sendMessage(ChatColor.RED + "ID must be a number!");
  123. return true;
  124. }
  125. final NPC npc = plugin.npcManager.getNPCByID(id);
  126. if (npc == null) {
  127. sender.sendMessage(ChatColor.RED + "NPC not found by that ID!");
  128. return true;
  129. }
  130. plugin.getConfig().set("npcs." + id, null);
  131. plugin.saveConfig();
  132. plugin.npcManager.despawnHuman(id);
  133. if(plugin.debug) plugin.log(ChatColor.GREEN + "Deleted NPC with ID: " + id);
  134. sender.sendMessage(ChatColor.GREEN + "NPC has been deleted.");
  135. return true;
  136. } else if (args[0].equalsIgnoreCase("radius")) {
  137. if (!isAuthorized(sender, "dwdnpc.admin")) return dispNoPerms(sender);
  138. if (args.length < 2) {
  139. sender.sendMessage(ChatColor.RED + "You did not specify an aware radius!");
  140. sender.sendMessage(ChatColor.GRAY + "/dwdnpc radius [radius]");
  141. return true;
  142. }
  143. Double radius;
  144. try {
  145. radius = Double.valueOf(args[1]);
  146. } catch(NumberFormatException e) {
  147. sender.sendMessage(ChatColor.RED + "Please enter a valid number!");
  148. return true;
  149. }
  150. if (radius < 1) {
  151. sender.sendMessage(ChatColor.RED + "Radius must be at least 1!");
  152. return true;
  153. } else if (radius > 50) {
  154. sender.sendMessage(ChatColor.RED + "To prevent lag, values of higher than 50 are not allowed!");
  155. return true;
  156. }
  157. if (args.length > 2) {
  158. if (args[2].equalsIgnoreCase("-id")) {
  159. if (args.length < 3) {
  160. sender.sendMessage(ChatColor.RED + "Missing ID after the -id flag!");
  161. return true;
  162. }
  163. Integer id;
  164. try {
  165. id = Integer.valueOf(args[3]);
  166. } catch (NumberFormatException nfe) {
  167. sender.sendMessage(ChatColor.RED + "The id given is not a valid number!");
  168. return true;
  169. }
  170. final NPC npc = plugin.npcManager.getNPCByID(id);
  171. if (npc == null) {
  172. sender.sendMessage(ChatColor.RED + "NPC not found by that ID!");
  173. return true;
  174. }
  175. plugin.getConfig().set("npcs." + id + ".radius", radius);
  176. plugin.saveConfig();
  177. sender.sendMessage(ChatColor.GREEN + "Radius saved. NPC with ID " + id + " now has aware radius of " + ChatColor.GRAY + radius);
  178. if(plugin.debug) plugin.log(ChatColor.GREEN + "Aware radius for NPC " + id + " is now set to: " + radius);
  179. return true;
  180. } else if (args[2].equalsIgnoreCase("-name")) {
  181. if (args.length < 3) {
  182. sender.sendMessage(ChatColor.RED + "Missing name after the -name flag!");
  183. return true;
  184. }
  185. final String name = args[3];
  186. final NPC npc = plugin.npcManager.getNPCByName(name);
  187. if (npc == null) {
  188. sender.sendMessage(ChatColor.RED + "NPC not found by that name!");
  189. return true;
  190. }
  191. if (plugin.npcManager.getNPCCountByName(name) > 1) {
  192. sender.sendMessage(ChatColor.RED + "Multiple NPCs found by that name! Please use the -id flag instead.");
  193. return true;
  194. }
  195. plugin.getConfig().set("npcs." + plugin.npcManager.getID(npc) + ".radius", radius);
  196. plugin.saveConfig();
  197. sender.sendMessage(ChatColor.GREEN + "Radius saved. NPC with name '" + name + "' now has aware radius of " + ChatColor.GRAY + radius);
  198. if(plugin.debug) plugin.log(ChatColor.GREEN + "Aware radius for NPC '" + name + "' is now set to: " + radius);
  199. return true;
  200. }
  201. }
  202. plugin.getConfig().set("look-at-radius", radius);
  203. plugin.saveConfig();
  204. plugin.lookAtRadius = radius;
  205. sender.sendMessage(ChatColor.GREEN + "Radius saved. All NPCs aware radius is now " + ChatColor.GRAY + radius);
  206. if(plugin.debug) plugin.log(ChatColor.GREEN + "Aware radius is now set to: " + radius);
  207. return true;
  208. } else if (args[0].equalsIgnoreCase("reload")) {
  209. if (!isAuthorized(sender, "dwdnpc.admin")) return dispNoPerms(sender);
  210. if(plugin.debug) plugin.log(ChatColor.GREEN + "Reloading config.yml");
  211. plugin.reloadConfig();
  212. plugin.loadConfig();
  213. if(plugin.debug) plugin.log(ChatColor.GREEN + "Removing all loaded NPCs.");
  214. plugin.npcManager.despawnAll();
  215. if(plugin.debug) plugin.log(ChatColor.GREEN + "Loading back NPCs from config.");
  216. plugin.npcManager.loadNPCs();
  217. if(plugin.debug) plugin.log(ChatColor.GREEN + "Restarting LookAtTask");
  218. plugin.stopDelayedTask();
  219. plugin.startDelayedTask();
  220. sender.sendMessage(ChatColor.GREEN + "All NPCs have been reloaded from config!");
  221. return true;
  222. } else return showusage(sender);
  223. }
  224. return false;
  225. }
  226. }