123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package me.km.snuviscript;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import me.hammerle.snuviscript.code.Script;
- import me.hammerle.snuviscript.inputprovider.Variable;
- import me.km.permissions.Command;
- import me.km.utils.Utils;
- import net.minecraft.command.ICommandSource;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.entity.player.ServerPlayerEntity;
- import net.minecraft.server.MinecraftServer;
- public class CommandScript extends Command
- {
- private final Scripts scripts;
- private final MinecraftServer server;
-
- public CommandScript(Scripts scripts, MinecraftServer server)
- {
- this.scripts = scripts;
- this.server = server;
- }
- @Override
- public String getName()
- {
- return "script";
- }
-
- @Override
- public Iterable<String> getAliases()
- {
- ArrayList<String> list = new ArrayList<>();
- list.add("s");
- return list;
- }
- @SuppressWarnings("unchecked")
- @Override
- public void execute(ICommandSource cs, String[] arg)
- {
- if(arg.length >= 1)
- {
- switch(arg[0].toLowerCase())
- {
- case "s":
- case "start":
- {
- if(arg.length >= 2)
- {
- String[] pars = Arrays.copyOfRange(arg, 1, arg.length);
- scripts.startScript(pars);
- return;
- }
- break;
- }
- case "startp":
- {
- if(arg.length >= 3)
- {
- PlayerEntity p = Utils.getPlayerByName(server, arg[1]);
- if(p == null)
- {
- sendMessage(cs, String.format("Cannot find player '%s'", arg[1]));
- return;
- }
- String[] pars = Arrays.copyOfRange(arg, 2, arg.length);
- scripts.startPlayerScript(p, pars);
- return;
- }
- break;
- }
- case "v":
- case "variable":
- {
- if(arg.length >= 3)
- {
- try
- {
- Script sc = scripts.getScriptManager().getScript(Integer.parseInt(arg[1]));
- if(sc == null)
- {
- throw new NumberFormatException();
- }
- Variable var = sc.getVar(arg[2]);
- if(var != null)
- {
- sendMessage(cs, String.format("%s = %s", arg[2], var.get(sc)));
- }
- else
- {
- sendMessage(cs, String.format("%s = null", arg[2]));
- }
- }
- catch(NumberFormatException ex)
- {
- sendMessage(cs, String.format("'%s' is not a valid id", arg[1]));
- }
- return;
- }
- break;
- }
- case "see":
- {
- Collection<Script> scs = scripts.getScriptManager().getScripts();
- if(scs.isEmpty())
- {
- sendMessage(cs, "No scripts are active");
- return;
- }
- sendMessage(cs, "Active scripts:");
- scs.forEach(sc ->
- {
- sendMessage(cs, String.format(" - %s (%d)", sc.getName(), sc.getId()));
- scripts.getPlayerList(sc.getId()).forEach(uuid ->
- {
- ServerPlayerEntity p = server.getPlayerList().getPlayerByUUID(uuid);
- sendMessage(cs, String.format(" - %s", p == null ? "null" : p.getName().getFormattedText()));
- });
- });
- return;
- }
- case "term":
- {
- if(arg.length >= 2)
- {
- try
- {
- if(arg[1].equals("all"))
- {
- scripts.clearPlayerRegistry();
- if(scripts.getScriptManager().removeScriptsSafe())
- {
- sendMessage(cs, "All active scripts were terminated");
- }
- else
- {
- sendMessage(cs, "Iterating is not allowed currently");
- }
- return;
- }
- int id = Integer.parseInt(arg[1]);
- Script qd = scripts.getScriptManager().getScript(id);
- if(qd != null)
- {
- scripts.getScriptManager().removeScriptSafe(qd);
- sendMessage(cs, String.format("Script '%s' was terminated", qd.getName()));
- }
- else
- {
- sendMessage(cs, String.format("Script id '%d' is not valid", id));
- }
- }
- catch(NumberFormatException ex)
- {
- sendMessage(cs, String.format("'%s' is not a valid id", arg[1]));
- }
- catch(Exception ex)
- {
- sendMessage(cs, "Exception on script termination");
- ex.printStackTrace();
- }
- return;
- }
- break;
- }
- }
- }
-
- sendMessage(cs, "/script ...");
- sendMessage(cs, "start <scripts...>", "starts a script");
- sendMessage(cs, "startp <player> <scripts...>", "starts a player script");
- sendMessage(cs, "variable <id> <name>", "shows the value of a variable");
- sendMessage(cs, "see", "shows active scripts");
- sendMessage(cs, "term <id/all>", "terminates a script");
- }
- }
|