Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
in-game command scripts!
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 24, 2016
1 parent e6c56d0 commit 39e5f1a
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 26 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;
Expand All @@ -13,9 +14,14 @@

public class Denizen2SpongeImplementation extends Denizen2Implementation {

@Override
public void preReload() {
GameCommandScript.clear();
}

@Override
public void reload() {
// TODO: stuff?
// ...?
}

@Override
Expand Down
Expand Up @@ -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<FlagMap> 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"));
Expand Down
@@ -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<GameCommandScript> 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<String> 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<String> 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<String, AbstractTagObject> 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();
}
}

0 comments on commit 39e5f1a

Please sign in to comment.