Permalink
Browse files

Added a command to list variables, and reworked the variable commands.

  • Loading branch information...
me4502 committed Jul 25, 2017
1 parent 7fd272c commit e90390dedc2f6beac958597b695ad78b109c2ac7
@@ -23,12 +23,15 @@
import com.me4502.modularframework.module.guice.ModuleConfiguration;
import com.sk89q.craftbook.core.util.ConfigValue;
import com.sk89q.craftbook.core.util.CraftBookException;
import com.sk89q.craftbook.core.util.PermissionNode;
import com.sk89q.craftbook.core.util.documentation.DocumentationProvider;
import com.sk89q.craftbook.sponge.CraftBookPlugin;
import com.sk89q.craftbook.sponge.mechanics.types.SpongeMechanic;
import com.sk89q.craftbook.sponge.mechanics.variable.command.GetVariableCommand;
import com.sk89q.craftbook.sponge.mechanics.variable.command.ListVariableCommand;
import com.sk89q.craftbook.sponge.mechanics.variable.command.RemoveVariableCommand;
import com.sk89q.craftbook.sponge.mechanics.variable.command.SetVariableCommand;
import com.sk89q.craftbook.sponge.util.SpongePermissionNode;
import com.sk89q.craftbook.sponge.util.TextUtil;
import com.sk89q.craftbook.sponge.util.type.TypeTokens;
import ninja.leaping.configurate.ConfigurationNode;
@@ -43,9 +46,11 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.command.SendCommandEvent;
import org.spongepowered.api.event.message.MessageChannelEvent;
import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.text.LiteralText;
import org.spongepowered.api.text.Text;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -58,7 +63,9 @@
@Module(id = "variables", name = "Variables", onEnable="onInitialize", onDisable="onDisable")
public class Variables extends SpongeMechanic implements DocumentationProvider {
private static final Pattern VARIABLE_PATTERN = Pattern.compile("%(?:([a-zA-Z0-9]+)\\|)*([a-zA-Z0-9]+)%");
public static final String GLOBAL_NAMESPACE = "global";
private static final Pattern VARIABLE_PATTERN = Pattern.compile("%(?:([a-zA-Z0-9_]+)\\|)*([a-zA-Z0-9]+)%");
public static Variables instance;
@@ -68,6 +75,18 @@
@ModuleConfiguration
public ConfigurationNode config;
private SpongePermissionNode setPermission = new SpongePermissionNode("craftbook.variables.set", "Allows setting variables.", PermissionDescription.ROLE_USER);
private SpongePermissionNode setGlobalPermission = new SpongePermissionNode("craftbook.variables.set.global", "Allows setting global variables.", PermissionDescription.ROLE_STAFF);
private SpongePermissionNode getPermission = new SpongePermissionNode("craftbook.variables.get", "Allows getting variables.", PermissionDescription.ROLE_USER);
private SpongePermissionNode getGlobalPermission = new SpongePermissionNode("craftbook.variables.get.global", "Allows getting global variables.", PermissionDescription.ROLE_STAFF);
private SpongePermissionNode delPermission = new SpongePermissionNode("craftbook.variables.remove", "Allows removing variables.", PermissionDescription.ROLE_USER);
private SpongePermissionNode delGlobalPermission = new SpongePermissionNode("craftbook.variables.remove.global", "Allows removing global variables.", PermissionDescription.ROLE_STAFF);
private SpongePermissionNode listPermission = new SpongePermissionNode("craftbook.variables.list", "Allows listing variables.", PermissionDescription.ROLE_USER);
private SpongePermissionNode listGlobalPermission = new SpongePermissionNode("craftbook.variables.list.global", "Allows listing global variables.", PermissionDescription.ROLE_STAFF);
private ConfigValue<Boolean> defaultToGlobal = new ConfigValue<>("default-to-global", "If no namespace is provided, default to global. "
+ "Otherwise personal namespace", true);
@@ -86,52 +105,74 @@ public void onInitialize() throws CraftBookException {
variableStore = new HashMap<>();
}
setPermission.register();
setGlobalPermission.register();
getPermission.register();
getGlobalPermission.register();
delPermission.register();
delGlobalPermission.register();
listPermission.register();
listGlobalPermission.register();
defaultToGlobal.load(config);
CommandSpec setVariable = CommandSpec.builder()
.description(Text.of("Set the value of a variable"))
.arguments(GenericArguments.string(Text.of("key")), GenericArguments.string(Text.of("value")))
.executor(new SetVariableCommand(this, false))
.build();
CommandSpec setGlobalVariable = CommandSpec.builder()
.description(Text.of("Set the value of a global variable"))
.arguments(GenericArguments.string(Text.of("key")), GenericArguments.string(Text.of("value")))
.executor(new SetVariableCommand(this, true))
.permission(setPermission.getNode())
.arguments(
GenericArguments.string(Text.of("key")),
GenericArguments.string(Text.of("value")),
GenericArguments.flags()
.permissionFlag(setGlobalPermission.getNode(), "g", "-global")
.valueFlag(GenericArguments.string(Text.of("namespace")), "n", "-namespace")
.buildWith(GenericArguments.none()))
.executor(new SetVariableCommand(this))
.build();
CommandSpec getVariable = CommandSpec.builder()
.description(Text.of("Get the value of a variable"))
.arguments(GenericArguments.string(Text.of("key")))
.executor(new GetVariableCommand(this, false))
.build();
CommandSpec getGlobalVariable = CommandSpec.builder()
.description(Text.of("Get the value of a global variable"))
.arguments(GenericArguments.string(Text.of("key")))
.executor(new GetVariableCommand(this, true))
.permission(getPermission.getNode())
.arguments(
GenericArguments.string(Text.of("key")),
GenericArguments.flags()
.permissionFlag(getGlobalPermission.getNode(), "g", "-global")
.valueFlag(GenericArguments.string(Text.of("namespace")), "n", "-namespace")
.buildWith(GenericArguments.none()))
.executor(new GetVariableCommand(this))
.build();
CommandSpec removeVariable = CommandSpec.builder()
.description(Text.of("Removes a variable"))
.arguments(GenericArguments.string(Text.of("key")))
.executor(new RemoveVariableCommand(this, false))
.permission(delPermission.getNode())
.arguments(
GenericArguments.string(Text.of("key")),
GenericArguments.flags()
.permissionFlag(delGlobalPermission.getNode(), "g", "-global")
.valueFlag(GenericArguments.string(Text.of("namespace")), "n", "-namespace")
.buildWith(GenericArguments.none()))
.executor(new RemoveVariableCommand(this))
.build();
CommandSpec removeGlobalVariable = CommandSpec.builder()
.description(Text.of("Removes a global variable"))
.arguments(GenericArguments.string(Text.of("key")))
.executor(new RemoveVariableCommand(this, true))
CommandSpec listVariable = CommandSpec.builder()
.description(Text.of("List variables"))
.permission(listPermission.getNode())
.arguments(
GenericArguments.flags()
.permissionFlag(listPermission.getNode(), "g", "-global")
.valueFlag(GenericArguments.string(Text.of("namespace")), "n", "-namespace")
.buildWith(GenericArguments.none()))
.executor(new ListVariableCommand(this))
.build();
CommandSpec variableCommand = CommandSpec.builder()
.description(Text.of("Base Variable command"))
.child(setVariable, "set", "def", "define")
.child(setGlobalVariable, "setglobal", "defglobal", "defineglobal")
.child(getVariable, "get")
.child(getGlobalVariable, "getglobal")
.child(removeVariable, "rm", "del", "remove")
.child(removeGlobalVariable, "rmglobal", "delglobal", "removeglobal")
.child(listVariable, "list", "ls")
.build();
variableCommandMapping = Sponge.getGame().getCommandManager().register(CraftBookPlugin.spongeInst(), variableCommand, "var", "variable", "variables").orElse(null);
@@ -172,6 +213,10 @@ public void removeVariable(String namespace, String key) {
variableStore.remove(namespace);
}
public Map<String, String> getVariables(String namespace) {
return variableStore.getOrDefault(namespace, Collections.emptyMap());
}
public String parseVariables(String line, @Nullable Player player) {
for(Pair<String, String> possibleVariable : getPossibleVariables(line)) {
String namespace = possibleVariable.getLeft();
@@ -180,7 +225,7 @@ public String parseVariables(String line, @Nullable Player player) {
boolean explicit = true;
if (namespace == null) {
if (defaultToGlobal.getValue()) {
namespace = "global";
namespace = GLOBAL_NAMESPACE;
if(player != null && getVariable(player.getUniqueId().toString(), name) != null) {
namespace = player.getUniqueId().toString();
}
@@ -259,4 +304,18 @@ public String getPath() {
defaultToGlobal
};
}
@Override
public PermissionNode[] getPermissionNodes() {
return new PermissionNode[] {
setPermission,
setGlobalPermission,
getPermission,
getGlobalPermission,
delPermission,
delGlobalPermission,
listPermission,
listGlobalPermission
};
}
}
@@ -31,18 +31,22 @@
public class GetVariableCommand implements CommandExecutor {
private Variables variables;
private boolean global;
public GetVariableCommand(Variables variables, boolean global) {
public GetVariableCommand(Variables variables) {
this.variables = variables;
this.global = global;
}
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
String key = args.<String>getOne("key").get();
boolean global = args.hasAny("g");
String namespace = global ? Variables.GLOBAL_NAMESPACE : args.<String>getOne("namespace").orElse(((Player) src).getUniqueId().toString());
if (namespace.equals(Variables.GLOBAL_NAMESPACE) && !global) {
src.sendMessage(Text.of(TextColors.RED, "Invalid namespace!"));
return CommandResult.empty();
}
src.sendMessage(Text.of(TextColors.YELLOW, "Variable " + key + " is set to " + variables.getVariable(global ? "global" : ((Player) src).getUniqueId().toString(), key)));
src.sendMessage(Text.of(TextColors.YELLOW, "Variable " + key + " is set to " + variables.getVariable(namespace, key)));
return CommandResult.success();
}
@@ -0,0 +1,67 @@
/*
* CraftBook Copyright (C) 2010-2017 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2017 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not,
* see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.craftbook.sponge.mechanics.variable.command;
import com.sk89q.craftbook.sponge.mechanics.variable.Variables;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.service.pagination.PaginationList;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.annotation.NonnullByDefault;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@NonnullByDefault
public class ListVariableCommand implements CommandExecutor {
private Variables variables;
public ListVariableCommand(Variables variables) {
this.variables = variables;
}
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
boolean global = args.hasAny("g");
String namespace = global ? Variables.GLOBAL_NAMESPACE : args.<String>getOne("namespace").orElse(((Player) src).getUniqueId().toString());
if (namespace.equals(Variables.GLOBAL_NAMESPACE) && !global) {
src.sendMessage(Text.of(TextColors.RED, "Invalid namespace!"));
return CommandResult.empty();
}
List<Text> variableList = new ArrayList<>();
for (Map.Entry<String, String> entry : variables.getVariables(namespace).entrySet()) {
variableList.add(Text.of(TextColors.YELLOW, entry.getKey(), TextColors.GRAY, ": ", TextColors.GREEN, entry.getValue()));
}
PaginationList.builder()
.title(Text.of(TextColors.YELLOW, "Variable List"))
.header(Text.of(TextColors.GREEN, namespace))
.contents(variableList)
.build().sendTo(src);
return CommandResult.success();
}
}
@@ -31,18 +31,22 @@
public class RemoveVariableCommand implements CommandExecutor {
private Variables variables;
private boolean global;
public RemoveVariableCommand(Variables variables, boolean global) {
public RemoveVariableCommand(Variables variables) {
this.variables = variables;
this.global = global;
}
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
String key = args.<String>getOne("key").get();
boolean global = args.hasAny("g");
String namespace = global ? Variables.GLOBAL_NAMESPACE : args.<String>getOne("namespace").orElse(((Player) src).getUniqueId().toString());
if (namespace.equals(Variables.GLOBAL_NAMESPACE) && !global) {
src.sendMessage(Text.of(TextColors.RED, "Invalid namespace!"));
return CommandResult.empty();
}
variables.removeVariable(global ? "global" : ((Player)src).getUniqueId().toString(), key);
variables.removeVariable(namespace, key);
src.sendMessage(Text.of(TextColors.YELLOW, "Removed variable " + key));
return CommandResult.success();
@@ -31,16 +31,20 @@
public class SetVariableCommand implements CommandExecutor {
private Variables variables;
private boolean global;
public SetVariableCommand(Variables variables, boolean global) {
public SetVariableCommand(Variables variables) {
this.variables = variables;
this.global = global;
}
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
String key = args.<String>getOne("key").get();
boolean global = args.hasAny("g");
String namespace = global ? Variables.GLOBAL_NAMESPACE : args.<String>getOne("namespace").orElse(((Player) src).getUniqueId().toString());
if (namespace.equals(Variables.GLOBAL_NAMESPACE) && !global) {
src.sendMessage(Text.of(TextColors.RED, "Invalid namespace!"));
return CommandResult.empty();
}
if(!Variables.isValidVariableKey(key)) {
src.sendMessage(Text.of(TextColors.RED, "Key contains invalid characters!"));
@@ -49,8 +53,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
String value = args.<String>getOne("value").get();
variables.addVariable(global ? "global" : ((Player) src).getUniqueId().toString(), key, value);
src.sendMessage(Text.of(TextColors.YELLOW, "Set variable " + key + " to value " + variables.getVariable(global ? "global" : ((Player) src).getUniqueId().toString(), key)));
variables.addVariable(namespace, key, value);
src.sendMessage(Text.of(TextColors.YELLOW, "Set variable " + key + " to value " + variables.getVariable(namespace, key)));
return CommandResult.success();
}
2 docs

0 comments on commit e90390d

Please sign in to comment.