From b8c120e0c409d49f3e716ea30ecf3d69c3efc6dd Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Fri, 26 Apr 2019 01:18:02 -0700 Subject: [PATCH] Finish porting commands --- .../worldedit/bukkit/WorldEditListener.java | 4 +- .../worldedit/command/ApplyBrushCommands.java | 113 +++++++++++++++++ .../worldedit/command/BrushCommands.java | 114 ++++++++++++++++- .../worldedit/command/PaintBrushCommands.java | 119 ++++++++++++++++++ .../worldedit/command/WorldEditCommands.java | 2 +- ...rnConverter.java => FactoryConverter.java} | 28 ++++- .../command/argument/ItemParser.java | 82 ------------ .../command/argument/ItemUseParser.java | 90 ------------- .../command/argument/MaskConverter.java | 78 ------------ .../argument/RegionFactoryConverter.java | 72 +++++++++++ .../argument/RegionFunctionParser.java | 40 ------ .../command/argument/ReplaceParser.java | 78 ------------ .../command/composition/ApplyCommand.java | 68 ---------- .../command/composition/PaintCommand.java | 64 ---------- .../command/composition/SelectionCommand.java | 2 +- .../composition/ShapedBrushCommand.java | 2 +- .../command/factory/ItemUseFactory.java | 28 +++++ .../command/factory/ReplaceFactory.java | 30 +++++ .../command/factory/TreeGeneratorFactory.java | 25 ++++ .../command/util/PermissionCondition.java | 2 +- .../command/util/PrintCommandHelp.java | 2 +- .../extension/platform/Capability.java | 4 +- ...anger.java => PlatformCommandManager.java} | 88 +++++-------- .../extension/platform/PlatformManager.java | 8 +- .../worldedit/function/ItemUseFunction.java | 23 ++++ .../worldedit/util/command/CommandUtil.java | 33 ++++- 26 files changed, 618 insertions(+), 581 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/ApplyBrushCommands.java create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/PaintBrushCommands.java rename worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/{PatternConverter.java => FactoryConverter.java} (67%) delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ItemParser.java delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ItemUseParser.java delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/MaskConverter.java create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegionFactoryConverter.java delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegionFunctionParser.java delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ReplaceParser.java delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/ApplyCommand.java delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/PaintCommand.java create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ItemUseFactory.java create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ReplaceFactory.java create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/TreeGeneratorFactory.java rename worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/{PlatformCommandMananger.java => PlatformCommandManager.java} (86%) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/function/ItemUseFunction.java diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java index 699263d50..7eea4312b 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java @@ -91,7 +91,7 @@ public class WorldEditListener implements Listener { if (split.length > 0) { split[0] = split[0].substring(1); - split = plugin.getWorldEdit().getPlatformManager().getPlatformCommandMananger().commandDetection(split); + split = plugin.getWorldEdit().getPlatformManager().getPlatformCommandManager().commandDetection(split); } final String newMessage = "/" + StringUtil.joinString(split, " "); @@ -118,7 +118,7 @@ public class WorldEditListener implements Listener { CommandParameters parameters = NoInputCommandParameters.builder() .injectedValues(MemoizingValueAccess.wrap(store)) .build(); - CommandManager commandManager = plugin.getWorldEdit().getPlatformManager().getPlatformCommandMananger().getCommandManager(); + CommandManager commandManager = plugin.getWorldEdit().getPlatformManager().getPlatformCommandManager().getCommandManager(); event.getCommands().removeIf(name -> // remove if in the manager and not satisfied commandManager.getCommand(name) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ApplyBrushCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ApplyBrushCommands.java new file mode 100644 index 000000000..2e9297b24 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ApplyBrushCommands.java @@ -0,0 +1,113 @@ +package com.sk89q.worldedit.command; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.sk89q.worldedit.LocalSession; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.blocks.BaseItem; +import com.sk89q.worldedit.command.factory.TreeGeneratorFactory; +import com.sk89q.worldedit.command.factory.ItemUseFactory; +import com.sk89q.worldedit.command.factory.ReplaceFactory; +import com.sk89q.worldedit.command.util.PermissionCondition; +import com.sk89q.worldedit.entity.Player; +import com.sk89q.worldedit.function.Contextual; +import com.sk89q.worldedit.function.RegionFunction; +import com.sk89q.worldedit.function.factory.Apply; +import com.sk89q.worldedit.function.pattern.Pattern; +import com.sk89q.worldedit.regions.factory.RegionFactory; +import com.sk89q.worldedit.util.TreeGenerator; +import com.sk89q.worldedit.util.command.CommandUtil; +import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import org.enginehub.piston.CommandManager; +import org.enginehub.piston.CommandParameters; +import org.enginehub.piston.DefaultCommandManagerService; +import org.enginehub.piston.annotation.Command; +import org.enginehub.piston.annotation.CommandContainer; +import org.enginehub.piston.annotation.param.Arg; +import org.enginehub.piston.inject.Key; +import org.enginehub.piston.part.CommandArgument; +import org.enginehub.piston.part.SubCommandPart; + +import java.util.stream.Collectors; + +import static java.util.Objects.requireNonNull; +import static org.enginehub.piston.part.CommandParts.arg; + +@CommandContainer +public class ApplyBrushCommands { + + private static final CommandArgument REGION_FACTORY = arg(TranslatableComponent.of("regionFactory"), TextComponent.of("The shape of the region")) + .defaultsTo(ImmutableList.of()) + .ofTypes(ImmutableList.of(Key.of(RegionFactory.class))) + .build(); + + private static final CommandArgument RADIUS = arg(TranslatableComponent.of("radius"), TextComponent.of("The size of the brush")) + .defaultsTo(ImmutableList.of("5")) + .ofTypes(ImmutableList.of(Key.of(double.class))) + .build(); + + public static void register(CommandManager commandManager) { + commandManager.register("apply", builder -> { + builder.description(TextComponent.of("Apply brush, apply a function to every block")); + builder.action(org.enginehub.piston.Command.Action.NULL_ACTION); + + CommandManager manager = DefaultCommandManagerService.getInstance() + .newCommandManager(); + CommandUtil.register( + manager, + ApplyBrushCommandsRegistration.builder(), + new ApplyBrushCommands() + ); + + builder.condition(new PermissionCondition(ImmutableSet.of("worldedit.brush.apply"))); + + builder.addParts(REGION_FACTORY, RADIUS); + builder.addPart(SubCommandPart.builder(TranslatableComponent.of("type"), TextComponent.of("Type of brush to use")) + .withCommands(manager.getAllCommands().collect(Collectors.toList())) + .build()); + }); + } + + private void setApplyBrush(CommandParameters parameters, Player player, LocalSession localSession, + Contextual generatorFactory) throws WorldEditException { + double radius = requireNonNull(RADIUS.value(parameters).asSingle(double.class)); + RegionFactory regionFactory = REGION_FACTORY.value(parameters).asSingle(RegionFactory.class); + BrushCommands.setOperationBasedBrush(player, localSession, radius, + new Apply(generatorFactory), regionFactory, "worldedit.brush.apply"); + } + + @Command( + name = "forest", + desc = "Plant trees" + ) + public void forest(CommandParameters parameters, + Player player, LocalSession localSession, + @Arg(desc = "The type of tree to plant") + TreeGenerator.TreeType type) throws WorldEditException { + setApplyBrush(parameters, player, localSession, new TreeGeneratorFactory(type)); + } + + @Command( + name = "item", + desc = "Use an item" + ) + public void item(CommandParameters parameters, + Player player, LocalSession localSession, + @Arg(desc = "The type of item to use") + BaseItem item) throws WorldEditException { + setApplyBrush(parameters, player, localSession, new ItemUseFactory(item)); + } + + @Command( + name = "set", + desc = "Place a block" + ) + public void set(CommandParameters parameters, + Player player, LocalSession localSession, + @Arg(desc = "The pattern of blocks to use") + Pattern pattern) throws WorldEditException { + setApplyBrush(parameters, player, localSession, new ReplaceFactory(pattern)); + } + +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java index e310f136a..ebc2e9e5a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java @@ -23,6 +23,8 @@ import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.command.factory.ReplaceFactory; +import com.sk89q.worldedit.command.factory.TreeGeneratorFactory; import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.command.tool.brush.ButcherBrush; import com.sk89q.worldedit.command.tool.brush.ClipboardBrush; @@ -30,6 +32,7 @@ import com.sk89q.worldedit.command.tool.brush.CylinderBrush; import com.sk89q.worldedit.command.tool.brush.GravityBrush; import com.sk89q.worldedit.command.tool.brush.HollowCylinderBrush; import com.sk89q.worldedit.command.tool.brush.HollowSphereBrush; +import com.sk89q.worldedit.command.tool.brush.OperationFactoryBrush; import com.sk89q.worldedit.command.tool.brush.SmoothBrush; import com.sk89q.worldedit.command.tool.brush.SphereBrush; import com.sk89q.worldedit.command.util.CommandPermissions; @@ -37,14 +40,21 @@ import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; import com.sk89q.worldedit.command.util.CreatureButcher; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extent.clipboard.Clipboard; +import com.sk89q.worldedit.function.Contextual; +import com.sk89q.worldedit.function.factory.Apply; +import com.sk89q.worldedit.function.factory.Deform; +import com.sk89q.worldedit.function.factory.Paint; import com.sk89q.worldedit.function.mask.BlockTypeMask; import com.sk89q.worldedit.function.mask.Mask; +import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.pattern.BlockPattern; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.regions.factory.RegionFactory; import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.session.request.RequestExtent; import com.sk89q.worldedit.util.HandSide; +import com.sk89q.worldedit.util.TreeGenerator; import com.sk89q.worldedit.world.block.BlockTypes; import org.enginehub.piston.annotation.Command; import org.enginehub.piston.annotation.CommandContainer; @@ -215,7 +225,7 @@ public class BrushCommands { player.print(String.format("Gravity brush equipped (%.0f).", radius)); } - + @Command( name = "butcher", aliases = { "kill" }, @@ -275,4 +285,106 @@ public class BrushCommands { player.print(String.format("Butcher brush equipped (%.0f).", radius)); } + + @Command( + name = "deform", + desc = "Deform brush, applies an expression to an area" + ) + @CommandPermissions("worldedit.brush.deform") + public void deform(Player player, LocalSession localSession, + @Arg(desc = "The shape of the region") + RegionFactory regionFactory, + @Arg(desc = "The size of the brush", def = "5") + double radius, + @Arg(desc = "Expression to apply", def = "y-=0.2") + String expression, + @Switch(name = 'r', desc = "Use the game's coordinate origin") + boolean useRawCoords, + @Switch(name = 'o', desc = "Use the placement position as the origin") + boolean usePlacement) throws WorldEditException { + Deform deform = new Deform(expression); + if (useRawCoords) { + deform.setMode(Deform.Mode.RAW_COORD); + } else if (usePlacement) { + deform.setMode(Deform.Mode.OFFSET); + deform.setOffset(localSession.getPlacementPosition(player).toVector3()); + } + setOperationBasedBrush(player, localSession, radius, + deform, regionFactory, "worldedit.brush.deform"); + } + + @Command( + name = "set", + desc = "Set brush, sets all blocks in the area" + ) + @CommandPermissions("worldedit.brush.set") + public void set(Player player, LocalSession localSession, + @Arg(desc = "The shape of the region") + RegionFactory regionFactory, + @Arg(desc = "The size of the brush", def = "5") + double radius, + @Arg(desc = "The pattern of blocks to set") + Pattern pattern) throws WorldEditException { + setOperationBasedBrush(player, localSession, radius, + new Apply(new ReplaceFactory(pattern)), regionFactory, "worldedit.brush.set"); + } + + @Command( + name = "forest", + desc = "Forest brush, creates a forest in the area" + ) + @CommandPermissions("worldedit.brush.forest") + public void forest(Player player, LocalSession localSession, + @Arg(desc = "The shape of the region") + RegionFactory regionFactory, + @Arg(desc = "The size of the brush", def = "5") + double radius, + @Arg(desc = "The density of the brush", def = "20") + double density, + @Arg(desc = "The type of tree to use") + TreeGenerator.TreeType type) throws WorldEditException { + setOperationBasedBrush(player, localSession, radius, + new Paint(new TreeGeneratorFactory(type), density / 100), regionFactory, "worldedit.brush.forest"); + } + + @Command( + name = "raise", + desc = "Raise brush, raise all blocks by one" + ) + @CommandPermissions("worldedit.brush.raise") + public void raise(Player player, LocalSession localSession, + @Arg(desc = "The shape of the region") + RegionFactory regionFactory, + @Arg(desc = "The size of the brush", def = "5") + double radius) throws WorldEditException { + setOperationBasedBrush(player, localSession, radius, + new Deform("y-=1"), regionFactory, "worldedit.brush.raise"); + } + + @Command( + name = "lower", + desc = "Lower brush, lower all blocks by one" + ) + @CommandPermissions("worldedit.brush.lower") + public void lower(Player player, LocalSession localSession, + @Arg(desc = "The shape of the region") + RegionFactory regionFactory, + @Arg(desc = "The size of the brush", def = "5") + double radius) throws WorldEditException { + setOperationBasedBrush(player, localSession, radius, + new Deform("y+=1"), regionFactory, "worldedit.brush.lower"); + } + + static void setOperationBasedBrush(Player player, LocalSession session, double radius, + Contextual factory, + RegionFactory regionFactory, + String permission) throws WorldEditException { + WorldEdit.getInstance().checkMaxBrushRadius(radius); + BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + tool.setSize(radius); + tool.setFill(null); + tool.setBrush(new OperationFactoryBrush(factory, regionFactory, session), permission); + + player.print("Set brush to " + factory); + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/PaintBrushCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/PaintBrushCommands.java new file mode 100644 index 000000000..649c2988b --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/PaintBrushCommands.java @@ -0,0 +1,119 @@ +package com.sk89q.worldedit.command; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.sk89q.worldedit.LocalSession; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.blocks.BaseItem; +import com.sk89q.worldedit.command.factory.TreeGeneratorFactory; +import com.sk89q.worldedit.command.factory.ItemUseFactory; +import com.sk89q.worldedit.command.factory.ReplaceFactory; +import com.sk89q.worldedit.command.util.PermissionCondition; +import com.sk89q.worldedit.entity.Player; +import com.sk89q.worldedit.function.Contextual; +import com.sk89q.worldedit.function.RegionFunction; +import com.sk89q.worldedit.function.factory.Paint; +import com.sk89q.worldedit.function.pattern.Pattern; +import com.sk89q.worldedit.regions.factory.RegionFactory; +import com.sk89q.worldedit.util.TreeGenerator; +import com.sk89q.worldedit.util.command.CommandUtil; +import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import org.enginehub.piston.CommandManager; +import org.enginehub.piston.CommandParameters; +import org.enginehub.piston.DefaultCommandManagerService; +import org.enginehub.piston.annotation.Command; +import org.enginehub.piston.annotation.CommandContainer; +import org.enginehub.piston.annotation.param.Arg; +import org.enginehub.piston.inject.Key; +import org.enginehub.piston.part.CommandArgument; +import org.enginehub.piston.part.SubCommandPart; + +import java.util.stream.Collectors; + +import static java.util.Objects.requireNonNull; +import static org.enginehub.piston.part.CommandParts.arg; + +@CommandContainer +public class PaintBrushCommands { + + private static final CommandArgument REGION_FACTORY = arg(TranslatableComponent.of("regionFactory"), TextComponent.of("The shape of the region")) + .defaultsTo(ImmutableList.of()) + .ofTypes(ImmutableList.of(Key.of(RegionFactory.class))) + .build(); + + private static final CommandArgument RADIUS = arg(TranslatableComponent.of("radius"), TextComponent.of("The size of the brush")) + .defaultsTo(ImmutableList.of("5")) + .ofTypes(ImmutableList.of(Key.of(double.class))) + .build(); + + private static final CommandArgument DENSITY = arg(TranslatableComponent.of("density"), TextComponent.of("The density of the brush")) + .defaultsTo(ImmutableList.of("20")) + .ofTypes(ImmutableList.of(Key.of(double.class))) + .build(); + + public static void register(CommandManager commandManager) { + commandManager.register("paint", builder -> { + builder.description(TextComponent.of("Paint brush, apply a function to a surface")); + builder.action(org.enginehub.piston.Command.Action.NULL_ACTION); + + CommandManager manager = DefaultCommandManagerService.getInstance() + .newCommandManager(); + CommandUtil.register( + manager, + PaintBrushCommandsRegistration.builder(), + new PaintBrushCommands() + ); + + builder.condition(new PermissionCondition(ImmutableSet.of("worldedit.brush.paint"))); + + builder.addParts(REGION_FACTORY, RADIUS, DENSITY); + builder.addPart(SubCommandPart.builder(TranslatableComponent.of("type"), TextComponent.of("Type of brush to use")) + .withCommands(manager.getAllCommands().collect(Collectors.toList())) + .build()); + }); + } + + private void setPaintBrush(CommandParameters parameters, Player player, LocalSession localSession, + Contextual generatorFactory) throws WorldEditException { + double radius = requireNonNull(RADIUS.value(parameters).asSingle(double.class)); + double density = requireNonNull(DENSITY.value(parameters).asSingle(double.class)) / 100; + RegionFactory regionFactory = REGION_FACTORY.value(parameters).asSingle(RegionFactory.class); + BrushCommands.setOperationBasedBrush(player, localSession, radius, + new Paint(generatorFactory, density), regionFactory, "worldedit.brush.paint"); + } + + @Command( + name = "forest", + desc = "Plant trees" + ) + public void forest(CommandParameters parameters, + Player player, LocalSession localSession, + @Arg(desc = "The type of tree to plant") + TreeGenerator.TreeType type) throws WorldEditException { + setPaintBrush(parameters, player, localSession, new TreeGeneratorFactory(type)); + } + + @Command( + name = "item", + desc = "Use an item" + ) + public void item(CommandParameters parameters, + Player player, LocalSession localSession, + @Arg(desc = "The type of item to use") + BaseItem item) throws WorldEditException { + setPaintBrush(parameters, player, localSession, new ItemUseFactory(item)); + } + + @Command( + name = "set", + desc = "Place a block" + ) + public void set(CommandParameters parameters, + Player player, LocalSession localSession, + @Arg(desc = "The pattern of blocks to use") + Pattern pattern) throws WorldEditException { + setPaintBrush(parameters, player, localSession, new ReplaceFactory(pattern)); + } + +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java index 1b7346cd0..7586b0109 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java @@ -120,7 +120,7 @@ public class WorldEditCommands { actor.checkPermission("worldedit.report.pastebin"); ActorCallbackPaste.pastebin( we.getSupervisor(), actor, result, "WorldEdit report: %s.report", - WorldEdit.getInstance().getPlatformManager().getPlatformCommandMananger().getExceptionConverter() + WorldEdit.getInstance().getPlatformManager().getPlatformCommandManager().getExceptionConverter() ); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/PatternConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java similarity index 67% rename from worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/PatternConverter.java rename to worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java index 4202b4679..2e7eedb72 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/PatternConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java @@ -21,12 +21,15 @@ package com.sk89q.worldedit.command.argument; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extent.Extent; +import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.pattern.Pattern; +import com.sk89q.worldedit.internal.registry.AbstractFactory; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.world.World; @@ -37,20 +40,33 @@ import org.enginehub.piston.converter.SuccessfulConversion; import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.Key; -public class PatternConverter implements ArgumentConverter { +import java.util.function.Function; + +public class FactoryConverter implements ArgumentConverter { public static void register(WorldEdit worldEdit, CommandManager commandManager) { - commandManager.registerConverter(Key.of(Pattern.class), new PatternConverter(worldEdit)); + commandManager.registerConverter(Key.of(Pattern.class), + new FactoryConverter<>(worldEdit, WorldEdit::getPatternFactory, "pattern")); + commandManager.registerConverter(Key.of(Mask.class), + new FactoryConverter<>(worldEdit, WorldEdit::getMaskFactory, "mask")); + commandManager.registerConverter(Key.of(BaseItem.class), + new FactoryConverter<>(worldEdit, WorldEdit::getItemFactory, "item")); } private final WorldEdit worldEdit; + private final Function> factoryExtractor; + private final String description; - private PatternConverter(WorldEdit worldEdit) { + private FactoryConverter(WorldEdit worldEdit, + Function> factoryExtractor, + String description) { this.worldEdit = worldEdit; + this.factoryExtractor = factoryExtractor; + this.description = description; } @Override - public ConversionResult convert(String argument, InjectedValueAccess context) { + public ConversionResult convert(String argument, InjectedValueAccess context) { Actor actor = context.injectedValue(Key.of(Actor.class)) .orElseThrow(() -> new IllegalStateException("No actor")); LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor); @@ -67,7 +83,7 @@ public class PatternConverter implements ArgumentConverter { try { return SuccessfulConversion.fromSingle( - worldEdit.getPatternFactory().parseFromInput(argument, parserContext) + factoryExtractor.apply(worldEdit).parseFromInput(argument, parserContext) ); } catch (InputParseException e) { throw new IllegalArgumentException(e); @@ -76,6 +92,6 @@ public class PatternConverter implements ArgumentConverter { @Override public Component describeAcceptableArguments() { - return TextComponent.of("any pattern"); + return TextComponent.of("any " + description); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ItemParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ItemParser.java deleted file mode 100644 index 880c17fb8..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ItemParser.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * Copyright (C) WorldEdit team and contributors - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser 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 Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.sk89q.worldedit.command.argument; - -import com.sk89q.minecraft.util.commands.CommandException; -import com.sk89q.minecraft.util.commands.CommandLocals; -import com.sk89q.worldedit.LocalSession; -import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.blocks.BaseItem; -import com.sk89q.worldedit.entity.Entity; -import com.sk89q.worldedit.extension.input.InputParseException; -import com.sk89q.worldedit.extension.input.ParserContext; -import com.sk89q.worldedit.extension.platform.Actor; -import com.sk89q.worldedit.extent.Extent; -import com.sk89q.worldedit.util.command.argument.CommandArgs; -import com.sk89q.worldedit.util.command.composition.SimpleCommand; -import com.sk89q.worldedit.world.World; - -public class ItemParser extends SimpleCommand { - - private final StringParser stringParser; - - public ItemParser(String name) { - stringParser = addParameter(new StringParser(name, "The item name", null)); - } - - public ItemParser(String name, String defaultSuggestion) { - stringParser = addParameter(new StringParser(name, "The item name", defaultSuggestion)); - } - - @Override - public BaseItem call(CommandArgs args, CommandLocals locals) throws CommandException { - String itemString = stringParser.call(args, locals); - - Actor actor = locals.get(Actor.class); - LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor); - - ParserContext parserContext = new ParserContext(); - parserContext.setActor(actor); - if (actor instanceof Entity) { - Extent extent = ((Entity) actor).getExtent(); - if (extent instanceof World) { - parserContext.setWorld((World) extent); - } - } - parserContext.setSession(session); - - try { - return WorldEdit.getInstance().getItemFactory().parseFromInput(itemString, parserContext); - } catch (InputParseException e) { - throw new CommandException(e.getMessage(), e); - } - } - - @Override - public String getDescription() { - return "Match an item"; - } - - @Override - protected boolean testPermission0(CommandLocals locals) { - return true; - } - -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ItemUseParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ItemUseParser.java deleted file mode 100644 index f6da447a1..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ItemUseParser.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * Copyright (C) WorldEdit team and contributors - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser 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 Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.sk89q.worldedit.command.argument; - -import com.sk89q.minecraft.util.commands.CommandException; -import com.sk89q.minecraft.util.commands.CommandLocals; -import com.sk89q.worldedit.EditSession; -import com.sk89q.worldedit.WorldEditException; -import com.sk89q.worldedit.blocks.BaseItem; -import com.sk89q.worldedit.function.Contextual; -import com.sk89q.worldedit.function.EditContext; -import com.sk89q.worldedit.function.RegionFunction; -import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldedit.util.Direction; -import com.sk89q.worldedit.util.command.argument.CommandArgs; -import com.sk89q.worldedit.util.command.composition.SimpleCommand; -import com.sk89q.worldedit.world.World; - -public class ItemUseParser extends SimpleCommand> { - - private final ItemParser itemParser = addParameter(new ItemParser("item", "minecraft:bone_meal")); - - @Override - public Contextual call(CommandArgs args, CommandLocals locals) throws CommandException { - BaseItem item = itemParser.call(args, locals); - return new ItemUseFactory(item); - } - - @Override - public String getDescription() { - return "Applies an item"; - } - - @Override - protected boolean testPermission0(CommandLocals locals) { - return true; - } - - private static final class ItemUseFactory implements Contextual { - private final BaseItem item; - - private ItemUseFactory(BaseItem item) { - this.item = item; - } - - @Override - public RegionFunction createFromContext(EditContext input) { - World world = ((EditSession) input.getDestination()).getWorld(); - return new ItemUseFunction(world, item); - } - - @Override - public String toString() { - return "application of the item " + item.getType() + ":" + item.getNbtData(); - } - } - - private static final class ItemUseFunction implements RegionFunction { - private final World world; - private final BaseItem item; - - private ItemUseFunction(World world, BaseItem item) { - this.world = world; - this.item = item; - } - - @Override - public boolean apply(BlockVector3 position) throws WorldEditException { - return world.useItem(position, item, Direction.UP); - } - } - -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/MaskConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/MaskConverter.java deleted file mode 100644 index 37e71cbf4..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/MaskConverter.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * Copyright (C) WorldEdit team and contributors - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser 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 Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.sk89q.worldedit.command.argument; - -import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.entity.Entity; -import com.sk89q.worldedit.extension.input.InputParseException; -import com.sk89q.worldedit.extension.input.ParserContext; -import com.sk89q.worldedit.extension.platform.Actor; -import com.sk89q.worldedit.extent.Extent; -import com.sk89q.worldedit.function.mask.Mask; -import com.sk89q.worldedit.util.formatting.text.Component; -import com.sk89q.worldedit.util.formatting.text.TextComponent; -import com.sk89q.worldedit.world.World; -import org.enginehub.piston.CommandManager; -import org.enginehub.piston.converter.ArgumentConverter; -import org.enginehub.piston.converter.ConversionResult; -import org.enginehub.piston.converter.FailedConversion; -import org.enginehub.piston.converter.SuccessfulConversion; -import org.enginehub.piston.inject.InjectedValueAccess; -import org.enginehub.piston.inject.Key; - -public class MaskConverter implements ArgumentConverter { - - public static void register(WorldEdit worldEdit, CommandManager commandManager) { - commandManager.registerConverter(Key.of(Mask.class), new MaskConverter(worldEdit)); - } - - private final WorldEdit worldEdit; - - private MaskConverter(WorldEdit worldEdit) { - this.worldEdit = worldEdit; - } - - @Override - public ConversionResult convert(String argument, InjectedValueAccess context) { - Actor actor = context.injectedValue(Key.of(Actor.class)) - .orElseThrow(() -> new IllegalStateException("No actor")); - ParserContext parserContext = new ParserContext(); - parserContext.setActor(actor); - if (actor instanceof Entity) { - Extent extent = ((Entity) actor).getExtent(); - if (extent instanceof World) { - parserContext.setWorld((World) extent); - } - } - parserContext.setSession(worldEdit.getSessionManager().get(actor)); - try { - return SuccessfulConversion.fromSingle( - worldEdit.getMaskFactory().parseFromInput(argument, parserContext) - ); - } catch (InputParseException e) { - return FailedConversion.from(e); - } - } - - @Override - public Component describeAcceptableArguments() { - return TextComponent.of("any mask"); - } -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegionFactoryConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegionFactoryConverter.java new file mode 100644 index 000000000..ed54327b1 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegionFactoryConverter.java @@ -0,0 +1,72 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser 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 Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.command.argument; + +import com.sk89q.worldedit.regions.factory.CuboidRegionFactory; +import com.sk89q.worldedit.regions.factory.CylinderRegionFactory; +import com.sk89q.worldedit.regions.factory.RegionFactory; +import com.sk89q.worldedit.regions.factory.SphereRegionFactory; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TextComponent; +import org.enginehub.piston.CommandManager; +import org.enginehub.piston.converter.ArgumentConverter; +import org.enginehub.piston.converter.ConversionResult; +import org.enginehub.piston.converter.FailedConversion; +import org.enginehub.piston.converter.SuccessfulConversion; +import org.enginehub.piston.inject.InjectedValueAccess; +import org.enginehub.piston.inject.Key; + +public class RegionFactoryConverter implements ArgumentConverter { + + public static void register(CommandManager commandManager) { + commandManager.registerConverter(Key.of(RegionFactory.class), new RegionFactoryConverter()); + } + + private RegionFactoryConverter() { + } + + @Override + public Component describeAcceptableArguments() { + return TextComponent.of("cuboid|sphere|cyl"); + } + + @Override + public ConversionResult convert(String argument, InjectedValueAccess context) { + try { + return SuccessfulConversion.fromSingle(parse(argument)); + } catch (Exception e) { + return FailedConversion.from(e); + } + } + + private RegionFactory parse(String argument) { + switch (argument) { + case "cuboid": + return new CuboidRegionFactory(); + case "sphere": + return new SphereRegionFactory(); + case "cyl": + case "cylinder": + return new CylinderRegionFactory(1); // TODO: Adjustable height + default: + throw new IllegalArgumentException("Not a known region type: " + argument); + } + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegionFunctionParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegionFunctionParser.java deleted file mode 100644 index be96b19bb..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegionFunctionParser.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * Copyright (C) WorldEdit team and contributors - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser 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 Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.sk89q.worldedit.command.argument; - -import com.sk89q.worldedit.function.Contextual; -import com.sk89q.worldedit.function.RegionFunction; -import com.sk89q.worldedit.util.command.composition.BranchingCommand; - -public class RegionFunctionParser extends BranchingCommand> { - - public RegionFunctionParser() { - super("functionTpe"); - putOption(new TreeGeneratorParser("treeType"), "forest"); - putOption(new ItemUseParser(), "item"); - putOption(new ReplaceParser(), "set"); - } - - @Override - public String getDescription() { - return "Choose a block function"; - } - -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ReplaceParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ReplaceParser.java deleted file mode 100644 index eebfb4a16..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ReplaceParser.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * Copyright (C) WorldEdit team and contributors - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser 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 Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.sk89q.worldedit.command.argument; - -import static com.sk89q.worldedit.util.GuavaUtil.firstNonNull; - -import com.sk89q.minecraft.util.commands.CommandException; -import com.sk89q.minecraft.util.commands.CommandLocals; -import com.sk89q.worldedit.extent.NullExtent; -import com.sk89q.worldedit.function.Contextual; -import com.sk89q.worldedit.function.EditContext; -import com.sk89q.worldedit.function.RegionFunction; -import com.sk89q.worldedit.function.block.BlockReplace; -import com.sk89q.worldedit.function.pattern.Pattern; -import com.sk89q.worldedit.util.command.argument.CommandArgs; -import com.sk89q.worldedit.util.command.composition.SimpleCommand; - - -public class ReplaceParser extends SimpleCommand> { - - // TODO rewrite for new system -// private final PatternParser fillArg = addParameter(new PatternParser("fillPattern")); - - @Override - public Contextual call(CommandArgs args, CommandLocals locals) throws CommandException { -// Pattern fill = fillArg.call(args, locals); -// return new ReplaceFactory(fill); - return null; - } - - @Override - public String getDescription() { - return "Replaces blocks"; - } - - @Override - protected boolean testPermission0(CommandLocals locals) { - return true; - } - - private static class ReplaceFactory implements Contextual { - private final Pattern fill; - - private ReplaceFactory(Pattern fill) { - this.fill = fill; - } - - @Override - public RegionFunction createFromContext(EditContext context) { - return new BlockReplace( - firstNonNull(context.getDestination(), new NullExtent()), - firstNonNull(context.getFill(), fill)); - } - - @Override - public String toString() { - return "replace blocks"; - } - } - -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/ApplyCommand.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/ApplyCommand.java deleted file mode 100644 index a73748e4c..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/ApplyCommand.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * Copyright (C) WorldEdit team and contributors - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser 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 Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.sk89q.worldedit.command.composition; - -import static com.google.common.base.Preconditions.checkNotNull; - -import com.sk89q.minecraft.util.commands.CommandException; -import com.sk89q.minecraft.util.commands.CommandLocals; -import com.sk89q.worldedit.command.argument.RegionFunctionParser; -import com.sk89q.worldedit.function.Contextual; -import com.sk89q.worldedit.function.RegionFunction; -import com.sk89q.worldedit.function.factory.Apply; -import com.sk89q.worldedit.function.operation.Operation; -import com.sk89q.worldedit.util.command.argument.CommandArgs; -import com.sk89q.worldedit.util.command.composition.CommandExecutor; -import com.sk89q.worldedit.util.command.composition.SimpleCommand; - -public class ApplyCommand extends SimpleCommand> { - - private final CommandExecutor> functionParser; - private final String description; - - public ApplyCommand() { - this(new RegionFunctionParser(), "Applies a function to every block"); - } - - public ApplyCommand(CommandExecutor> functionParser, String description) { - checkNotNull(functionParser, "functionParser"); - checkNotNull(description, "description"); - this.functionParser = functionParser; - this.description = description; - addParameter(functionParser); - } - - @Override - public Apply call(CommandArgs args, CommandLocals locals) throws CommandException { - Contextual function = functionParser.call(args, locals); - return new Apply(function); - } - - @Override - public String getDescription() { - return description; - } - - @Override - protected boolean testPermission0(CommandLocals locals) { - return true; - } - -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/PaintCommand.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/PaintCommand.java deleted file mode 100644 index 8ce3e004c..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/PaintCommand.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * Copyright (C) WorldEdit team and contributors - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser 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 Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.sk89q.worldedit.command.composition; - -import com.sk89q.minecraft.util.commands.CommandException; -import com.sk89q.minecraft.util.commands.CommandLocals; -import com.sk89q.worldedit.command.argument.NumberParser; -import com.sk89q.worldedit.command.argument.RegionFunctionParser; -import com.sk89q.worldedit.function.Contextual; -import com.sk89q.worldedit.function.RegionFunction; -import com.sk89q.worldedit.function.factory.Paint; -import com.sk89q.worldedit.util.command.argument.CommandArgs; -import com.sk89q.worldedit.util.command.composition.CommandExecutor; -import com.sk89q.worldedit.util.command.composition.SimpleCommand; - -public class PaintCommand extends SimpleCommand { - - private final NumberParser densityCommand = addParameter(new NumberParser("density", "0-100", "20")); - private final CommandExecutor> functionParser; - - public PaintCommand() { - this(new RegionFunctionParser()); - } - - public PaintCommand(CommandExecutor> functionParser) { - this.functionParser = functionParser; - addParameter(functionParser); - } - - @Override - public Paint call(CommandArgs args, CommandLocals locals) throws CommandException { - double density = densityCommand.call(args, locals).doubleValue() / 100.0; - Contextual function = functionParser.call(args, locals); - return new Paint(function, density); - } - - @Override - public String getDescription() { - return "Applies a function to surfaces"; - } - - @Override - protected boolean testPermission0(CommandLocals locals) { - return true; - } - -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/SelectionCommand.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/SelectionCommand.java index dd51c64a2..c0f93463c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/SelectionCommand.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/SelectionCommand.java @@ -94,7 +94,7 @@ public class SelectionCommand extends SimpleCommand { return operation; } catch (IncompleteRegionException e) { - WorldEdit.getInstance().getPlatformManager().getPlatformCommandMananger().getExceptionConverter().convert(e); + WorldEdit.getInstance().getPlatformManager().getPlatformCommandManager().getExceptionConverter().convert(e); return null; } } else { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/ShapedBrushCommand.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/ShapedBrushCommand.java index 35fb758a3..911c4080e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/ShapedBrushCommand.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/ShapedBrushCommand.java @@ -77,7 +77,7 @@ public class ShapedBrushCommand extends SimpleCommand { tool.setFill(null); tool.setBrush(new OperationFactoryBrush(factory, regionFactory, session), permission); } catch (MaxBrushRadiusException | InvalidToolBindException e) { - WorldEdit.getInstance().getPlatformManager().getPlatformCommandMananger().getExceptionConverter().convert(e); + WorldEdit.getInstance().getPlatformManager().getPlatformCommandManager().getExceptionConverter().convert(e); } player.print("Set brush to " + factory); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ItemUseFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ItemUseFactory.java new file mode 100644 index 000000000..33cd470c8 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ItemUseFactory.java @@ -0,0 +1,28 @@ +package com.sk89q.worldedit.command.factory; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.blocks.BaseItem; +import com.sk89q.worldedit.function.ItemUseFunction; +import com.sk89q.worldedit.function.Contextual; +import com.sk89q.worldedit.function.EditContext; +import com.sk89q.worldedit.function.RegionFunction; +import com.sk89q.worldedit.world.World; + +public final class ItemUseFactory implements Contextual { + private final BaseItem item; + + public ItemUseFactory(BaseItem item) { + this.item = item; + } + + @Override + public RegionFunction createFromContext(EditContext input) { + World world = ((EditSession) input.getDestination()).getWorld(); + return new ItemUseFunction(world, item); + } + + @Override + public String toString() { + return "application of the item " + item.getType() + ":" + item.getNbtData(); + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ReplaceFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ReplaceFactory.java new file mode 100644 index 000000000..03453fcd7 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ReplaceFactory.java @@ -0,0 +1,30 @@ +package com.sk89q.worldedit.command.factory; + +import com.sk89q.worldedit.extent.NullExtent; +import com.sk89q.worldedit.function.Contextual; +import com.sk89q.worldedit.function.EditContext; +import com.sk89q.worldedit.function.RegionFunction; +import com.sk89q.worldedit.function.block.BlockReplace; +import com.sk89q.worldedit.function.pattern.Pattern; + +import static com.sk89q.worldedit.util.GuavaUtil.firstNonNull; + +public class ReplaceFactory implements Contextual { + private final Pattern fill; + + public ReplaceFactory(Pattern fill) { + this.fill = fill; + } + + @Override + public RegionFunction createFromContext(EditContext context) { + return new BlockReplace( + firstNonNull(context.getDestination(), new NullExtent()), + firstNonNull(context.getFill(), fill)); + } + + @Override + public String toString() { + return "replace blocks"; + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/TreeGeneratorFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/TreeGeneratorFactory.java new file mode 100644 index 000000000..47548b4d5 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/TreeGeneratorFactory.java @@ -0,0 +1,25 @@ +package com.sk89q.worldedit.command.factory; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.function.Contextual; +import com.sk89q.worldedit.function.EditContext; +import com.sk89q.worldedit.function.generator.ForestGenerator; +import com.sk89q.worldedit.util.TreeGenerator; + +public final class TreeGeneratorFactory implements Contextual { + private final TreeGenerator.TreeType type; + + public TreeGeneratorFactory(TreeGenerator.TreeType type) { + this.type = type; + } + + @Override + public ForestGenerator createFromContext(EditContext input) { + return new ForestGenerator((EditSession) input.getDestination(), type); + } + + @Override + public String toString() { + return "tree of type " + type; + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PermissionCondition.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PermissionCondition.java index 4b33675f9..48b6acc16 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PermissionCondition.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PermissionCondition.java @@ -32,7 +32,7 @@ public final class PermissionCondition implements Command.Condition { private final Set permissions; - PermissionCondition(Set permissions) { + public PermissionCondition(Set permissions) { this.permissions = permissions; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PrintCommandHelp.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PrintCommandHelp.java index 57337b322..b1971b0a9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PrintCommandHelp.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PrintCommandHelp.java @@ -74,7 +74,7 @@ public class PrintCommandHelp { actor.printError("Page must be >= 1."); return; } - CommandManager manager = we.getPlatformManager().getPlatformCommandMananger().getCommandManager(); + CommandManager manager = we.getPlatformManager().getPlatformCommandManager().getCommandManager(); final int perPage = actor instanceof Player ? 8 : 20; // More pages for console diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Capability.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Capability.java index 50aa913b4..e91c799bc 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Capability.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Capability.java @@ -50,12 +50,12 @@ public enum Capability { USER_COMMANDS { @Override void initialize(PlatformManager platformManager, Platform platform) { - platformManager.getPlatformCommandMananger().register(platform); + platformManager.getPlatformCommandManager().registerCommandsWith(platform); } @Override void unload(PlatformManager platformManager, Platform platform) { - platformManager.getPlatformCommandMananger().unregister(); + platformManager.getPlatformCommandManager().removeCommands(); } }, diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandMananger.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java similarity index 86% rename from worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandMananger.java rename to worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index eb8b4adbc..ed93a11b8 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandMananger.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -26,6 +26,7 @@ import com.sk89q.worldedit.IncompleteRegionException; import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.command.ApplyBrushCommands; import com.sk89q.worldedit.command.BiomeCommands; import com.sk89q.worldedit.command.BiomeCommandsRegistration; import com.sk89q.worldedit.command.BrushCommands; @@ -42,6 +43,7 @@ import com.sk89q.worldedit.command.HistoryCommands; import com.sk89q.worldedit.command.HistoryCommandsRegistration; import com.sk89q.worldedit.command.NavigationCommands; import com.sk89q.worldedit.command.NavigationCommandsRegistration; +import com.sk89q.worldedit.command.PaintBrushCommands; import com.sk89q.worldedit.command.RegionCommands; import com.sk89q.worldedit.command.RegionCommandsRegistration; import com.sk89q.worldedit.command.SchematicCommands; @@ -71,11 +73,10 @@ import com.sk89q.worldedit.command.argument.DirectionConverter; import com.sk89q.worldedit.command.argument.EntityRemoverConverter; import com.sk89q.worldedit.command.argument.EnumConverter; import com.sk89q.worldedit.command.argument.ExpandAmountConverter; -import com.sk89q.worldedit.command.argument.MaskConverter; -import com.sk89q.worldedit.command.argument.PatternConverter; +import com.sk89q.worldedit.command.argument.FactoryConverter; +import com.sk89q.worldedit.command.argument.RegionFactoryConverter; import com.sk89q.worldedit.command.argument.VectorConverter; import com.sk89q.worldedit.command.argument.ZonedDateTimeConverter; -import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; import com.sk89q.worldedit.command.util.PermissionCondition; import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.entity.Player; @@ -83,7 +84,6 @@ import com.sk89q.worldedit.event.platform.CommandEvent; import com.sk89q.worldedit.event.platform.CommandSuggestionEvent; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.internal.annotation.Selection; -import com.sk89q.worldedit.internal.command.CommandLoggingHandler; import com.sk89q.worldedit.internal.command.WorldEditExceptionConverter; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.session.request.Request; @@ -102,7 +102,6 @@ import org.enginehub.piston.exception.CommandException; import org.enginehub.piston.exception.CommandExecutionException; import org.enginehub.piston.exception.ConditionFailedException; import org.enginehub.piston.exception.UsageException; -import org.enginehub.piston.gen.CommandCallListener; import org.enginehub.piston.gen.CommandRegistration; import org.enginehub.piston.inject.InjectedValueStore; import org.enginehub.piston.inject.Key; @@ -116,29 +115,28 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; -import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.function.Consumer; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.regex.Pattern; import java.util.stream.Collectors; import static com.google.common.base.Preconditions.checkNotNull; +import static com.sk89q.worldedit.util.command.CommandUtil.COMMAND_LOG; +import static com.sk89q.worldedit.util.command.CommandUtil.register; /** * Handles the registration and invocation of commands. * *

This class is primarily for internal usage.

*/ -public final class PlatformCommandMananger { +public final class PlatformCommandManager { public static final Pattern COMMAND_CLEAN_PATTERN = Pattern.compile("^[/]+"); - private static final Logger log = LoggerFactory.getLogger(PlatformCommandMananger.class); - private static final java.util.logging.Logger commandLog = - java.util.logging.Logger.getLogger(PlatformCommandMananger.class.getCanonicalName() + ".CommandLog"); + private static final Logger log = LoggerFactory.getLogger(PlatformCommandManager.class); private static final Pattern numberFormatExceptionPattern = Pattern.compile("^For input string: \"(.*)\"$"); - private static final CommandPermissionsConditionGenerator PERM_GEN = new CommandPermissionsConditionGenerator(); private final WorldEdit worldEdit; private final PlatformManager platformManager; @@ -146,14 +144,13 @@ public final class PlatformCommandMananger { private final InjectedValueStore globalInjectedValues; private final DynamicStreamHandler dynamicHandler = new DynamicStreamHandler(); private final WorldEditExceptionConverter exceptionConverter; - private final List callListeners; /** * Create a new instance. * * @param worldEdit the WorldEdit instance */ - PlatformCommandMananger(final WorldEdit worldEdit, PlatformManager platformManager) { + PlatformCommandManager(final WorldEdit worldEdit, PlatformManager platformManager) { checkNotNull(worldEdit); checkNotNull(platformManager); this.worldEdit = worldEdit; @@ -162,32 +159,17 @@ public final class PlatformCommandMananger { this.commandManager = DefaultCommandManagerService.getInstance() .newCommandManager(); this.globalInjectedValues = MapBackedValueStore.create(); - this.callListeners = Collections.singletonList( - new CommandLoggingHandler(worldEdit, commandLog) - ); // setup separate from main constructor // ensures that everything is definitely assigned initialize(); } - private void register(CommandManager manager, CommandRegistration registration, CI instance) { - registration.containerInstance(instance) - .commandManager(manager) - .listeners(callListeners); - if (registration instanceof CommandPermissionsConditionGenerator.Registration) { - ((CommandPermissionsConditionGenerator.Registration) registration).commandPermissionsConditionGenerator( - PERM_GEN - ); - } - registration.build(); - } - private void initialize() { // Register this instance for command events worldEdit.getEventBus().register(this); // Setup the logger - commandLog.addHandler(dynamicHandler); + COMMAND_LOG.addHandler(dynamicHandler); // Set up the commands manager registerAlwaysInjectedValues(); @@ -197,8 +179,7 @@ public final class PlatformCommandMananger { private void registerArgumentConverters() { DirectionConverter.register(worldEdit, commandManager); - MaskConverter.register(worldEdit, commandManager); - PatternConverter.register(worldEdit, commandManager); + FactoryConverter.register(worldEdit, commandManager); for (int count = 2; count <= 3; count++) { commandManager.registerConverter(Key.of(double.class, Annotations.radii(count)), CommaSeparatedValuesConverter.wrapAndLimit(ArgumentConverters.get( @@ -212,6 +193,7 @@ public final class PlatformCommandMananger { ZonedDateTimeConverter.register(commandManager); BooleanConverter.register(commandManager); EntityRemoverConverter.register(commandManager); + RegionFactoryConverter.register(commandManager); } private void registerAlwaysInjectedValues() { @@ -243,7 +225,13 @@ public final class PlatformCommandMananger { } private void registerSubCommands(String name, List aliases, String desc, - CommandRegistration registration, CI instance) { + CommandRegistration registration, CI instance) { + registerSubCommands(name, aliases, desc, registration, instance, m -> {}); + } + + private void registerSubCommands(String name, List aliases, String desc, + CommandRegistration registration, CI instance, + Consumer additionalConfig) { commandManager.register(name, cmd -> { cmd.aliases(aliases); cmd.description(TextComponent.of(desc)); @@ -292,7 +280,11 @@ public final class PlatformCommandMananger { ImmutableList.of("br"), "Brushing commands", BrushCommandsRegistration.builder(), - new BrushCommands(worldEdit) + new BrushCommands(worldEdit), + manager -> { + PaintBrushCommands.register(manager); + ApplyBrushCommands.register(manager); + } ); registerSubCommands( "worldedit", @@ -371,31 +363,13 @@ public final class PlatformCommandMananger { UtilityCommandsRegistration.builder(), new UtilityCommands(worldEdit) ); - - // Unported commands are below. Delete once they're added to the main manager above. - /* - dispatcher = new CommandGraph() - .builder(builder) - .commands() - .group("brush", "br") - .describeAs("Brushing commands") - .register(adapt(new ShapedBrushCommand(new DeformCommand(), "worldedit.brush.deform")), "deform") - .register(adapt(new ShapedBrushCommand(new ApplyCommand(new ReplaceParser(), "Set all blocks within region"), "worldedit.brush.set")), "set") - .register(adapt(new ShapedBrushCommand(new PaintCommand(), "worldedit.brush.paint")), "paint") - .register(adapt(new ShapedBrushCommand(new ApplyCommand(), "worldedit.brush.apply")), "apply") - .register(adapt(new ShapedBrushCommand(new PaintCommand(new TreeGeneratorParser("treeType")), "worldedit.brush.forest")), "forest") - .register(adapt(new ShapedBrushCommand(ProvidedValue.create(new Deform("y-=1", Mode.RAW_COORD), "Raise one block"), "worldedit.brush.raise")), "raise") - .register(adapt(new ShapedBrushCommand(ProvidedValue.create(new Deform("y+=1", Mode.RAW_COORD), "Lower one block"), "worldedit.brush.lower")), "lower") - .parent() - .getDispatcher(); - */ } public ExceptionConverter getExceptionConverter() { return exceptionConverter; } - void register(Platform platform) { + void registerCommandsWith(Platform platform) { log.info("Registering commands with " + platform.getClass().getCanonicalName()); LocalConfiguration config = platform.getConfiguration(); @@ -405,10 +379,10 @@ public final class PlatformCommandMananger { // Register log if (!logging || path.isEmpty()) { dynamicHandler.setHandler(null); - commandLog.setLevel(Level.OFF); + COMMAND_LOG.setLevel(Level.OFF); } else { File file = new File(config.getWorkingDirectory(), path); - commandLog.setLevel(Level.ALL); + COMMAND_LOG.setLevel(Level.ALL); log.info("Logging WorldEdit commands to " + file.getAbsolutePath()); @@ -424,7 +398,7 @@ public final class PlatformCommandMananger { platform.registerCommands(commandManager); } - void unregister() { + void removeCommands() { dynamicHandler.setHandler(null); } @@ -581,8 +555,4 @@ public final class PlatformCommandMananger { return commandManager; } - public static java.util.logging.Logger getLogger() { - return commandLog; - } - } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java index 692c1558e..f37c9b4a5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java @@ -68,7 +68,7 @@ public class PlatformManager { private static final Logger logger = LoggerFactory.getLogger(PlatformManager.class); private final WorldEdit worldEdit; - private final PlatformCommandMananger platformCommandMananger; + private final PlatformCommandManager platformCommandManager; private final List platforms = new ArrayList<>(); private final Map preferences = new EnumMap<>(Capability.class); private @Nullable String firstSeenVersion; @@ -83,7 +83,7 @@ public class PlatformManager { public PlatformManager(WorldEdit worldEdit) { checkNotNull(worldEdit); this.worldEdit = worldEdit; - this.platformCommandMananger = new PlatformCommandMananger(worldEdit, this); + this.platformCommandManager = new PlatformCommandManager(worldEdit, this); // Register this instance for events worldEdit.getEventBus().register(this); @@ -277,8 +277,8 @@ public class PlatformManager { * * @return the command manager */ - public PlatformCommandMananger getPlatformCommandMananger() { - return platformCommandMananger; + public PlatformCommandManager getPlatformCommandManager() { + return platformCommandManager; } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/ItemUseFunction.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/ItemUseFunction.java new file mode 100644 index 000000000..b3b8e4748 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/ItemUseFunction.java @@ -0,0 +1,23 @@ +package com.sk89q.worldedit.function; + +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.blocks.BaseItem; +import com.sk89q.worldedit.function.RegionFunction; +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.util.Direction; +import com.sk89q.worldedit.world.World; + +public final class ItemUseFunction implements RegionFunction { + private final World world; + private final BaseItem item; + + public ItemUseFunction(World world, BaseItem item) { + this.world = world; + this.item = item; + } + + @Override + public boolean apply(BlockVector3 position) throws WorldEditException { + return world.useItem(position, item, Direction.UP); + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandUtil.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandUtil.java index c583add43..ccf702e16 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandUtil.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandUtil.java @@ -19,13 +19,22 @@ package com.sk89q.worldedit.util.command; -import com.sk89q.worldedit.extension.platform.PlatformCommandMananger; +import com.google.common.collect.ImmutableList; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; +import com.sk89q.worldedit.extension.platform.PlatformCommandManager; +import com.sk89q.worldedit.internal.command.CommandLoggingHandler; import org.enginehub.piston.Command; +import org.enginehub.piston.CommandManager; +import org.enginehub.piston.gen.CommandCallListener; +import org.enginehub.piston.gen.CommandRegistration; import org.enginehub.piston.part.SubCommandPart; import java.util.Comparator; +import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.logging.Logger; import java.util.stream.Collectors; public class CommandUtil { @@ -38,7 +47,7 @@ public class CommandUtil { } private static String clean(String input) { - return PlatformCommandMananger.COMMAND_CLEAN_PATTERN.matcher(input).replaceAll(""); + return PlatformCommandManager.COMMAND_CLEAN_PATTERN.matcher(input).replaceAll(""); } private static final Comparator BY_CLEAN_NAME = @@ -48,6 +57,26 @@ public class CommandUtil { return BY_CLEAN_NAME; } + private static final CommandPermissionsConditionGenerator PERM_GEN = new CommandPermissionsConditionGenerator(); + + public static final Logger COMMAND_LOG = + Logger.getLogger("com.sk89q.worldedit.CommandLog"); + private static final List CALL_LISTENERS = ImmutableList.of( + new CommandLoggingHandler(WorldEdit.getInstance(), COMMAND_LOG) + ); + + public static void register(CommandManager manager, CommandRegistration registration, CI instance) { + registration.containerInstance(instance) + .commandManager(manager) + .listeners(CALL_LISTENERS); + if (registration instanceof CommandPermissionsConditionGenerator.Registration) { + ((CommandPermissionsConditionGenerator.Registration) registration).commandPermissionsConditionGenerator( + PERM_GEN + ); + } + registration.build(); + } + private CommandUtil() { } }