CommandScript.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. {
  15. private final Scripts scripts;
  16. private final MinecraftServer server;
  17. public CommandScript(Scripts scripts, MinecraftServer server)
  18. {
  19. this.scripts = scripts;
  20. this.server = server;
  21. }
  22. @Override
  23. public String getName()
  24. {
  25. return "script";
  26. }
  27. @Override
  28. public Iterable<String> getAliases()
  29. {
  30. ArrayList<String> list = new ArrayList<>();
  31. list.add("s");
  32. return list;
  33. }
  34. @SuppressWarnings("unchecked")
  35. @Override
  36. public void execute(ICommandSource cs, String[] arg)
  37. {
  38. if(arg.length >= 1)
  39. {
  40. switch(arg[0].toLowerCase())
  41. {
  42. case "s":
  43. case "start":
  44. {
  45. if(arg.length >= 2)
  46. {
  47. String[] pars = Arrays.copyOfRange(arg, 1, arg.length);
  48. scripts.startScript(pars);
  49. return;
  50. }
  51. break;
  52. }
  53. case "startp":
  54. {
  55. if(arg.length >= 3)
  56. {
  57. PlayerEntity p = Utils.getPlayerByName(server, arg[1]);
  58. if(p == null)
  59. {
  60. sendMessage(cs, String.format("Cannot find player '%s'", arg[1]));
  61. return;
  62. }
  63. String[] pars = Arrays.copyOfRange(arg, 2, arg.length);
  64. scripts.startPlayerScript(p, pars);
  65. return;
  66. }
  67. break;
  68. }
  69. case "v":
  70. case "variable":
  71. {
  72. if(arg.length >= 3)
  73. {
  74. try
  75. {
  76. Script sc = scripts.getScriptManager().getScript(Integer.parseInt(arg[1]));
  77. if(sc == null)
  78. {
  79. throw new NumberFormatException();
  80. }
  81. Variable var = sc.getVar(arg[2]);
  82. if(var != null)
  83. {
  84. sendMessage(cs, String.format("%s = %s", arg[2], var.get(sc)));
  85. }
  86. else
  87. {
  88. sendMessage(cs, String.format("%s = null", arg[2]));
  89. }
  90. }
  91. catch(NumberFormatException ex)
  92. {
  93. sendMessage(cs, String.format("'%s' is not a valid id", arg[1]));
  94. }
  95. return;
  96. }
  97. break;
  98. }
  99. case "see":
  100. {
  101. Collection<Script> scs = scripts.getScriptManager().getScripts();
  102. if(scs.isEmpty())
  103. {
  104. sendMessage(cs, "No scripts are active");
  105. return;
  106. }
  107. sendMessage(cs, "Active scripts:");
  108. scs.forEach(sc ->
  109. {
  110. sendMessage(cs, String.format(" - %s (%d)", sc.getName(), sc.getId()));
  111. scripts.getPlayerList(sc.getId()).forEach(uuid ->
  112. {
  113. ServerPlayerEntity p = server.getPlayerList().getPlayerByUUID(uuid);
  114. sendMessage(cs, String.format(" - %s", p == null ? "null" : p.getName().getFormattedText()));
  115. });
  116. });
  117. return;
  118. }
  119. case "term":
  120. {
  121. if(arg.length >= 2)
  122. {
  123. try
  124. {
  125. if(arg[1].equals("all"))
  126. {
  127. scripts.clearPlayerRegistry();
  128. if(scripts.getScriptManager().removeScriptsSafe())
  129. {
  130. sendMessage(cs, "All active scripts were terminated");
  131. }
  132. else
  133. {
  134. sendMessage(cs, "Iterating is not allowed currently");
  135. }
  136. return;
  137. }
  138. int id = Integer.parseInt(arg[1]);
  139. Script qd = scripts.getScriptManager().getScript(id);
  140. if(qd != null)
  141. {
  142. scripts.getScriptManager().removeScriptSafe(qd);
  143. sendMessage(cs, String.format("Script '%s' was terminated", qd.getName()));
  144. }
  145. else
  146. {
  147. sendMessage(cs, String.format("Script id '%d' is not valid", id));
  148. }
  149. }
  150. catch(NumberFormatException ex)
  151. {
  152. sendMessage(cs, String.format("'%s' is not a valid id", arg[1]));
  153. }
  154. catch(Exception ex)
  155. {
  156. sendMessage(cs, "Exception on script termination");
  157. ex.printStackTrace();
  158. }
  159. return;
  160. }
  161. break;
  162. }
  163. }
  164. }
  165. sendMessage(cs, "/script ...");
  166. sendMessage(cs, "start <scripts...>", "starts a script");
  167. sendMessage(cs, "startp <player> <scripts...>", "starts a player script");
  168. sendMessage(cs, "variable <id> <name>", "shows the value of a variable");
  169. sendMessage(cs, "see", "shows active scripts");
  170. sendMessage(cs, "term <id/all>", "terminates a script");
  171. }
  172. }