CommandScript.java 3.7 KB

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