Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup command params #1637

Merged
merged 4 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Command(String name, String description, String usageMessage, String[] al
this.aliases = aliases;
this.activeAliases = aliases;
this.timing = Timings.getCommandTiming(this);
this.commandParameters.put("default", new CommandParameter[]{new CommandParameter("args", CommandParamType.RAWTEXT, true)});
this.commandParameters.put("default", new CommandParameter[]{CommandParameter.newType("args", true, CommandParamType.RAWTEXT)});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/command/SimpleCommandMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void registerSimpleCommands(Object object) {
if (commandParameters != null) {
Map<String, CommandParameter[]> map = Arrays.stream(commandParameters.parameters())
.collect(Collectors.toMap(Parameters::name, parameters -> Arrays.stream(parameters.parameters())
.map(parameter -> new CommandParameter(parameter.name(), parameter.type(), parameter.optional()))
.map(parameter -> CommandParameter.newType(parameter.name(), parameter.optional(), parameter.type()))
.distinct()
.toArray(CommandParameter[]::new)));

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/cn/nukkit/command/data/CommandEnum.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
package cn.nukkit.command.data;

import cn.nukkit.block.BlockID;
import cn.nukkit.item.ItemID;
import com.google.common.collect.ImmutableList;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
* @author CreeperFace
*/
public class CommandEnum {

public static final CommandEnum ENUM_BOOLEAN = new CommandEnum("Boolean", ImmutableList.of("true", "false"));
public static final CommandEnum ENUM_GAMEMODE = new CommandEnum("GameMode",
ImmutableList.of("survival", "creative", "s", "c", "adventure", "a", "spectator", "view", "v", "spc"));
public static final CommandEnum ENUM_BLOCK;
public static final CommandEnum ENUM_ITEM;

static {
ImmutableList.Builder<String> blocks = ImmutableList.builder();
for (Field field : BlockID.class.getDeclaredFields()) {
blocks.add(field.getName().toLowerCase());
}
ENUM_BLOCK = new CommandEnum("Block", blocks.build());

ImmutableList.Builder<String> items = ImmutableList.builder();
for (Field field : ItemID.class.getDeclaredFields()) {
items.add(field.getName().toLowerCase());
}
items.addAll(ENUM_BLOCK.getValues());
ENUM_ITEM = new CommandEnum("Item", items.build());
}

private String name;
private List<String> values;

SupremeMortal marked this conversation as resolved.
Show resolved Hide resolved
public CommandEnum(String name, String... values) {
this(name, Arrays.asList(values));
}

public CommandEnum(String name, List<String> values) {
this.name = name;
this.values = values;
Expand Down
100 changes: 80 additions & 20 deletions src/main/java/cn/nukkit/command/data/CommandParameter.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
package cn.nukkit.command.data;


import java.util.ArrayList;
import java.util.Arrays;

public class CommandParameter {

public final static String ARG_TYPE_STRING = "string";
public final static String ARG_TYPE_STRING_ENUM = "stringenum";
public final static String ARG_TYPE_BOOL = "bool";
public final static String ARG_TYPE_TARGET = "target";
public final static String ARG_TYPE_PLAYER = "target";
public final static String ARG_TYPE_BLOCK_POS = "blockpos";
public final static String ARG_TYPE_RAW_TEXT = "rawtext";
public final static String ARG_TYPE_INT = "int";

public static final String ENUM_TYPE_ITEM_LIST = "itemType";
public static final String ENUM_TYPE_BLOCK_LIST = "blockType";
public static final String ENUM_TYPE_COMMAND_LIST = "commandName";
public static final String ENUM_TYPE_ENCHANTMENT_LIST = "enchantmentType";
public static final String ENUM_TYPE_ENTITY_LIST = "entityType";
public static final String ENUM_TYPE_EFFECT_LIST = "effectType";
public static final String ENUM_TYPE_PARTICLE_LIST = "particleType";

public String name;
public CommandParamType type;
public boolean optional;
Expand All @@ -31,47 +12,126 @@ public class CommandParameter {
public CommandEnum enumData;
public String postFix;

/**
* @deprecated use {@link #newType(String, boolean, CommandParamType)} instead
*/
@Deprecated
public CommandParameter(String name, String type, boolean optional) {
this(name, fromString(type), optional);
}

/**
* @deprecated use {@link #newType(String, boolean, CommandParamType)} instead
*/
@Deprecated
public CommandParameter(String name, CommandParamType type, boolean optional) {
this.name = name;
this.type = type;
this.optional = optional;
}

/**
* @deprecated use {@link #newType(String, boolean, CommandParamType)} instead
*/
@Deprecated
public CommandParameter(String name, boolean optional) {
this(name, CommandParamType.RAWTEXT, optional);
}

/**
* @deprecated use {@link #newType(String, CommandParamType)} instead
*/
@Deprecated
public CommandParameter(String name) {
this(name, false);
}

/**
* @deprecated use {@link #newEnum(String, boolean, String)} instead
*/
@Deprecated
public CommandParameter(String name, boolean optional, String enumType) {
this.name = name;
this.type = CommandParamType.RAWTEXT;
this.optional = optional;
this.enumData = new CommandEnum(enumType, new ArrayList<>());
}

/**
* @deprecated use {@link #newEnum(String, boolean, String[])} instead
*/
@Deprecated
public CommandParameter(String name, boolean optional, String[] enumValues) {
this.name = name;
this.type = CommandParamType.RAWTEXT;
this.optional = optional;
this.enumData = new CommandEnum(name + "Enums", Arrays.asList(enumValues));
this.enumData = new CommandEnum(name + "Enums", enumValues);
}

/**
* @deprecated use {@link #newEnum(String, String)} instead
*/
@Deprecated
public CommandParameter(String name, String enumType) {
this(name, false, enumType);
}

/**
* @deprecated use {@link #newEnum(String, String[])} instead
*/
@Deprecated
public CommandParameter(String name, String[] enumValues) {
this(name, false, enumValues);
}

private CommandParameter(String name, boolean optional, CommandParamType type, CommandEnum enumData, String postFix) {
this.name = name;
this.optional = optional;
this.type = type;
this.enumData = enumData;
this.postFix = postFix;
}

public static CommandParameter newType(String name, CommandParamType type) {
return newType(name, false, type);
}

public static CommandParameter newType(String name, boolean optional, CommandParamType type) {
return new CommandParameter(name, optional, type, null, null);
}

public static CommandParameter newEnum(String name, String[] values) {
return newEnum(name, false, values);
}

public static CommandParameter newEnum(String name, boolean optional, String[] values) {
return newEnum(name, optional, new CommandEnum(name + "Enums", values));
}

public static CommandParameter newEnum(String name, String type) {
return newEnum(name, false, type);
}

public static CommandParameter newEnum(String name, boolean optional, String type) {
return newEnum(name, optional, new CommandEnum(type, new ArrayList<>()));
}

public static CommandParameter newEnum(String name, CommandEnum data) {
return newEnum(name, false, data);
}

public static CommandParameter newEnum(String name, boolean optional, CommandEnum data) {
return new CommandParameter(name, optional, CommandParamType.RAWTEXT, data, null);
}

public static CommandParameter newPostfix(String name, String postfix) {
return newPostfix(name, false, postfix);
}

public static CommandParameter newPostfix(String name, boolean optional, String postfix) {
return new CommandParameter(name, optional, CommandParamType.RAWTEXT, null, postfix);
}

protected static CommandParamType fromString(String param) {
switch (param) {
case "string":
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cn/nukkit/command/defaults/BanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public BanCommand(String name) {
this.commandParameters.clear();
this.commandParameters.put("default",
new CommandParameter[]{
new CommandParameter("player", CommandParamType.TARGET, false),
new CommandParameter("reason", CommandParamType.STRING, true)
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newType("reason", true, CommandParamType.STRING)
});
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/cn/nukkit/command/defaults/BanIpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ public BanIpCommand(String name) {
this.setAliases(new String[]{"banip"});
this.commandParameters.clear();
this.commandParameters.put("default", new CommandParameter[]{
new CommandParameter("player", CommandParamType.TARGET, false),
new CommandParameter("reason", CommandParamType.STRING, true)
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newType("reason", true, CommandParamType.STRING)
});
this.commandParameters.put("byIp", new CommandParameter[]{
CommandParameter.newType("ip", CommandParamType.STRING),
CommandParameter.newType("reason", true, CommandParamType.STRING)
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cn/nukkit/command/defaults/BanListCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.nukkit.command.defaults;

import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandEnum;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.permission.BanEntry;
Expand All @@ -18,7 +19,7 @@ public BanListCommand(String name) {
this.setPermission("nukkit.command.ban.list");
this.commandParameters.clear();
this.commandParameters.put("default", new CommandParameter[]{
new CommandParameter("ips|players", true)
CommandParameter.newEnum("type", true, new CommandEnum("BanListType", "ips", "players"))
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class DebugPasteCommand extends VanillaCommand {
public DebugPasteCommand(String name) {
super(name, "%nukkit.command.debug.description", "%nukkit.command.debug.usage");
this.setPermission("nukkit.command.debug.perform");
this.commandParameters.clear();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.nukkit.Server;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandEnum;
import cn.nukkit.command.data.CommandParamType;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.lang.TranslationContainer;
Expand All @@ -17,11 +18,10 @@ public DefaultGamemodeCommand(String name) {
this.setPermission("nukkit.command.defaultgamemode");
this.commandParameters.clear();
this.commandParameters.put("default", new CommandParameter[]{
new CommandParameter("mode", CommandParamType.INT, false)
CommandParameter.newType("gameMode", CommandParamType.INT)
});
this.commandParameters.put("byString", new CommandParameter[]{
new CommandParameter("mode", new String[]{"survival", "creative", "s", "c",
"adventure", "a", "spectator", "view", "v"})
CommandParameter.newEnum("gameMode", CommandEnum.ENUM_GAMEMODE)
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/command/defaults/DeopCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public DeopCommand(String name) {
super(name, "%nukkit.command.deop.description", "%commands.deop.description");
this.setPermission("nukkit.command.op.take");
this.commandParameters.put("default", new CommandParameter[]{
new CommandParameter("player", CommandParamType.TARGET, false)
CommandParameter.newType("player", CommandParamType.TARGET)
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.nukkit.Server;
import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandEnum;
import cn.nukkit.command.data.CommandParamType;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.lang.TranslationContainer;
Expand All @@ -21,11 +22,10 @@ public DifficultyCommand(String name) {
this.setPermission("nukkit.command.difficulty");
this.commandParameters.clear();
this.commandParameters.put("default", new CommandParameter[]{
new CommandParameter("difficulty", CommandParamType.INT, false)
CommandParameter.newType("difficulty", CommandParamType.INT)
});
this.commandParameters.put("byString", new CommandParameter[]{
new CommandParameter("difficulty", new String[]{"peaceful", "p", "easy", "e",
"normal", "n", "hard", "h"})
CommandParameter.newEnum("difficulty", new CommandEnum("Difficulty", "peaceful", "p", "easy", "e", "normal", "n", "hard", "h"))
});
}

Expand Down
28 changes: 21 additions & 7 deletions src/main/java/cn/nukkit/command/defaults/EffectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.nukkit.Player;
import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandEnum;
import cn.nukkit.command.data.CommandParamType;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.lang.TranslationContainer;
Expand All @@ -11,6 +12,11 @@
import cn.nukkit.utils.ServerException;
import cn.nukkit.utils.TextFormat;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Snake1999 and Pub4Game on 2016/1/23.
* Package cn.nukkit.command.defaults in project nukkit.
Expand All @@ -20,16 +26,24 @@ public EffectCommand(String name) {
super(name, "%nukkit.command.effect.description", "%commands.effect.usage");
this.setPermission("nukkit.command.effect");
this.commandParameters.clear();

List<String> effects = new ArrayList<>();
for (Field field : Effect.class.getDeclaredFields()) {
if (field.getType() == int.class && field.getModifiers() == (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL)) {
effects.add(field.getName().toLowerCase());
}
}

this.commandParameters.put("default", new CommandParameter[]{
new CommandParameter("player", CommandParamType.TARGET, false),
new CommandParameter("effect", CommandParamType.STRING, false), //Do not use Enum here because of buggy behavior
new CommandParameter("seconds", CommandParamType.INT, true),
new CommandParameter("amplifier", true),
new CommandParameter("hideParticle", true, new String[]{"true", "false"})
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newEnum("effect", new CommandEnum("Effect", effects)),
CommandParameter.newType("seconds", true, CommandParamType.INT),
CommandParameter.newType("amplifier", true, CommandParamType.INT),
CommandParameter.newEnum("hideParticle", true, CommandEnum.ENUM_BOOLEAN)
});
this.commandParameters.put("clear", new CommandParameter[]{
new CommandParameter("player", CommandParamType.TARGET, false),
new CommandParameter("clear", new String[]{"clear"})
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newEnum("clear", new CommandEnum("ClearEffects", "clear"))
});
}

Expand Down
Loading