From e0bf8249bdf23386039d395ec55b2a011c2e09ac Mon Sep 17 00:00:00 2001 From: Dico Date: Sun, 23 Sep 2018 21:34:48 +0100 Subject: Tweaks --- .../command/parameter/type/EnumParameterType.java | 44 ++++++++++++++ .../type/MapBasedParameterTypeSelector.java | 5 ++ .../reflect/ReflectiveRegistration.java | 2 +- .../dico/parcels2/command/ParcelCommandBuilder.kt | 67 +++++++++++----------- .../dico/parcels2/command/ParcelParameterTypes.kt | 10 ++++ .../io/dico/parcels2/listener/ParcelListeners.kt | 2 +- 6 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/EnumParameterType.java diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/EnumParameterType.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/EnumParameterType.java new file mode 100644 index 0000000..e71c6cc --- /dev/null +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/EnumParameterType.java @@ -0,0 +1,44 @@ +package io.dico.dicore.command.parameter.type; + +import io.dico.dicore.command.CommandException; +import io.dico.dicore.command.parameter.ArgumentBuffer; +import io.dico.dicore.command.parameter.Parameter; +import org.bukkit.Location; +import org.bukkit.command.CommandSender; + +import java.util.ArrayList; +import java.util.List; + +public class EnumParameterType extends SimpleParameterType { + private final E[] universe; + + public EnumParameterType(Class returnType) { + super(returnType); + universe = returnType.getEnumConstants(); + if (universe == null) { + throw new IllegalArgumentException("returnType must be an enum"); + } + } + + @Override protected E parse(Parameter parameter, CommandSender sender, String input) throws CommandException { + for (E constant : universe) { + if (constant.name().equalsIgnoreCase(input)) { + return constant; + } + } + + throw CommandException.invalidArgument(parameter.getName(), "the enum value does not exist"); + } + + @Override public List complete(Parameter parameter, CommandSender sender, Location location, ArgumentBuffer buffer) { + String input = buffer.next().toUpperCase(); + List result = new ArrayList<>(); + for (E constant : universe) { + if (constant.name().toUpperCase().startsWith(input.toUpperCase())) { + result.add(constant.name().toLowerCase()); + } + } + return result; + } + +} diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/MapBasedParameterTypeSelector.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/MapBasedParameterTypeSelector.java index 4e475fe..d407f87 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/MapBasedParameterTypeSelector.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/MapBasedParameterTypeSelector.java @@ -23,6 +23,11 @@ public class MapBasedParameterTypeSelector implements IParameterTypeSelector { if (useDefault && out == null) { out = defaultSelector.selectExact(key); } + if (out == null && key.getReturnType().isEnum()) { + //noinspection unchecked + out = new EnumParameterType(key.getReturnType()); + addType(false, out); + } return cast(out); } diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveRegistration.java b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveRegistration.java index 669f440..0495ba9 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveRegistration.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveRegistration.java @@ -223,7 +223,7 @@ public class ReflectiveRegistration { String[] parameterNames = lookupParameterNames(method, parameters, start); for (int i = start, n = parameters.length; i < n; i++) { - if (parameters[i].getType().getName().equals("kotlin.coroutines.experimental.Continuation")) { + if (parameters[i].getType().getName().equals("kotlin.coroutines.Continuation")) { List temp = new ArrayList<>(Arrays.asList(parameterNames)); temp.remove(i - start); parameterNames = temp.toArray(new String[0]); diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt index e2c7a1d..b9dfe1e 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt @@ -5,50 +5,51 @@ import io.dico.dicore.command.ICommandAddress import io.dico.dicore.command.ICommandDispatcher import io.dico.dicore.command.registration.reflect.ReflectiveRegistration import io.dico.parcels2.ParcelsPlugin +import io.dico.parcels2.logger import java.util.LinkedList import java.util.Queue @Suppress("UsePropertyAccessSyntax") -fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher { - //@formatter:off - return CommandBuilder() - .setChatController(ParcelsChatController()) - .addParameterType(false, ParcelParameterType(plugin.parcelProvider)) - .addParameterType(true, ParcelTarget.PType(plugin.parcelProvider)) +fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher = + with(CommandBuilder()) { + setChatController(ParcelsChatController()) + addParameterType(false, ParcelParameterType(plugin.parcelProvider)) + addParameterType(false, ProfileParameterType()) + addParameterType(true, ParcelTarget.PType(plugin.parcelProvider)) - .group("parcel", "plot", "plots", "p") - .addRequiredPermission("parcels.command") - .registerCommands(CommandsGeneral(plugin)) - .registerCommands(CommandsAddedStatusLocal(plugin)) + group("parcel", "plot", "plots", "p") { + addRequiredPermission("parcels.command") + registerCommands(CommandsGeneral(plugin)) + registerCommands(CommandsAddedStatusLocal(plugin)) - .group("option", "opt", "o") - .apply { CommandsParcelOptions.setGroupDescription(this) } - .registerCommands(CommandsParcelOptions(plugin)) - .parent() + group("option", "opt", "o") { + CommandsParcelOptions.setGroupDescription(this) + registerCommands(CommandsParcelOptions(plugin)) + } - .group("global", "g") - .registerCommands(CommandsAddedStatusGlobal(plugin)) - .parent() + group("global", "g") { + registerCommands(CommandsAddedStatusGlobal(plugin)) + } - .group("admin", "a") - .registerCommands(CommandsAdmin(plugin)) - .parent() + group("admin", "a") { + registerCommands(CommandsAdmin(plugin)) + } - .putDebugCommands(plugin) + if (!logger.isDebugEnabled) return@group - .parent() - .generateHelpAndSyntaxCommands() - .getDispatcher() - //@formatter:on -} + group("debug", "d") { + registerCommands(CommandsDebug(plugin)) + } + } + + generateHelpAndSyntaxCommands() + getDispatcher() + } -private fun CommandBuilder.putDebugCommands(plugin: ParcelsPlugin): CommandBuilder { - //if (!logger.isDebugEnabled) return this - //@formatter:off - return group("debug", "d") - .registerCommands(CommandsDebug(plugin)) - .parent() - //@formatter:on +inline fun CommandBuilder.group(name: String, vararg aliases: String, config: CommandBuilder.() -> Unit) { + group(name, *aliases) + config() + parent() } private fun CommandBuilder.generateHelpAndSyntaxCommands(): CommandBuilder { diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt index de3cf64..e7c4e48 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt @@ -7,6 +7,7 @@ import io.dico.dicore.command.parameter.type.ParameterType import io.dico.parcels2.Parcel import io.dico.parcels2.ParcelProvider import io.dico.parcels2.ParcelWorld +import io.dico.parcels2.PlayerProfile import org.bukkit.command.CommandSender import org.bukkit.entity.Player @@ -44,3 +45,12 @@ class ParcelParameterType(val parcelProvider: ParcelProvider) : ParameterType(PlayerProfile::class.java) { + + override fun parse(parameter: Parameter, sender: CommandSender, buffer: ArgumentBuffer): PlayerProfile { + val input = buffer.next() + return PlayerProfile.byName(input, allowReal = true, allowFake = true) + } + +} diff --git a/src/main/kotlin/io/dico/parcels2/listener/ParcelListeners.kt b/src/main/kotlin/io/dico/parcels2/listener/ParcelListeners.kt index e39583c..1af3406 100644 --- a/src/main/kotlin/io/dico/parcels2/listener/ParcelListeners.kt +++ b/src/main/kotlin/io/dico/parcels2/listener/ParcelListeners.kt @@ -170,7 +170,7 @@ class ParcelListeners( if (ppa.isNullOr { hasBlockVisitors }) event.isCancelled = true } - private val bedTypes = EnumSet.copyOf(getMaterialsWithWoodTypePrefix("BED").toList()) + private val bedTypes = EnumSet.copyOf(getMaterialsWithWoolColorPrefix("BED").toList()) /* * Prevents players from placing liquids, using flint and steel, changing redstone components, * using inputs (unless allowed by the plot), -- cgit v1.2.3