From 59fcef898eb2dc69db1a6a08c2e5fa348d2c86d3 Mon Sep 17 00:00:00 2001 From: Octavia Togami Date: Sat, 16 Jul 2022 00:35:59 -0700 Subject: [PATCH] Clean up getBrushTool deprecations --- .../com/sk89q/worldedit/LocalSession.java | 22 ++++ .../worldedit/command/BrushCommands.java | 119 ++++++++++++------ .../worldedit/command/ToolUtilCommands.java | 36 +++++- .../worldedit/command/tool/BrushTool.java | 13 ++ .../src/main/resources/lang/strings.json | 1 + 5 files changed, 147 insertions(+), 44 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java index 53ab65b36..f85e139d9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -30,6 +30,7 @@ import com.sk89q.worldedit.command.tool.SelectionWand; import com.sk89q.worldedit.command.tool.SinglePickaxe; import com.sk89q.worldedit.command.tool.Tool; +import com.sk89q.worldedit.command.tool.brush.Brush; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Locatable; @@ -678,6 +679,8 @@ public Tool getTool(ItemType item) { * @param item the item type * @return the tool, or {@code null} * @throws InvalidToolBindException if the item can't be bound to that item + * @deprecated Use {@link #getBrush(ItemType)} or {@link #forceBrush(ItemType, Brush, String)} + * if you need to bind a specific brush */ @Deprecated public BrushTool getBrushTool(ItemType item) throws InvalidToolBindException { @@ -702,6 +705,25 @@ public BrushTool getBrush(ItemType item) { return getTool(item) instanceof BrushTool tool ? tool : null; } + /** + * Force the tool to become a brush tool with the specified brush and permission. + * + * @param item the item type + * @param brush the brush to bind + * @param permission the permission to check before use is allowed + * @return the brush tool assigned to the item type + */ + public BrushTool forceBrush(ItemType item, Brush brush, String permission) throws InvalidToolBindException { + BrushTool tool = getBrush(item); + if (tool == null) { + tool = new BrushTool(brush, permission); + setTool(item, tool); + } else { + tool.setBrush(brush, permission); + } + return tool; + } + /** * Set the tool. * 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 b1abbda49..d3325a073 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 @@ -27,6 +27,8 @@ 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.InvalidToolBindException; +import com.sk89q.worldedit.command.tool.brush.Brush; import com.sk89q.worldedit.command.tool.brush.ButcherBrush; import com.sk89q.worldedit.command.tool.brush.ClipboardBrush; import com.sk89q.worldedit.command.tool.brush.CylinderBrush; @@ -132,16 +134,16 @@ public void sphereBrush(Player player, LocalSession session, boolean hollow) throws WorldEditException { worldEdit.checkMaxBrushRadius(radius); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + Brush brush = hollow ? new HollowSphereBrush() : new SphereBrush(); + + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + brush, + "worldedit.brush.sphere" + ); tool.setFill(pattern); tool.setSize(radius); - if (hollow) { - tool.setBrush(new HollowSphereBrush(), "worldedit.brush.sphere"); - } else { - tool.setBrush(new SphereBrush(), "worldedit.brush.sphere"); - } - player.printInfo(TranslatableComponent.of("worldedit.brush.sphere.equip", TextComponent.of(String.format("%.0f", radius)))); ToolCommands.sendUnbindInstruction(player, UNBIND_COMMAND_COMPONENT); } @@ -164,16 +166,16 @@ public void cylinderBrush(Player player, LocalSession session, worldEdit.checkMaxBrushRadius(radius); worldEdit.checkMaxBrushRadius(height); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + Brush brush = hollow ? new HollowCylinderBrush(height) : new CylinderBrush(height); + + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + brush, + "worldedit.brush.cylinder" + ); tool.setFill(pattern); tool.setSize(radius); - if (hollow) { - tool.setBrush(new HollowCylinderBrush(height), "worldedit.brush.cylinder"); - } else { - tool.setBrush(new CylinderBrush(height), "worldedit.brush.cylinder"); - } - player.printInfo(TranslatableComponent.of("worldedit.brush.cylinder.equip", TextComponent.of((int) radius), TextComponent.of(height))); ToolCommands.sendUnbindInstruction(player, UNBIND_COMMAND_COMPONENT); } @@ -198,12 +200,14 @@ public void splatterBrush(Player player, LocalSession session, return; } - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new SplatterBrush(decay), + "worldedit.brush.splatter" + ); tool.setFill(pattern); tool.setSize(radius); - tool.setBrush(new SplatterBrush(decay), "worldedit.brush.splatter"); - player.printInfo(TranslatableComponent.of("worldedit.brush.splatter.equip", TextComponent.of((int) radius), TextComponent.of(decay))); ToolCommands.sendUnbindInstruction(player, UNBIND_COMMAND_COMPONENT); } @@ -239,8 +243,11 @@ public void clipboardBrush(Player player, LocalSession session, worldEdit.checkMaxBrushRadius(size.getBlockY() / 2D - 1); worldEdit.checkMaxBrushRadius(size.getBlockZ() / 2D - 1); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); - tool.setBrush(new ClipboardBrush(newHolder, ignoreAir, usingOrigin, pasteEntities, pasteBiomes, sourceMask), "worldedit.brush.clipboard"); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new ClipboardBrush(newHolder, ignoreAir, usingOrigin, pasteEntities, pasteBiomes, sourceMask), + "worldedit.brush.clipboard" + ); player.printInfo(TranslatableComponent.of("worldedit.brush.clipboard.equip")); ToolCommands.sendUnbindInstruction(player, UNBIND_COMMAND_COMPONENT); @@ -261,9 +268,12 @@ public void smoothBrush(Player player, LocalSession session, Mask mask) throws WorldEditException { worldEdit.checkMaxBrushRadius(radius); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new SmoothBrush(iterations, mask), + "worldedit.brush.smooth" + ); tool.setSize(radius); - tool.setBrush(new SmoothBrush(iterations, mask), "worldedit.brush.smooth"); player.printInfo(TranslatableComponent.of( "worldedit.brush.smooth.equip", @@ -291,9 +301,12 @@ public void snowSmoothBrush(Player player, LocalSession session, Mask mask) throws WorldEditException { worldEdit.checkMaxBrushRadius(radius); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new SnowSmoothBrush(iterations, snowBlockCount, mask), + "worldedit.brush.snowsmooth" + ); tool.setSize(radius); - tool.setBrush(new SnowSmoothBrush(iterations, snowBlockCount, mask), "worldedit.brush.snowsmooth"); player.printInfo(TranslatableComponent.of( "worldedit.brush.snowsmooth.equip", @@ -315,11 +328,14 @@ public void extinguishBrush(Player player, LocalSession session, double radius) throws WorldEditException { worldEdit.checkMaxBrushRadius(radius); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new SphereBrush(), + "worldedit.brush.ex" + ); tool.setFill(BlockTypes.AIR.getDefaultState()); tool.setSize(radius); tool.setMask(new BlockTypeMask(new RequestExtent(), BlockTypes.FIRE)); - tool.setBrush(new SphereBrush(), "worldedit.brush.ex"); player.printInfo(TranslatableComponent.of("worldedit.brush.extinguish.equip", TextComponent.of((int) radius))); ToolCommands.sendUnbindInstruction(player, UNBIND_COMMAND_COMPONENT); @@ -345,9 +361,12 @@ public void gravityBrush(Player player, LocalSession session, Integer height) throws WorldEditException { worldEdit.checkMaxBrushRadius(radius); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new GravityBrush(height), + "worldedit.brush.gravity" + ); tool.setSize(radius); - tool.setBrush(new GravityBrush(height), "worldedit.brush.gravity"); player.printInfo(TranslatableComponent.of("worldedit.brush.gravity.equip", TextComponent.of((int) radius))); ToolCommands.sendUnbindInstruction(player, UNBIND_COMMAND_COMPONENT); @@ -405,9 +424,11 @@ public void butcherBrush(Player player, LocalSession session, flags.or(CreatureButcher.Flags.ARMOR_STAND, killArmorStands, "worldedit.butcher.armorstands"); flags.or(CreatureButcher.Flags.WATER, killWater, "worldedit.butcher.water"); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new ButcherBrush(flags), "worldedit.brush.butcher" + ); tool.setSize(radius); - tool.setBrush(new ButcherBrush(flags), "worldedit.brush.butcher"); player.printInfo(TranslatableComponent.of("worldedit.brush.butcher.equip", TextComponent.of((int) radius))); ToolCommands.sendUnbindInstruction(player, UNBIND_COMMAND_COMPONENT); @@ -435,7 +456,6 @@ void heightmapBrush(Player player, LocalSession session, if (loader.isPresent()) { worldEdit.checkMaxBrushRadius(radius); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); AssetLoadTask task = new AssetLoadTask<>(loader.get(), imageName); AsyncCommandBuilder.wrap(task, player) @@ -443,8 +463,17 @@ void heightmapBrush(Player player, LocalSession session, .setDelayMessage(TranslatableComponent.of("worldedit.asset.load.loading")) .setWorkingMessage(TranslatableComponent.of("worldedit.asset.load.still-loading")) .onSuccess(TranslatableComponent.of("worldedit.brush.heightmap.equip", TextComponent.of((int) radius)), heightmap -> { + BrushTool tool; + try { + tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new ImageHeightmapBrush(heightmap, intensity, erase, flatten, randomize), + "worldedit.brush.heightmap" + ); + } catch (InvalidToolBindException e) { + throw new RuntimeException(e); + } tool.setSize(radius); - tool.setBrush(new ImageHeightmapBrush(heightmap, intensity, erase, flatten, randomize), "worldedit.brush.heightmap"); ToolCommands.sendUnbindInstruction(player, UNBIND_COMMAND_COMPONENT); }) .onFailure(TranslatableComponent.of("worldedit.asset.load.failed"), worldEdit.getPlatformManager().getPlatformCommandManager().getExceptionConverter()) @@ -599,9 +628,12 @@ public void morph(Player player, LocalSession session, @Arg(desc = "Dilate iterations", def = "1") int numDilateIterations) throws WorldEditException { worldEdit.checkMaxBrushRadius(brushSize); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new MorphBrush(minErodeFaces, numErodeIterations, minDilateFaces, numDilateIterations), + "worldedit.brush.morph" + ); tool.setSize(brushSize); - tool.setBrush(new MorphBrush(minErodeFaces, numErodeIterations, minDilateFaces, numDilateIterations), "worldedit.brush.morph"); player.printInfo(TranslatableComponent.of("worldedit.brush.morph.equip", TextComponent.of((int) brushSize))); } @@ -615,9 +647,12 @@ public void erode(Player player, LocalSession session, @Arg(desc = "The size of the brush", def = "5") double brushSize) throws WorldEditException { worldEdit.checkMaxBrushRadius(brushSize); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new MorphBrush(2, 1, 5, 1), + "worldedit.brush.morph" + ); tool.setSize(brushSize); - tool.setBrush(new MorphBrush(2, 1, 5, 1), "worldedit.brush.morph"); player.printInfo(TranslatableComponent.of("worldedit.brush.morph.equip", TextComponent.of((int) brushSize))); } @@ -631,9 +666,12 @@ public void dilate(Player player, LocalSession session, @Arg(desc = "The size of the brush", def = "5") double brushSize) throws WorldEditException { worldEdit.checkMaxBrushRadius(brushSize); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new MorphBrush(5, 1, 2, 1), + "worldedit.brush.morph" + ); tool.setSize(brushSize); - tool.setBrush(new MorphBrush(5, 1, 2, 1), "worldedit.brush.morph"); player.printInfo(TranslatableComponent.of("worldedit.brush.morph.equip", TextComponent.of((int) brushSize))); } @@ -643,10 +681,13 @@ static void setOperationBasedBrush(Player player, LocalSession session, double r RegionFactory shape, String permission) throws WorldEditException { WorldEdit.getInstance().checkMaxBrushRadius(radius); - BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + BrushTool tool = session.forceBrush( + player.getItemInHand(HandSide.MAIN_HAND).getType(), + new OperationFactoryBrush(factory, shape, session), + permission + ); tool.setSize(radius); tool.setFill(null); - tool.setBrush(new OperationFactoryBrush(factory, shape, session), permission); player.printInfo(TranslatableComponent.of("worldedit.brush.operation.equip", TextComponent.of(factory.toString()))); ToolCommands.sendUnbindInstruction(player, UNBIND_COMMAND_COMPONENT); 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 b71f30218..62963676c 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,6 +22,7 @@ import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.command.tool.BrushTool; import com.sk89q.worldedit.command.util.CommandPermissions; import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; import com.sk89q.worldedit.entity.Player; @@ -76,7 +77,12 @@ public void togglePickaxe(Player player, LocalSession session, public void mask(Player player, LocalSession session, @Arg(desc = "The mask to set", def = "") Mask mask) throws WorldEditException { - session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()).setMask(mask); + BrushTool brushTool = session.getBrush(player.getItemInHand(HandSide.MAIN_HAND).getType()); + if (brushTool == null) { + player.printError(TranslatableComponent.of("worldedit.brush.none.equipped")); + return; + } + brushTool.setMask(mask); if (mask == null) { player.printInfo(TranslatableComponent.of("worldedit.tool.mask.disabled")); } else { @@ -93,7 +99,12 @@ public void mask(Player player, LocalSession session, public void material(Player player, LocalSession session, @Arg(desc = "The pattern of blocks to use") Pattern pattern) throws WorldEditException { - session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()).setFill(pattern); + BrushTool brushTool = session.getBrush(player.getItemInHand(HandSide.MAIN_HAND).getType()); + if (brushTool == null) { + player.printError(TranslatableComponent.of("worldedit.brush.none.equipped")); + return; + } + brushTool.setFill(pattern); player.printInfo(TranslatableComponent.of("worldedit.tool.material.set")); } @@ -105,7 +116,12 @@ public void material(Player player, LocalSession session, public void range(Player player, LocalSession session, @Arg(desc = "The range of the brush") int range) throws WorldEditException { - session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()).setRange(range); + BrushTool brushTool = session.getBrush(player.getItemInHand(HandSide.MAIN_HAND).getType()); + if (brushTool == null) { + player.printError(TranslatableComponent.of("worldedit.brush.none.equipped")); + return; + } + brushTool.setRange(range); player.printInfo(TranslatableComponent.of("worldedit.tool.range.set")); } @@ -119,7 +135,12 @@ public void size(Player player, LocalSession session, int size) throws WorldEditException { we.checkMaxBrushRadius(size); - session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()).setSize(size); + BrushTool brushTool = session.getBrush(player.getItemInHand(HandSide.MAIN_HAND).getType()); + if (brushTool == null) { + player.printError(TranslatableComponent.of("worldedit.brush.none.equipped")); + return; + } + brushTool.setSize(size); player.printInfo(TranslatableComponent.of("worldedit.tool.size.set")); } @@ -131,7 +152,12 @@ public void size(Player player, LocalSession session, public void traceMask(Player player, LocalSession session, @Arg(desc = "The trace mask to set", def = "") Mask mask) throws WorldEditException { - session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()).setTraceMask(mask); + BrushTool brushTool = session.getBrush(player.getItemInHand(HandSide.MAIN_HAND).getType()); + if (brushTool == null) { + player.printError(TranslatableComponent.of("worldedit.brush.none.equipped")); + return; + } + brushTool.setTraceMask(mask); if (mask == null) { player.printInfo(TranslatableComponent.of("worldedit.tool.tracemask.disabled")); } else { 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 62fa0fb7f..a7e3fcb66 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 @@ -64,6 +64,19 @@ public BrushTool(String permission) { this.permission = permission; } + /** + * Construct the tool. + * + * @param brush the brush to bind + * @param permission the permission to check before use is allowed + */ + public BrushTool(Brush brush, String permission) { + checkNotNull(brush); + checkNotNull(permission); + this.brush = brush; + this.permission = permission; + } + @Override public boolean canUse(Actor player) { return player.hasPermission(permission); diff --git a/worldedit-core/src/main/resources/lang/strings.json b/worldedit-core/src/main/resources/lang/strings.json index 41b8436b1..84921ef86 100644 --- a/worldedit-core/src/main/resources/lang/strings.json +++ b/worldedit-core/src/main/resources/lang/strings.json @@ -39,6 +39,7 @@ "worldedit.brush.heightmap.unknown": "Unknown heightmap brush: {0}.", "worldedit.brush.morph.equip": "Morph brush shape equipped: {0}.", "worldedit.brush.none.equip": "Brush unbound from your current item.", + "worldedit.brush.none.equipped": "You have no brush bound to your current item. Try /brush sphere for a basic brush.", "worldedit.setbiome.changed": "Biomes were changed for approximately {0} blocks.", "worldedit.setbiome.warning": "You may have to re-join your game (or close and re-open your world) to see changes.",