From 39e5f1af4b7f3f1a8c005c75de75616fdaa64158 Mon Sep 17 00:00:00 2001 From: mcmonkey4eva Date: Thu, 24 Nov 2016 08:05:15 -0800 Subject: [PATCH] in-game command scripts! --- .../denizen2sponge/Denizen2Sponge.java | 3 + .../Denizen2SpongeImplementation.java | 8 +- .../spongecommands/ExCommand.java | 25 --- .../spongescripts/GameCommandScript.java | 149 ++++++++++++++++++ 4 files changed, 159 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/denizenscript/denizen2sponge/spongescripts/GameCommandScript.java diff --git a/src/main/java/com/denizenscript/denizen2sponge/Denizen2Sponge.java b/src/main/java/com/denizenscript/denizen2sponge/Denizen2Sponge.java index ceaaf53..cb4c95c 100644 --- a/src/main/java/com/denizenscript/denizen2sponge/Denizen2Sponge.java +++ b/src/main/java/com/denizenscript/denizen2sponge/Denizen2Sponge.java @@ -29,6 +29,7 @@ import com.denizenscript.denizen2sponge.spongecommands.ExCommand; import com.denizenscript.denizen2sponge.spongeevents.Denizen2SpongeLoadedEvent; import com.denizenscript.denizen2sponge.spongeevents.Denizen2SpongeLoadingEvent; +import com.denizenscript.denizen2sponge.spongescripts.GameCommandScript; import com.denizenscript.denizen2sponge.tags.handlers.*; import com.denizenscript.denizen2sponge.utilities.flags.FlagHelper; import com.google.inject.Inject; @@ -157,6 +158,8 @@ public void onServerStart(GamePreInitializationEvent event) { Denizen2Core.register(new ServerTagBase()); Denizen2Core.register(new TextsTagBase()); Denizen2Core.register(new WorldTagBase()); + // Sponge Script Types + Denizen2Core.register("command", GameCommandScript::new); // Sponge Commands ExCommand.register(); // Sponge related Helpers diff --git a/src/main/java/com/denizenscript/denizen2sponge/Denizen2SpongeImplementation.java b/src/main/java/com/denizenscript/denizen2sponge/Denizen2SpongeImplementation.java index be91756..bee986f 100644 --- a/src/main/java/com/denizenscript/denizen2sponge/Denizen2SpongeImplementation.java +++ b/src/main/java/com/denizenscript/denizen2sponge/Denizen2SpongeImplementation.java @@ -4,6 +4,7 @@ import com.denizenscript.denizen2core.commands.CommandEntry; import com.denizenscript.denizen2core.commands.CommandQueue; import com.denizenscript.denizen2core.utilities.ErrorInducedException; +import com.denizenscript.denizen2sponge.spongescripts.GameCommandScript; import org.spongepowered.api.Sponge; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColors; @@ -13,9 +14,14 @@ public class Denizen2SpongeImplementation extends Denizen2Implementation { + @Override + public void preReload() { + GameCommandScript.clear(); + } + @Override public void reload() { - // TODO: stuff? + // ...? } @Override diff --git a/src/main/java/com/denizenscript/denizen2sponge/spongecommands/ExCommand.java b/src/main/java/com/denizenscript/denizen2sponge/spongecommands/ExCommand.java index 7833275..a8864d6 100644 --- a/src/main/java/com/denizenscript/denizen2sponge/spongecommands/ExCommand.java +++ b/src/main/java/com/denizenscript/denizen2sponge/spongecommands/ExCommand.java @@ -64,37 +64,12 @@ public CommandResult execute(CommandSource commandSource, CommandContext command argset.remove(0); } String cmd = CoreUtilities.concat(argset, " "); - // TODO: Redirect output to the commandSource! MapTag defs = new MapTag(); AbstractSender send = null; if (commandSource instanceof Player) { defs.getInternal().put("source", new TextTag("player")); defs.getInternal().put("player", new PlayerTag((Player) commandSource)); send = new PlayerSender((Player) commandSource); - /* - MapTag mt; - Debug.info("SUPPORTS? : " + ((Player) commandSource).supports(FlagHelper.FLAGMAP)); - Optional omt = ((Player) commandSource).get(FlagHelper.FLAGMAP); - if (omt.isPresent()) { - Debug.info("FOUND IT!: " + omt.get().flags); - mt = omt.get().flags; - } - else { - mt = new MapTag(); - } - IntegerTag it; - if (mt.getInternal().containsKey("HELLO")) { - it = IntegerTag.getFor((e) -> { throw new RuntimeException("Denizen2: " + e); }, mt.getInternal().get("HELLO")); - Debug.info("FOUND NUMBER!: " + it); - it = new IntegerTag(it.getInternal() + 1); - } - else { - it = new IntegerTag(0); - } - mt.getInternal().put("HELLO", it); - ((Player) commandSource).offer(new FlagMapDataImpl(new FlagMap(mt))); - commandSource.sendMessage(Text.of("FOUND: " + mt.getInternal().get("HELLO"))); - */ } else if (commandSource instanceof CommandBlockSource) { defs.getInternal().put("source", new TextTag("block")); diff --git a/src/main/java/com/denizenscript/denizen2sponge/spongescripts/GameCommandScript.java b/src/main/java/com/denizenscript/denizen2sponge/spongescripts/GameCommandScript.java new file mode 100644 index 0000000..0721a62 --- /dev/null +++ b/src/main/java/com/denizenscript/denizen2sponge/spongescripts/GameCommandScript.java @@ -0,0 +1,149 @@ +package com.denizenscript.denizen2sponge.spongescripts; + +import com.denizenscript.denizen2core.Denizen2Core; +import com.denizenscript.denizen2core.commands.CommandQueue; +import com.denizenscript.denizen2core.commands.CommandScriptSection; +import com.denizenscript.denizen2core.scripts.CommandScript; +import com.denizenscript.denizen2core.tags.AbstractTagObject; +import com.denizenscript.denizen2core.tags.objects.ListTag; +import com.denizenscript.denizen2core.tags.objects.MapTag; +import com.denizenscript.denizen2core.tags.objects.TextTag; +import com.denizenscript.denizen2core.utilities.AbstractSender; +import com.denizenscript.denizen2core.utilities.CoreUtilities; +import com.denizenscript.denizen2core.utilities.debugging.ColorSet; +import com.denizenscript.denizen2core.utilities.debugging.Debug; +import com.denizenscript.denizen2core.utilities.yaml.YAMLConfiguration; +import com.denizenscript.denizen2sponge.Denizen2Sponge; +import com.denizenscript.denizen2sponge.tags.objects.LocationTag; +import com.denizenscript.denizen2sponge.tags.objects.PlayerTag; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.command.*; +import org.spongepowered.api.command.args.CommandContext; +import org.spongepowered.api.command.args.GenericArguments; +import org.spongepowered.api.command.source.CommandBlockSource; +import org.spongepowered.api.command.spec.CommandExecutor; +import org.spongepowered.api.command.spec.CommandSpec; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.text.Text; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class GameCommandScript extends CommandScript implements CommandExecutor { + + public static List currentCommandScripts = new ArrayList<>(); + + public static void clear() { + for (GameCommandScript script : currentCommandScripts) { + script.disable(); + } + currentCommandScripts.clear(); + } + + // <--[explanation] + // @Name In-Game Command Scripts + // @Group Script Types + // @Description + // An in-game command script is a script that handles in-game commands. + // It is simply identified with the type "command". + // --> + + public GameCommandScript(String name, YAMLConfiguration section) { + super(name, section); + if (section.isList("name")) { + cmdName = section.getStringList("name"); + } + else { + cmdName = new ArrayList<>(); + cmdName.add(section.getString("name")); + } + cmdDesc = section.getString("description", "No description."); + cmdPerm = section.getString("permission", null); + } + + public List cmdName; + + public String cmdDesc; + + public String cmdPerm; + + @Override + public boolean isExecutable(String section) { + return !section.startsWith("constant"); + } + + public CommandScriptSection getSection(String name) { + if (name == null || name.length() == 0) { + return null; + } + return sections.get(CoreUtilities.toLowerCase(name)); + } + + @Override + public boolean init() { + if (super.init()) { + register(); + return true; + } + return false; + } + + public CommandMapping cmdMapping; + + public void disable() { + Sponge.getCommandManager().removeMapping(cmdMapping); + } + + public void register() { + if (Denizen2Core.getImplementation().generalDebug()) { + Debug.good("Registering command " + ColorSet.emphasis + cmdName + ColorSet.good + " to sponge..."); + } + CommandSpec.Builder cmdspecb = CommandSpec.builder() + .description(Text.of(cmdDesc)) + .arguments(GenericArguments.optional(GenericArguments.remainingRawJoinedStrings(Text.of("dCommand")))) + .executor(this); + if (cmdPerm != null) { + cmdspecb.permission(cmdPerm); + } + CommandSpec cmdspec = cmdspecb.build(); + cmdMapping = Sponge.getCommandManager().register(Denizen2Sponge.instance, cmdspec, cmdName).get(); + } + + @Override + public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException { + ArrayList argset = new ArrayList<>(commandContext.hasAny("dCommand") ? + commandContext.getAll("dCommand") : new ArrayList<>()); + String cmd = CoreUtilities.concat(argset, " "); + AbstractSender send = null; + CommandQueue q = getSection("script").toQueue(); + MapTag context = new MapTag(); + context.getInternal().put("raw_arguments", new TextTag(cmd)); + ListTag lt = new ListTag(); + for (String str : argset) { + lt.getInternal().add(new TextTag(str)); + } + context.getInternal().put("arguments", lt); + if (commandSource instanceof Player) { + context.getInternal().put("source", new TextTag("player")); + context.getInternal().put("player", new PlayerTag((Player) commandSource)); + } + else if (commandSource instanceof CommandBlockSource) { + context.getInternal().put("source", new TextTag("block")); + context.getInternal().put("location", new LocationTag(((CommandBlockSource) commandSource).getLocation())); + } + else { + context.getInternal().put("source", new TextTag("server")); + } + q.commandStack.peek().setDefinition("context", context); + if (getDebugMode().showFull) { + Debug.good("Running in-game command script: " + ColorSet.emphasis + cmdName + ColorSet.good + "..."); + for (Map.Entry def : context.getInternal().entrySet()) { + Debug.good("Context Definition: " + ColorSet.emphasis + def.getKey() + ColorSet.good + + " is " + ColorSet.emphasis + def.getValue().toString()); + } + } + q.start(); + return CommandResult.empty(); + } +}