CommandScript.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package me.km.snuviscript;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5. import me.hammerle.snuviscript.code.Script;
  6. import me.km.permissions.Command;
  7. import net.minecraft.command.ICommandSource;
  8. import net.minecraft.server.MinecraftServer;
  9. public class CommandScript extends Command {
  10. private final Scripts scripts;
  11. private final MinecraftServer server;
  12. public CommandScript(Scripts scripts, MinecraftServer server) {
  13. this.scripts = scripts;
  14. this.server = server;
  15. }
  16. @Override
  17. public String getName() {
  18. return "script";
  19. }
  20. @Override
  21. public Iterable<String> getAliases() {
  22. ArrayList<String> list = new ArrayList<>();
  23. list.add("s");
  24. return list;
  25. }
  26. @SuppressWarnings("unchecked")
  27. @Override
  28. public void execute(ICommandSource cs, String[] arg) {
  29. if(arg.length >= 1) {
  30. switch(arg[0].toLowerCase()) {
  31. case "s":
  32. case "start": {
  33. if(arg.length >= 2) {
  34. String[] pars = Arrays.copyOfRange(arg, 1, arg.length);
  35. scripts.startScript(null, pars);
  36. return;
  37. }
  38. break;
  39. }
  40. case "see": {
  41. Collection<Script> scs = scripts.getScriptManager().getScripts();
  42. if(scs.isEmpty()) {
  43. sendMessage(cs, "No scripts are active.");
  44. return;
  45. }
  46. sendMessage(cs, "Active scripts:");
  47. scs.forEach(sc -> sendListMessage(cs, String.valueOf(sc.getId()), sc.getName()));
  48. return;
  49. }
  50. case "t":
  51. case "term": {
  52. if(arg.length >= 2) {
  53. try {
  54. if(arg[1].equals("all")) {
  55. scripts.getScriptManager().removeScripts();
  56. sendMessage(cs, "All active scripts were terminated.");
  57. return;
  58. }
  59. int id = Integer.parseInt(arg[1]);
  60. Script sc = scripts.getScriptManager().getScript(id);
  61. if(sc != null) {
  62. scripts.getScriptManager().removeScript(sc);
  63. sendMessage(cs, String.format("Script '%s' was terminated.", sc.getName()));
  64. } else {
  65. sendMessage(cs, String.format("Script id '%d' is not valid.", id));
  66. }
  67. } catch(NumberFormatException ex) {
  68. sendMessage(cs, String.format("'%s' is not a valid id.", arg[1]));
  69. } catch(Exception ex) {
  70. sendMessage(cs, "An exception on script termination was thrown.");
  71. ex.printStackTrace();
  72. }
  73. return;
  74. }
  75. break;
  76. }
  77. }
  78. }
  79. sendMessage(cs, "/script ...");
  80. sendListMessage(cs, "start <scripts...>", "starts a script");
  81. sendListMessage(cs, "see", "shows active scripts");
  82. sendListMessage(cs, "term <id/all>", "terminates a script");
  83. }
  84. }