CommandScript.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.hammerle.snuviscript.inputprovider.Variable;
  7. import me.km.permissions.Command;
  8. import me.km.utils.Utils;
  9. import net.minecraft.command.ICommandSource;
  10. import net.minecraft.entity.player.PlayerEntity;
  11. import net.minecraft.entity.player.ServerPlayerEntity;
  12. import net.minecraft.server.MinecraftServer;
  13. public class CommandScript extends Command {
  14. private final Scripts scripts;
  15. private final MinecraftServer server;
  16. public CommandScript(Scripts scripts, MinecraftServer server) {
  17. this.scripts = scripts;
  18. this.server = server;
  19. }
  20. @Override
  21. public String getName() {
  22. return "script";
  23. }
  24. @Override
  25. public Iterable<String> getAliases() {
  26. ArrayList<String> list = new ArrayList<>();
  27. list.add("s");
  28. return list;
  29. }
  30. @SuppressWarnings("unchecked")
  31. @Override
  32. public void execute(ICommandSource cs, String[] arg) {
  33. if(arg.length >= 1) {
  34. switch(arg[0].toLowerCase()) {
  35. case "s":
  36. case "start": {
  37. if(arg.length >= 2) {
  38. String[] pars = Arrays.copyOfRange(arg, 1, arg.length);
  39. scripts.startScript(null, pars);
  40. return;
  41. }
  42. break;
  43. }
  44. case "startp": {
  45. if(arg.length >= 3) {
  46. PlayerEntity p = Utils.getPlayerByName(server, arg[1]);
  47. if(p == null) {
  48. sendMessage(cs, String.format("Cannot find player '%s'.", arg[1]));
  49. return;
  50. }
  51. String[] pars = Arrays.copyOfRange(arg, 2, arg.length);
  52. scripts.startPlayerScript(p, pars);
  53. return;
  54. }
  55. break;
  56. }
  57. case "v":
  58. case "variable": {
  59. if(arg.length >= 3) {
  60. try {
  61. Script sc = scripts.getScriptManager().getScript(Integer.parseInt(arg[1]));
  62. if(sc == null) {
  63. throw new NumberFormatException();
  64. }
  65. Variable var = sc.getVar(arg[2]);
  66. if(var != null) {
  67. sendMessage(cs, String.format("%s = %s", arg[2], var.get(sc)));
  68. } else {
  69. sendMessage(cs, String.format("%s = null", arg[2]));
  70. }
  71. } catch(NumberFormatException ex) {
  72. sendMessage(cs, String.format("'%s' is not a valid id.", arg[1]));
  73. }
  74. return;
  75. }
  76. break;
  77. }
  78. case "see": {
  79. Collection<Script> scs = scripts.getScriptManager().getScripts();
  80. if(scs.isEmpty()) {
  81. sendMessage(cs, "No scripts are active.");
  82. return;
  83. }
  84. sendMessage(cs, "Active scripts:");
  85. scs.forEach(sc -> {
  86. String id = String.valueOf(sc.getId());
  87. sendListMessage(cs, id, sc.getName());
  88. scripts.getPlayerList(sc.getId()).forEach(uuid -> {
  89. ServerPlayerEntity p = server.getPlayerList().getPlayerByUUID(uuid);
  90. sendListMessage(cs, id, p == null ? "null" : p.getName().getFormattedText());
  91. });
  92. });
  93. return;
  94. }
  95. case "term": {
  96. if(arg.length >= 2) {
  97. try {
  98. if(arg[1].equals("all")) {
  99. scripts.clearPlayerRegistry();
  100. scripts.getScriptManager().removeScripts();
  101. sendMessage(cs, "All active scripts were terminated.");
  102. return;
  103. }
  104. int id = Integer.parseInt(arg[1]);
  105. Script sc = scripts.getScriptManager().getScript(id);
  106. if(sc != null) {
  107. scripts.getScriptManager().removeScript(sc);
  108. sendMessage(cs, String.format("Script '%s' was terminated.", sc.getName()));
  109. } else {
  110. sendMessage(cs, String.format("Script id '%d' is not valid.", id));
  111. }
  112. } catch(NumberFormatException ex) {
  113. sendMessage(cs, String.format("'%s' is not a valid id.", arg[1]));
  114. } catch(Exception ex) {
  115. sendMessage(cs, "An exception on script termination was thrown.");
  116. ex.printStackTrace();
  117. }
  118. return;
  119. }
  120. break;
  121. }
  122. }
  123. }
  124. sendMessage(cs, "/script ...");
  125. sendListMessage(cs, "start <scripts...>", "starts a script");
  126. sendListMessage(cs, "startp <player> <scripts...>", "starts a player script");
  127. sendListMessage(cs, "variable <id> <name>", "shows the value of a variable");
  128. sendListMessage(cs, "see", "shows active scripts");
  129. sendListMessage(cs, "term <id/all>", "terminates a script");
  130. }
  131. }