CommandScript.java 3.3 KB

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