From e32bc6db4a91e21227c4f43c6baf5dfd6c7b4d4e Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Wed, 23 Oct 2019 15:44:13 +0100 Subject: [PATCH] Use enum type for scoll actions --- .../fawe/object/brush/BrushSettings.java | 10 ++-- .../scroll/{ScrollAction.java => Scroll.java} | 59 +++++++++++-------- .../object/brush/scroll/ScrollClipboard.java | 2 +- .../fawe/object/brush/scroll/ScrollMask.java | 2 +- .../object/brush/scroll/ScrollPattern.java | 2 +- .../fawe/object/brush/scroll/ScrollRange.java | 2 +- .../fawe/object/brush/scroll/ScrollSize.java | 2 +- .../object/brush/scroll/ScrollTarget.java | 2 +- .../brush/scroll/ScrollTargetOffset.java | 4 +- .../sk89q/worldedit/command/ToolCommands.java | 21 ------- .../worldedit/command/ToolUtilCommands.java | 17 +++--- .../command/argument/EnumConverter.java | 5 +- .../worldedit/command/tool/BrushTool.java | 6 +- 13 files changed, 62 insertions(+), 72 deletions(-) rename worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/{ScrollAction.java => Scroll.java} (67%) diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/BrushSettings.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/BrushSettings.java index 8b6b3f066..6e855f10b 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/BrushSettings.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/BrushSettings.java @@ -3,7 +3,7 @@ package com.boydti.fawe.object.brush; import static com.google.common.base.Preconditions.checkNotNull; import com.boydti.fawe.Fawe; -import com.boydti.fawe.object.brush.scroll.ScrollAction; +import com.boydti.fawe.object.brush.scroll.Scroll; import com.boydti.fawe.object.extent.ResettableExtent; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; @@ -48,7 +48,7 @@ public class BrushSettings { private Pattern material; private Expression size = new Expression(1); private Set permissions; - private ScrollAction scrollAction; + private Scroll scrollAction; private String lastWorld; public BrushSettings() { @@ -107,7 +107,7 @@ public class BrushSettings { } if (settings.containsKey(SettingType.SCROLL_ACTION.name())) { String actionArgs = (String) settings.get(SettingType.SCROLL_ACTION.name()); - ScrollAction action = ScrollAction.fromArguments(tool, player, session, actionArgs, false); + Scroll action = Scroll.fromArguments(tool, player, session, actionArgs, false); if (action != null) { bs.setScrollAction(action); bs.constructor.put(SettingType.SCROLL_ACTION, actionArgs); @@ -194,7 +194,7 @@ public class BrushSettings { return setSize(new Expression(size)); } - public BrushSettings setScrollAction(ScrollAction scrollAction) { + public BrushSettings setScrollAction(Scroll scrollAction) { if (scrollAction == null) constructor.remove(SettingType.SCROLL_ACTION); this.scrollAction = scrollAction; return this; @@ -260,7 +260,7 @@ public class BrushSettings { return Collections.unmodifiableSet(permissions); } - public ScrollAction getScrollAction() { + public Scroll getScrollAction() { return scrollAction; } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollAction.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/Scroll.java similarity index 67% rename from worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollAction.java rename to worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/Scroll.java index 1602ee08d..684fb817a 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollAction.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/Scroll.java @@ -2,37 +2,46 @@ package com.boydti.fawe.object.brush.scroll; import com.boydti.fawe.config.BBC; import com.boydti.fawe.object.clipboard.MultiClipboardHolder; -import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.ParserContext; -import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.pattern.Pattern; import java.io.IOException; +import java.util.List; -public abstract class ScrollAction implements ScrollTool { +public abstract class Scroll implements ScrollTool { private BrushTool tool; - public static ScrollAction fromArguments(BrushTool tool, Player player, LocalSession session, String arguments, boolean message) throws InputParseException { + public enum Action { + NONE, + CLIPBOARD, + MASK, + PATTERN, + TARGET_OFFSET, + RANGE, + SIZE, + TARGET + } + + public static com.boydti.fawe.object.brush.scroll.Scroll fromArguments(BrushTool tool, Player player, LocalSession session, Action mode, List arguments, boolean message) throws InputParseException { ParserContext parserContext = new ParserContext(); parserContext.setActor(player); parserContext.setWorld(player.getWorld()); parserContext.setSession(session); - String[] split = arguments.split(" "); - switch (split[0].toLowerCase()) { - case "none": + switch (mode) { + case NONE: return null; - case "clipboard": - if (split.length != 2) { + case CLIPBOARD: + if (arguments.size() != 2) { if (message) BBC.COMMAND_SYNTAX.send(player, "clipboard [file]"); return null; } - String filename = split[1]; + String filename = arguments.get(1); try { MultiClipboardHolder multi = ClipboardFormats.loadAllFromInput(player, filename, null, message); if (multi == null) { @@ -42,35 +51,35 @@ public abstract class ScrollAction implements ScrollTool { } catch (IOException e) { throw new RuntimeException(e); } - case "mask": - if (split.length < 2) { + case MASK: + if (arguments.size() < 2) { if (message) BBC.COMMAND_SYNTAX.send(player, "mask [mask 1] [mask 2] [mask 3]..."); return null; } - Mask[] masks = new Mask[split.length - 1]; - for (int i = 1; i < split.length; i++) { - String arg = split[i]; + Mask[] masks = new Mask[arguments.size() - 1]; + for (int i = 1; i < arguments.size(); i++) { + String arg = arguments.get(i); masks[i - 1] = WorldEdit.getInstance().getMaskFactory().parseFromInput(arg, parserContext); } return (new ScrollMask(tool, masks)); - case "pattern": - if (split.length < 2) { + case PATTERN: + if (arguments.size() < 2) { if (message) BBC.COMMAND_SYNTAX.send(player, "pattern [pattern 1] [pattern 2] [pattern 3]..."); return null; } - Pattern[] patterns = new Pattern[split.length - 1]; - for (int i = 1; i < split.length; i++) { - String arg = split[i]; + Pattern[] patterns = new Pattern[arguments.size() - 1]; + for (int i = 1; i < arguments.size(); i++) { + String arg = arguments.get(i); patterns[i - 1] = WorldEdit.getInstance().getPatternFactory().parseFromInput(arg, parserContext); } return (new ScrollPattern(tool, patterns)); - case "targetoffset": + case TARGET_OFFSET: return (new ScrollTargetOffset(tool)); - case "range": + case RANGE: return (new ScrollRange(tool)); - case "size": + case SIZE: return (new ScrollSize(tool)); - case "target": + case TARGET: return (new ScrollTarget(tool)); default: return null; @@ -78,7 +87,7 @@ public abstract class ScrollAction implements ScrollTool { } } - public ScrollAction(BrushTool tool) { + public Scroll(BrushTool tool) { this.tool = tool; } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollClipboard.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollClipboard.java index 1c381feeb..229a932f4 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollClipboard.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollClipboard.java @@ -7,7 +7,7 @@ import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.session.ClipboardHolder; import java.util.List; -public class ScrollClipboard extends ScrollAction { +public class ScrollClipboard extends Scroll { private final List clipboards; private final LocalSession session; private int index = 0; diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollMask.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollMask.java index 2f97c61c9..aba5bf42b 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollMask.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollMask.java @@ -5,7 +5,7 @@ import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.function.mask.Mask; -public class ScrollMask extends ScrollAction { +public class ScrollMask extends Scroll { private final Mask[] masks; private int index; diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollPattern.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollPattern.java index 61e348571..36bfd5cc4 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollPattern.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollPattern.java @@ -5,7 +5,7 @@ import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.function.pattern.Pattern; -public class ScrollPattern extends ScrollAction { +public class ScrollPattern extends Scroll { private final Pattern[] patterns; private int index; diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollRange.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollRange.java index d673a3c11..1ef919946 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollRange.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollRange.java @@ -5,7 +5,7 @@ import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.entity.Player; -public class ScrollRange extends ScrollAction { +public class ScrollRange extends Scroll { public ScrollRange(BrushTool tool) { super(tool); } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollSize.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollSize.java index 2ebe13587..528bb8e1f 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollSize.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollSize.java @@ -4,7 +4,7 @@ import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.entity.Player; -public class ScrollSize extends ScrollAction { +public class ScrollSize extends Scroll { public ScrollSize(BrushTool tool) { super(tool); } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollTarget.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollTarget.java index d4771b1be..68d7ceb56 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollTarget.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollTarget.java @@ -5,7 +5,7 @@ import com.boydti.fawe.util.MathMan; import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.entity.Player; -public class ScrollTarget extends ScrollAction { +public class ScrollTarget extends Scroll { public ScrollTarget(BrushTool tool) { super(tool); } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollTargetOffset.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollTargetOffset.java index 09a864e57..2a07431ed 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollTargetOffset.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/scroll/ScrollTargetOffset.java @@ -1,11 +1,9 @@ package com.boydti.fawe.object.brush.scroll; -import com.boydti.fawe.util.MathMan; -import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.entity.Player; -public class ScrollTargetOffset extends ScrollAction { +public class ScrollTargetOffset extends Scroll { public ScrollTargetOffset(BrushTool tool) { super(tool); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolCommands.java index 09125c666..52f7f0c9e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolCommands.java @@ -20,26 +20,14 @@ package com.sk89q.worldedit.command; import com.boydti.fawe.config.BBC; -import com.boydti.fawe.object.brush.BrushSettings; import com.boydti.fawe.object.brush.InspectBrush; -import com.boydti.fawe.object.brush.TargetMode; -import com.boydti.fawe.object.brush.scroll.ScrollAction; -import com.boydti.fawe.object.brush.visualization.VisualMode; -import com.boydti.fawe.object.extent.ResettableExtent; -import com.boydti.fawe.util.MathMan; -import com.boydti.fawe.util.StringMan; -import com.google.common.collect.Iterables; -import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; -import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItemStack; -import com.sk89q.worldedit.command.argument.Arguments; import com.sk89q.worldedit.command.tool.BlockDataCyler; import com.sk89q.worldedit.command.tool.BlockReplacer; -import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.command.tool.DistanceWand; import com.sk89q.worldedit.command.tool.FloatingTreeRemover; import com.sk89q.worldedit.command.tool.FloodFillTool; @@ -51,22 +39,13 @@ import com.sk89q.worldedit.command.tool.TreePlanter; import com.sk89q.worldedit.command.util.CommandPermissions; import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; import com.sk89q.worldedit.entity.Player; -import com.sk89q.worldedit.event.platform.CommandEvent; -import com.sk89q.worldedit.extension.platform.PlatformCommandManager; -import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.pattern.Pattern; -import com.sk89q.worldedit.internal.annotation.Range; -import com.sk89q.worldedit.internal.command.CommandArgParser; -import com.sk89q.worldedit.internal.expression.Expression; import com.sk89q.worldedit.util.HandSide; import com.sk89q.worldedit.util.TreeGenerator; import com.sk89q.worldedit.world.item.ItemType; import org.enginehub.piston.annotation.Command; import org.enginehub.piston.annotation.CommandContainer; import org.enginehub.piston.annotation.param.Arg; -import org.enginehub.piston.annotation.param.Switch; - -import java.util.List; @CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class) public class ToolCommands { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolUtilCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolUtilCommands.java index f25a1244f..e1ea79b6d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolUtilCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolUtilCommands.java @@ -22,7 +22,7 @@ package com.sk89q.worldedit.command; import com.boydti.fawe.config.BBC; import com.boydti.fawe.object.brush.BrushSettings; import com.boydti.fawe.object.brush.TargetMode; -import com.boydti.fawe.object.brush.scroll.ScrollAction; +import com.boydti.fawe.object.brush.scroll.Scroll; import com.boydti.fawe.object.brush.visualization.VisualMode; import com.boydti.fawe.object.extent.ResettableExtent; import com.boydti.fawe.util.MathMan; @@ -326,24 +326,25 @@ public class ToolUtilCommands { public void scroll(Player player, EditSession editSession, LocalSession session, @Switch(name = 'h', desc = "TODO") boolean offHand, - @Arg(desc = "Target Modes") - String modes, + @Arg(desc = "Target Modes", def = "none") + Scroll.Action mode, @Arg(desc = "The scroll action", variable = true) List commandStr) throws WorldEditException { - // TODO NOT IMPLEMENTED Convert ScrollAction to an argument converter BrushTool bt = session.getBrushTool(player, false); if (bt == null) { BBC.BRUSH_NONE.send(player); return; } + BrushSettings settings = offHand ? bt.getOffHand() : bt.getContext(); - ScrollAction action = ScrollAction.fromArguments(bt, player, session, StringMan.join(commandStr, " "), true); + Scroll action = Scroll.fromArguments(bt, player, session, mode, commandStr, true); settings.setScrollAction(action); - if (modes.equalsIgnoreCase("none")) { + if (mode == Scroll.Action.NONE) { BBC.BRUSH_SCROLL_ACTION_UNSET.send(player); } else if (action != null) { - settings.addSetting(BrushSettings.SettingType.SCROLL_ACTION, modes); - BBC.BRUSH_SCROLL_ACTION_SET.send(player, modes); + String full = (mode.name().toLowerCase() + " " + StringMan.join(commandStr, " ")).trim(); + settings.addSetting(BrushSettings.SettingType.SCROLL_ACTION, full); + BBC.BRUSH_SCROLL_ACTION_SET.send(player, mode); } bt.update(); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/EnumConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/EnumConverter.java index d1268e466..0ba67a994 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/EnumConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/EnumConverter.java @@ -19,6 +19,7 @@ package com.sk89q.worldedit.command.argument; +import com.boydti.fawe.object.brush.scroll.Scroll; import com.google.common.collect.ImmutableSet; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.command.util.HookMode; @@ -48,7 +49,9 @@ public final class EnumConverter { r -> ImmutableSet.of(r.getDisplayName()), null)); commandManager.registerConverter(Key.of(HookMode.class), - basic(HookMode.class)); + basic(HookMode.class)); + commandManager.registerConverter(Key.of(Scroll.Action.class), + basic(Scroll.Action.class)); } private static > ArgumentConverter basic(Class enumClass) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java index 744a99965..bfb6611b9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java @@ -28,7 +28,7 @@ import com.boydti.fawe.object.brush.BrushSettings; import com.boydti.fawe.object.brush.MovableTool; import com.boydti.fawe.object.brush.ResettableTool; import com.boydti.fawe.object.brush.TargetMode; -import com.boydti.fawe.object.brush.scroll.ScrollAction; +import com.boydti.fawe.object.brush.scroll.Scroll; import com.boydti.fawe.object.brush.scroll.ScrollTool; import com.boydti.fawe.object.brush.visualization.VisualExtent; import com.boydti.fawe.object.brush.visualization.VisualMode; @@ -555,7 +555,7 @@ public class BrushTool implements DoubleActionTraceTool, ScrollTool, MovableTool - public void setScrollAction(ScrollAction scrollAction) { + public void setScrollAction(Scroll scrollAction) { this.getContext().setScrollAction(scrollAction); update(); } @@ -603,7 +603,7 @@ public class BrushTool implements DoubleActionTraceTool, ScrollTool, MovableTool @Override public boolean increment(Player player, int amount) { BrushSettings current = getContext(); - ScrollAction tmp = current.getScrollAction(); + Scroll tmp = current.getScrollAction(); if (tmp != null) { tmp.setTool(this); if (tmp.increment(player, amount)) {