Fixed some bugs and cleanup some code.

This commit is contained in:
Matthew Miller
2018-08-13 22:18:12 +10:00
parent f96487a2d1
commit e0e7778536
22 changed files with 92 additions and 394 deletions

View File

@ -37,11 +37,10 @@ import com.sk89q.worldedit.command.tool.LongRangeBuildTool;
import com.sk89q.worldedit.command.tool.QueryTool;
import com.sk89q.worldedit.command.tool.TreePlanter;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.util.TreeGenerator;
import com.sk89q.worldedit.world.block.BlockStateHolder;
public class ToolCommands {
private final WorldEdit we;
@ -111,18 +110,9 @@ public class ToolCommands {
max = 1
)
@CommandPermissions("worldedit.tool.replacer")
public void repl(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
ParserContext context = new ParserContext();
context.setActor(player);
context.setWorld(player.getWorld());
context.setSession(session);
context.setRestricted(true);
context.setPreferringWildcard(false);
BlockStateHolder targetBlock = we.getBlockFactory().parseFromInput(args.getString(0), context);
public void repl(Player player, LocalSession session, Pattern pattern) throws WorldEditException {
BaseItemStack itemStack = player.getItemInHand(HandSide.MAIN_HAND);
session.setTool(itemStack.getType(), new BlockReplacer(targetBlock));
session.setTool(itemStack.getType(), new BlockReplacer(pattern));
player.print("Block replacer tool bound to " + itemStack.getType().getName() + ".");
}
@ -149,22 +139,15 @@ public class ToolCommands {
max = 2
)
@CommandPermissions("worldedit.tool.flood-fill")
public void floodFill(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
public void floodFill(Player player, LocalSession session, Pattern pattern, int range) throws WorldEditException {
LocalConfiguration config = we.getConfiguration();
int range = args.getInteger(1);
if (range > config.maxSuperPickaxeSize) {
player.printError("Maximum range: " + config.maxSuperPickaxeSize);
return;
}
ParserContext context = new ParserContext();
context.setActor(player);
context.setWorld(player.getWorld());
context.setSession(session);
Pattern pattern = we.getPatternFactory().parseFromInput(args.getString(0), context);
BaseItemStack itemStack = player.getItemInHand(HandSide.MAIN_HAND);
session.setTool(itemStack.getType(), new FloodFillTool(range, pattern));
player.print("Block flood fill tool bound to " + itemStack.getType().getName() + ".");
@ -209,23 +192,20 @@ public class ToolCommands {
max = 2
)
@CommandPermissions("worldedit.tool.lrbuild")
public void longrangebuildtool(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
ParserContext context = new ParserContext();
context.setActor(player);
context.setWorld(player.getWorld());
context.setSession(session);
context.setRestricted(true);
context.setPreferringWildcard(false);
BlockStateHolder secondary = we.getBlockFactory().parseFromInput(args.getString(0), context);
BlockStateHolder primary = we.getBlockFactory().parseFromInput(args.getString(1), context);
public void longrangebuildtool(Player player, LocalSession session, Pattern secondary, Pattern primary) throws WorldEditException {
BaseItemStack itemStack = player.getItemInHand(HandSide.MAIN_HAND);
session.setTool(itemStack.getType(), new LongRangeBuildTool(primary, secondary));
player.print("Long-range building tool bound to " + itemStack.getType().getName() + ".");
player.print("Left-click set to " + secondary.getBlockType().getName() + "; right-click set to "
+ primary.getBlockType().getName() + ".");
String primaryName = "pattern";
String secondaryName = "pattern";
if (primary instanceof BlockPattern) {
primaryName = ((BlockPattern) primary).getBlock().getBlockType().getName();
}
if (secondary instanceof BlockPattern) {
secondaryName = ((BlockPattern) secondary).getBlock().getBlockType().getName();
}
player.print("Left-click set to " + secondaryName + "; right-click set to "
+ primaryName + ".");
}
}

View File

@ -23,22 +23,24 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
/**
* A mode that replaces one block.
*/
public class BlockReplacer implements DoubleActionBlockTool {
private BlockStateHolder targetBlock;
private Pattern pattern;
public BlockReplacer(BlockStateHolder targetBlock) {
this.targetBlock = targetBlock;
public BlockReplacer(Pattern pattern) {
this.pattern = pattern;
}
@Override
@ -53,7 +55,8 @@ public class BlockReplacer implements DoubleActionBlockTool {
EditSession editSession = session.createEditSession(player);
try {
editSession.setBlock(clicked.toVector(), targetBlock);
Vector position = clicked.toVector();
editSession.setBlock(position, pattern.apply(position));
} catch (MaxChangedBlocksException ignored) {
} finally {
if (bag != null) {
@ -69,11 +72,11 @@ public class BlockReplacer implements DoubleActionBlockTool {
@Override
public boolean actSecondary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) {
EditSession editSession = session.createEditSession(player);
targetBlock = (editSession).getBlock(clicked.toVector());
BlockType type = targetBlock.getBlockType();
BlockStateHolder targetBlock = editSession.getBlock(clicked.toVector());
if (type != null) {
player.print("Replacer tool switched to: " + type.getName());
if (targetBlock != null) {
pattern = new BlockPattern(targetBlock);
player.print("Replacer tool switched to: " + targetBlock.getBlockType().getName());
}
return true;

View File

@ -26,6 +26,7 @@ import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockTypes;
@ -35,10 +36,10 @@ import com.sk89q.worldedit.world.block.BlockTypes;
*/
public class LongRangeBuildTool extends BrushTool implements DoubleActionTraceTool {
private BlockStateHolder primary;
private BlockStateHolder secondary;
private Pattern primary;
private Pattern secondary;
public LongRangeBuildTool(BlockStateHolder primary, BlockStateHolder secondary) {
public LongRangeBuildTool(Pattern secondary, Pattern primary) {
super("worldedit.tool.lrbuild");
this.primary = primary;
this.secondary = secondary;
@ -55,7 +56,8 @@ public class LongRangeBuildTool extends BrushTool implements DoubleActionTraceTo
if (pos == null) return false;
EditSession eS = session.createEditSession(player);
try {
if (secondary.getBlockType() == BlockTypes.AIR) {
BlockStateHolder applied = secondary.apply(pos.toVector());
if (applied.getBlockType() == BlockTypes.AIR) {
eS.setBlock(pos.toVector(), secondary);
} else {
eS.setBlock(pos.getDirection(), secondary);
@ -74,7 +76,8 @@ public class LongRangeBuildTool extends BrushTool implements DoubleActionTraceTo
if (pos == null) return false;
EditSession eS = session.createEditSession(player);
try {
if (primary.getBlockType() == BlockTypes.AIR) {
BlockStateHolder applied = primary.apply(pos.toVector());
if (applied.getBlockType() == BlockTypes.AIR) {
eS.setBlock(pos.toVector(), primary);
} else {
eS.setBlock(pos.getDirection(), primary);