From 7ba002ac8d993db59f4b609f0acde38ab593a6a3 Mon Sep 17 00:00:00 2001 From: sk89q Date: Tue, 1 Jul 2014 13:50:49 -0700 Subject: [PATCH 1/6] Clean up ParametricCallable. --- .../command/parametric/ParametricCallable.java | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java b/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java index 6fc9266fe..eb4bc8d1a 100644 --- a/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java +++ b/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java @@ -19,7 +19,6 @@ package com.sk89q.worldedit.util.command.parametric; -import com.google.common.base.Joiner; import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandException; @@ -109,8 +108,7 @@ class ParametricCallable implements CommandCallable { } // Special annotation bindings } else if (parameter.getBinding() == null) { - parameter.setBinding(builder.getBindings().get( - annotation.annotationType())); + parameter.setBinding(builder.getBindings().get(annotation.annotationType())); parameter.setClassifier(annotation); } } @@ -188,7 +186,6 @@ class ParametricCallable implements CommandCallable { // Provide help if -? is specified if (context.hasFlag('?')) { - String title = Joiner.on(" ").join(parentCommands); throw new InvalidUsageException(null, this, true); } @@ -314,8 +311,7 @@ class ParametricCallable implements CommandCallable { CommandContext context = existing.getContext(); if (parameter.isValueFlag()) { - return new StringArgumentStack( - context, context.getFlag(parameter.getFlag()), false); + return new StringArgumentStack(context, context.getFlag(parameter.getFlag()), false); } else { String v = context.hasFlag(parameter.getFlag()) ? "true" : "false"; return new StringArgumentStack(context, v, true); @@ -370,17 +366,14 @@ class ParametricCallable implements CommandCallable { * @throws ParameterException on an error * @throws CommandException on an error */ - private Object getDefaultValue(int i, ContextArgumentStack scoped) - throws ParameterException, CommandException, InvocationTargetException { + private Object getDefaultValue(int i, ContextArgumentStack scoped) throws ParameterException, CommandException, InvocationTargetException { CommandContext context = scoped.getContext(); ParameterData parameter = parameters[i]; String[] defaultValue = parameter.getDefaultValue(); if (defaultValue != null) { try { - return parameter.getBinding().bind( - parameter, new StringArgumentStack( - context, defaultValue, false), false); + return parameter.getBinding().bind(parameter, new StringArgumentStack(context, defaultValue, false), false); } catch (MissingParameterException e) { throw new ParametricException( "The default value of the parameter using the binding " + @@ -399,8 +392,7 @@ class ParametricCallable implements CommandCallable { * @param scoped the argument scope * @throws UnconsumedParameterException thrown if parameters were not consumed */ - private void checkUnconsumed(ContextArgumentStack scoped) - throws UnconsumedParameterException { + private void checkUnconsumed(ContextArgumentStack scoped) throws UnconsumedParameterException { CommandContext context = scoped.getContext(); String unconsumed; String unconsumedFlags = getUnusedFlags(context); From ede28df487e82ec1f2d64fba3a239563ddb08301 Mon Sep 17 00:00:00 2001 From: sk89q Date: Tue, 1 Jul 2014 14:15:19 -0700 Subject: [PATCH 2/6] Fixed handling of legacy Command.flags/.anyFlags. Fixes WORLDEDIT-3137. --- .../parametric/ParametricCallable.java | 59 +++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java b/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java index eb4bc8d1a..cd59cbfb6 100644 --- a/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java +++ b/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java @@ -19,6 +19,7 @@ package com.sk89q.worldedit.util.command.parametric; +import com.google.common.primitives.Chars; import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandException; @@ -55,6 +56,8 @@ class ParametricCallable implements CommandCallable { private final Method method; private final ParameterData[] parameters; private final Set valueFlags = new HashSet(); + private final boolean anyFlags; + private final Set legacyFlags = new HashSet(); private final SimpleDescription description = new SimpleDescription(); private final CommandPermissions commandPermissions; @@ -157,6 +160,10 @@ class ParametricCallable implements CommandCallable { } } + // Gather legacy flags + anyFlags = definition.anyFlags(); + legacyFlags.addAll(Chars.asList(definition.flags().toCharArray())); + // Finish description description.setDescription(!definition.desc().isEmpty() ? definition.desc() : null); description.setHelp(!definition.help().isEmpty() ? definition.help() : null); @@ -412,35 +419,41 @@ class ParametricCallable implements CommandCallable { * @param context the command context */ private String getUnusedFlags(CommandContext context) { - Set unusedFlags = null; - for (char flag : context.getFlags()) { - boolean found = false; + if (!anyFlags) { + Set unusedFlags = null; + for (char flag : context.getFlags()) { + boolean found = false; - for (ParameterData parameter : parameters) { - Character paramFlag = parameter.getFlag(); - if (paramFlag != null && flag == paramFlag) { - found = true; + if (legacyFlags.contains(flag)) { break; } - } - - if (!found) { - if (unusedFlags == null) { - unusedFlags = new HashSet(); + + for (ParameterData parameter : parameters) { + Character paramFlag = parameter.getFlag(); + if (paramFlag != null && flag == paramFlag) { + found = true; + break; + } } - unusedFlags.add(flag); + + if (!found) { + if (unusedFlags == null) { + unusedFlags = new HashSet(); + } + unusedFlags.add(flag); + } + } + + if (unusedFlags != null) { + StringBuilder builder = new StringBuilder(); + for (Character flag : unusedFlags) { + builder.append("-").append(flag).append(" "); + } + + return builder.toString().trim(); } } - - if (unusedFlags != null) { - StringBuilder builder = new StringBuilder(); - for (Character flag : unusedFlags) { - builder.append("-").append(flag).append(" "); - } - - return builder.toString().trim(); - } - + return null; } From 8f8d991ee1d75f4d46e1972157015eb97bc0daf8 Mon Sep 17 00:00:00 2001 From: sk89q Date: Tue, 1 Jul 2014 15:04:30 -0700 Subject: [PATCH 3/6] Fixed /butcher not working correctly in-game. @Optional does not work on parameter bindings of the PROVIDES type at this time. Fixes WORLDEDIT-3139. --- .../java/com/sk89q/worldedit/command/UtilityCommands.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java b/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java index 7f596c8e3..d57bedd28 100644 --- a/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java +++ b/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java @@ -375,9 +375,9 @@ public class UtilityCommands { ) @CommandPermissions("worldedit.butcher") @Logging(PLACEMENT) - public void butcher(Actor actor, @Optional Player player, @Optional LocalSession session, CommandContext args) throws WorldEditException { - + public void butcher(Actor actor, CommandContext args) throws WorldEditException { LocalConfiguration config = we.getConfiguration(); + Player player = actor instanceof Player ? (Player) actor : null; // technically the default can be larger than the max, but that's not my problem int radius = config.butcherDefaultRadius; @@ -407,6 +407,7 @@ public class UtilityCommands { int killed; if (player != null) { + LocalSession session = we.getSessionManager().get(player); killed = player.getWorld().killMobs(session.getPlacementPosition(player), radius, flags.flags); } else { killed = 0; From e207ccebe8baad919cb947602562a22d2074b7e4 Mon Sep 17 00:00:00 2001 From: sk89q Date: Tue, 1 Jul 2014 15:16:27 -0700 Subject: [PATCH 4/6] Fixed activated hoppers rotating incorrectly. Fixes WORLDEDIT-3138. --- .../com/sk89q/worldedit/blocks/BlockData.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/blocks/BlockData.java b/src/main/java/com/sk89q/worldedit/blocks/BlockData.java index fed187ed9..3d78435a1 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/BlockData.java +++ b/src/main/java/com/sk89q/worldedit/blocks/BlockData.java @@ -124,7 +124,7 @@ public final class BlockData { /* FALL-THROUGH */ case BlockID.COCOA_PLANT: - case BlockID.TRIPWIRE_HOOK: + case BlockID.TRIPWIRE_HOOK: { int extra = data & ~0x3; int withoutFlags = data & 0x3; switch (withoutFlags) { @@ -134,7 +134,7 @@ public final class BlockData { case 3: return 0 | extra; } break; - + } case BlockID.SIGN_POST: return (data + 4) % 16; @@ -145,15 +145,17 @@ public final class BlockData { case BlockID.BURNING_FURNACE: case BlockID.ENDER_CHEST: case BlockID.TRAPPED_CHEST: - case BlockID.HOPPER: - switch (data) { - case 2: return 5; - case 3: return 4; - case 4: return 2; - case 5: return 3; + case BlockID.HOPPER: { + int extra = data & 0x8; + int withoutFlags = data & ~0x8; + switch (withoutFlags) { + case 2: return 5 | extra; + case 3: return 4 | extra; + case 4: return 2 | extra; + case 5: return 3 | extra; } break; - + } case BlockID.DISPENSER: case BlockID.DROPPER: int dispPower = data & 0x8; From 06c248e518fdfd59c2db81f000e29ca26aab2cae Mon Sep 17 00:00:00 2001 From: sk89q Date: Tue, 1 Jul 2014 15:37:12 -0700 Subject: [PATCH 5/6] Rectified that hopper fix being very incomplete (and unit test breaking!). --- .../com/sk89q/worldedit/blocks/BlockData.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/blocks/BlockData.java b/src/main/java/com/sk89q/worldedit/blocks/BlockData.java index 3d78435a1..815ef38c9 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/BlockData.java +++ b/src/main/java/com/sk89q/worldedit/blocks/BlockData.java @@ -345,7 +345,7 @@ public final class BlockData { /* FALL-THROUGH */ case BlockID.COCOA_PLANT: - case BlockID.TRIPWIRE_HOOK: + case BlockID.TRIPWIRE_HOOK: { int extra = data & ~0x3; int withoutFlags = data & 0x3; switch (withoutFlags) { @@ -355,7 +355,7 @@ public final class BlockData { case 0: return 3 | extra; } break; - + } case BlockID.SIGN_POST: return (data + 12) % 16; @@ -366,15 +366,17 @@ public final class BlockData { case BlockID.BURNING_FURNACE: case BlockID.ENDER_CHEST: case BlockID.TRAPPED_CHEST: - case BlockID.HOPPER: - switch (data) { - case 5: return 2; - case 4: return 3; - case 2: return 4; - case 3: return 5; + case BlockID.HOPPER: { + int extra = data & 0x8; + int withoutFlags = data & ~0x8; + switch (withoutFlags) { + case 5: return 2 | extra; + case 4: return 3 | extra; + case 2: return 4 | extra; + case 3: return 5 | extra; } break; - + } case BlockID.DISPENSER: case BlockID.DROPPER: int dispPower = data & 0x8; @@ -617,13 +619,15 @@ public final class BlockData { case BlockID.ENDER_CHEST: case BlockID.TRAPPED_CHEST: case BlockID.HOPPER: - switch (data) { + int extra = data & 0x8; + int withoutFlags = data & ~0x8; + switch (withoutFlags) { case 2: case 3: - return data ^ flipZ; + return (data ^ flipZ) | extra; case 4: case 5: - return data ^ flipX; + return (data ^ flipX) | extra; } break; @@ -912,8 +916,10 @@ public final class BlockData { case BlockID.ENDER_CHEST: case BlockID.TRAPPED_CHEST: case BlockID.HOPPER: - if (data < 2 || data > 5) return -1; - return mod((data - 2 + increment), 4) + 2; + int extra = data & 0x8; + int withoutFlags = data & ~0x8; + if (withoutFlags < 2 || withoutFlags > 5) return -1; + return (mod((withoutFlags - 2 + increment), 4) + 2) | extra; case BlockID.DISPENSER: case BlockID.DROPPER: From 765d987ae14baaf8e1b95ac7ada4850a3d6b4ddb Mon Sep 17 00:00:00 2001 From: sk89q Date: Wed, 2 Jul 2014 12:52:42 -0700 Subject: [PATCH 6/6] ParametricCallable.mayConsumeArguments() handles flags better. --- .../parametric/ParametricCallable.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java b/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java index cd59cbfb6..a96434ac0 100644 --- a/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java +++ b/src/main/java/com/sk89q/worldedit/util/command/parametric/ParametricCallable.java @@ -344,20 +344,24 @@ class ParametricCallable implements CommandCallable { // Optional non-flag parameters: // - Before required parameters: Consume if there are 'left over' args // - At the end: Always consumes - - if (parameter.isOptional() && parameter.getFlag() == null) { - int numberFree = context.argsLength() - scoped.position(); - for (int j = i; j < parameters.length; j++) { - if (parameters[j].isNonFlagConsumer() && !parameters[j].isOptional()) { - // We already checked if the consumed count was > -1 - // when we created this object - numberFree -= parameters[j].getConsumedCount(); + + if (parameter.isOptional()) { + if (parameter.getFlag() != null) { + return !parameter.isValueFlag() || context.hasFlag(parameter.getFlag()); + } else { + int numberFree = context.argsLength() - scoped.position(); + for (int j = i; j < parameters.length; j++) { + if (parameters[j].isNonFlagConsumer() && !parameters[j].isOptional()) { + // We already checked if the consumed count was > -1 + // when we created this object + numberFree -= parameters[j].getConsumedCount(); + } + } + + // Skip this optional parameter + if (numberFree < 1) { + return false; } - } - - // Skip this optional parameter - if (numberFree < 1) { - return false; } }