From 122236f6c7835ff8e74c1c9e51c6c0e09ca38caf Mon Sep 17 00:00:00 2001 From: matt <4009945+MattBDev@users.noreply.github.com> Date: Tue, 2 Apr 2019 23:36:32 -0400 Subject: [PATCH] Too lazy to write a commit message --- .../java/com/sk89q/worldedit/EditSession.java | 39 ++-- .../extension/platform/PlatformManager.java | 179 +++++++----------- .../function/pattern/BlockPattern.java | 3 +- .../sk89q/worldedit/math/BlockVector3.java | 4 +- .../com/sk89q/worldedit/math/Vector3.java | 31 ++- 5 files changed, 111 insertions(+), 145 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java index bc6ed024a..3181fde02 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -1221,20 +1221,12 @@ public class EditSession extends AbstractDelegateExtent implements HasFaweQueue, @Override public BlockVector3 getMinimumPoint() { - if (extent != null) { - return this.extent.getMinimumPoint(); - } else { - return BlockVector3.at(-30000000, 0, -30000000); - } + return getWorld().getMinimumPoint(); } @Override public BlockVector3 getMaximumPoint() { - if (extent != null) { - return this.extent.getMaximumPoint(); - } else { - return BlockVector3.at(30000000, 255, 30000000); - } + return getWorld().getMaximumPoint(); } @Override @@ -1364,7 +1356,7 @@ public class EditSession extends AbstractDelegateExtent implements HasFaweQueue, * @return the number of blocks that matched the pattern */ public int countBlocks(final Region region, final Set searchBlocks) { - final Mask mask = new BlockMaskBuilder().addBlocks(searchBlocks).build(extent); + Mask mask = new BlockMaskBuilder().addBlocks(searchBlocks).build(extent); RegionVisitor visitor = new RegionVisitor(region, new RegionFunction() { @Override public boolean apply(BlockVector3 position) throws WorldEditException { @@ -1516,10 +1508,11 @@ public class EditSession extends AbstractDelegateExtent implements HasFaweQueue, checkArgument(apothem >= 1, "apothem >= 1"); checkArgument(height >= 1, "height >= 1"); - final Region region = new CuboidRegion(this.getWorld(), // Causes clamping of Y range + Region region = new CuboidRegion( + getWorld(), // Causes clamping of Y range position.add(-apothem + 1, 0, -apothem + 1), position.add(apothem - 1, height - 1, apothem - 1)); - final Pattern pattern = BlockTypes.AIR.getDefaultState(); - return this.setBlocks(region, pattern); + Pattern pattern = new BlockPattern(BlockTypes.AIR.getDefaultState()); + return setBlocks(region, pattern); } /** @@ -1536,10 +1529,10 @@ public class EditSession extends AbstractDelegateExtent implements HasFaweQueue, checkArgument(apothem >= 1, "apothem >= 1"); checkArgument(height >= 1, "height >= 1"); - final Region region = new CuboidRegion(this.getWorld(), // Causes clamping of Y range + Region region = new CuboidRegion(getWorld(), // Causes clamping of Y range position.add(-apothem + 1, 0, -apothem + 1), position.add(apothem - 1, -height + 1, apothem - 1)); - final Pattern pattern = BlockTypes.AIR.getDefaultState(); - return this.setBlocks(region, pattern); + Pattern pattern = new BlockPattern(BlockTypes.AIR.getDefaultState()); + return setBlocks(region, pattern); } /** @@ -1629,9 +1622,8 @@ public class EditSession extends AbstractDelegateExtent implements HasFaweQueue, Operations.completeBlindly(visitor); this.changes += visitor.getAffected(); } else { - Iterator iter = region.iterator(); - while (iter.hasNext()) { - if (this.extent.setBlock(iter.next(), block)) { + for (BlockVector3 blockVector3 : region) { + if (this.extent.setBlock(blockVector3, block)) { changes++; } } @@ -1704,8 +1696,7 @@ public class EditSession extends AbstractDelegateExtent implements HasFaweQueue, * @return number of blocks affected * @throws MaxChangedBlocksException thrown if too many blocks are changed */ - @SuppressWarnings("deprecation") - public int replaceBlocks(final Region region, final Mask mask, final Pattern pattern) { + public int replaceBlocks(Region region, Mask mask, Pattern pattern) throws MaxChangedBlocksException { checkNotNull(region); checkNotNull(mask); checkNotNull(pattern); @@ -3363,9 +3354,9 @@ public class EditSession extends AbstractDelegateExtent implements HasFaweQueue, } private void setExistingBlocks(BlockVector3 pos1, BlockVector3 pos2) { - for (int x = (int) pos1.getX(); x <= (int) pos2.getX(); x++) { + for (int x = pos1.getX(); x <= pos2.getX(); x++) { for (int z = pos1.getBlockZ(); z <= pos2.getBlockZ(); z++) { - for (int y = (int) pos1.getY(); y <= (int) pos2.getY(); y++) { + for (int y = pos1.getY(); y <= pos2.getY(); y++) { int from = queue.getCombinedId4Data(x, y, z); queue.setBlock(x, y, z, from); if (BlockTypes.getFromStateId(from).getMaterial().hasContainer()) { 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 94caacaca..207947a9e 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 @@ -43,6 +43,7 @@ import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.event.platform.*; import com.sk89q.worldedit.extension.platform.permission.ActorSelectorLimits; import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.math.Vector3; import com.sk89q.worldedit.regions.RegionSelector; import com.sk89q.worldedit.session.request.Request; import com.sk89q.worldedit.util.HandSide; @@ -276,7 +277,7 @@ public class PlatformManager { /** * Get the current configuration. - *

+ * *

If no platform has been registered yet, then a default configuration * will be returned.

* @@ -304,142 +305,113 @@ public class PlatformManager { public void handleBlockInteract(BlockInteractEvent event) { // Create a proxy actor with a potentially different world for // making changes to the world - Request.reset(); - final Actor actor = createProxyActor(event.getCause()); + Actor actor = createProxyActor(event.getCause()); + + Location location = event.getLocation(); + try { - final Location location = event.getLocation(); - final BlockVector3 vector = location.toBlockPoint(); + Vector3 vector = location.toVector(); // At this time, only handle interaction from players if (actor instanceof Player) { - final LocalSession session = worldEdit.getSessionManager().get(actor); - Player playerActor = (Player) actor; + Player player = (Player) actor; + LocalSession session = worldEdit.getSessionManager().get(actor); + Request.reset(); VirtualWorld virtual = session.getVirtualWorld(); if (virtual != null) { - virtual.handleBlockInteract(playerActor, vector, event); + virtual.handleBlockInteract(player, vector.toBlockPoint(), event); if (event.isCancelled()) return; } if (event.getType() == Interaction.HIT) { - if (session.isToolControlEnabled() && playerActor.getItemInHand(HandSide.MAIN_HAND).getType().getId().equals(getConfiguration().wandItem)) { - FawePlayer fp = FawePlayer.wrap(playerActor); + if (player.getItemInHand(HandSide.MAIN_HAND).getType().getId().equals(getConfiguration().wandItem)) { + if (!session.isToolControlEnabled()) { + return; + } if (!actor.hasPermission("worldedit.selection.pos")) { return; } - final RegionSelector selector = session.getRegionSelector(playerActor.getWorld()); - final Player player = new LocationMaskedPlayerWrapper(PlayerWrapper.wrap((Player) actor), ((Player) actor).getLocation()); - fp.runAction(new Runnable() { - @Override - public void run() { - if (selector.selectPrimary(vector, ActorSelectorLimits.forActor(player))) { - selector.explainPrimarySelection(actor, session, vector); - } + FawePlayer fp = FawePlayer.wrap(player); + RegionSelector selector = session.getRegionSelector(player.getWorld()); + final Player maskedPlayerWrapper = + new LocationMaskedPlayerWrapper(PlayerWrapper.wrap((Player) actor), + ((Player) actor).getLocation()); + BlockVector3 blockPoint = vector.toBlockPoint(); + fp.runAction(() -> { + if (selector.selectPrimary(blockPoint, + ActorSelectorLimits.forActor(maskedPlayerWrapper))) { + selector + .explainPrimarySelection(actor, session, blockPoint); } }, false, true); event.setCancelled(true); return; } - if (session.hasSuperPickAxe() && playerActor.isHoldingPickAxe()) { + if (player.isHoldingPickAxe() && session.hasSuperPickAxe()) { final BlockTool superPickaxe = session.getSuperPickaxe(); - if (superPickaxe != null && superPickaxe.canUse(playerActor)) { - FawePlayer fp = FawePlayer.wrap(playerActor); - final Player player = new LocationMaskedPlayerWrapper(PlayerWrapper.wrap((Player) actor), ((Player) actor).getLocation()); - fp.runAction(new Runnable() { - @Override - public void run() { - reset(superPickaxe).actPrimary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), player, session, location); - } - }, false, true); + if (superPickaxe != null && superPickaxe.canUse(player)) { + FawePlayer fp = FawePlayer.wrap(player); + final Player maskedPlayerWrapper = new LocationMaskedPlayerWrapper(PlayerWrapper.wrap((Player) actor), ((Player) actor).getLocation()); + fp.runAction(() -> reset(superPickaxe).actPrimary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), maskedPlayerWrapper, session, location), false, true); event.setCancelled(true); return; } } -//<<<<<<< HEAD - final Tool tool = session.getTool(playerActor); - if (tool != null && tool instanceof DoubleActionBlockTool) { - if (tool.canUse(playerActor)) { - FawePlayer fp = FawePlayer.wrap(playerActor); - final Player player = new LocationMaskedPlayerWrapper(PlayerWrapper.wrap((Player) actor), ((Player) actor).getLocation()); - fp.runAction(new Runnable() { - @Override - public void run() { - reset(((DoubleActionBlockTool) tool)).actSecondary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), player, session, location); - } - }, false, true); + Tool tool = session.getTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + if (tool instanceof DoubleActionBlockTool) { + if (tool.canUse(player)) { + FawePlayer fp = FawePlayer.wrap(player); + final Player maskedPlayerWrapper = new LocationMaskedPlayerWrapper(PlayerWrapper.wrap((Player) actor), ((Player) actor).getLocation()); + fp.runAction(() -> reset(((DoubleActionBlockTool) tool)).actSecondary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), maskedPlayerWrapper, session, location), false, true); event.setCancelled(true); return; } -//======= -// -// RegionSelector selector = session.getRegionSelector(player.getWorld()); -// -// BlockVector3 blockPoint = vector.toBlockPoint(); -// if (selector.selectPrimary(blockPoint, ActorSelectorLimits.forActor(player))) { -// selector.explainPrimarySelection(actor, session, blockPoint); -//>>>>>>> 399e0ad5... Refactor vector system to be cleaner } } else if (event.getType() == Interaction.OPEN) { - if (session.isToolControlEnabled() && playerActor.getItemInHand(HandSide.MAIN_HAND).getType().getId().equals(getConfiguration().wandItem)) { - FawePlayer fp = FawePlayer.wrap(playerActor); + if (player.getItemInHand(HandSide.MAIN_HAND).getType().getId().equals(getConfiguration().wandItem)) { + if (!session.isToolControlEnabled()) { + return; + } if (!actor.hasPermission("worldedit.selection.pos")) { return; } + FawePlayer fp = FawePlayer.wrap(player); if (fp.checkAction()) { - final RegionSelector selector = session.getRegionSelector(playerActor.getWorld()); - final Player player = new LocationMaskedPlayerWrapper(PlayerWrapper.wrap((Player) actor), ((Player) actor).getLocation()); - fp.runAction(new Runnable() { - @Override - public void run() { - if (selector.selectSecondary(vector, ActorSelectorLimits.forActor(player))) { - selector.explainSecondarySelection(actor, session, vector); - } + RegionSelector selector = session.getRegionSelector(player.getWorld()); + Player maskedPlayerWrapper = new LocationMaskedPlayerWrapper( + PlayerWrapper.wrap((Player) actor), + ((Player) actor).getLocation()); + BlockVector3 blockPoint = vector.toBlockPoint(); + fp.runAction(() -> { + if (selector.selectSecondary(blockPoint, + ActorSelectorLimits.forActor(maskedPlayerWrapper))) { + selector.explainSecondarySelection(actor, session, + blockPoint); } }, false, true); } event.setCancelled(true); return; } - -//<<<<<<< HEAD - final Tool tool = session.getTool(playerActor); - if (tool != null && tool instanceof BlockTool) { - if (tool.canUse(playerActor)) { - FawePlayer fp = FawePlayer.wrap(playerActor); + Tool tool = session.getTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + if (tool instanceof BlockTool) { + if (tool.canUse(player)) { + FawePlayer fp = FawePlayer.wrap(player); if (fp.checkAction()) { - final Player player = new LocationMaskedPlayerWrapper(PlayerWrapper.wrap((Player) actor), ((Player) actor).getLocation()); - fp.runAction(new Runnable() { - @Override - public void run() { - if (tool instanceof BrushTool) { - ((BlockTool) tool).actPrimary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), player, session, location); - } else { - reset((BlockTool) tool).actPrimary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), player, session, location); - } + final Player maskedPlayerWrapper = new LocationMaskedPlayerWrapper(PlayerWrapper.wrap((Player) actor), ((Player) actor).getLocation()); + fp.runAction(() -> { + if (tool instanceof BrushTool) { + ((BlockTool) tool).actPrimary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), maskedPlayerWrapper, session, location); + } else { + reset((BlockTool) tool).actPrimary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), maskedPlayerWrapper, session, location); } }, false, true); event.setCancelled(true); return; } } -//======= -// RegionSelector selector = session.getRegionSelector(player.getWorld()); -// BlockVector3 blockPoint = vector.toBlockPoint(); -// if (selector.selectSecondary(blockPoint, ActorSelectorLimits.forActor(player))) { -// selector.explainSecondarySelection(actor, session, blockPoint); -// } -// -// event.setCancelled(true); -// return; -// } -// -// Tool tool = session.getTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); -// if (tool instanceof BlockTool) { -// if (tool.canUse(player)) { -// ((BlockTool) tool).actPrimary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), player, session, location); -// event.setCancelled(true); -//>>>>>>> 399e0ad5... Refactor vector system to be cleaner } } } @@ -459,7 +431,6 @@ public class PlatformManager { } } - @SuppressWarnings("deprecation") @Subscribe public void handlePlayerInput(PlayerInputEvent event) { // Create a proxy actor with a potentially different world for @@ -477,7 +448,7 @@ public class PlatformManager { try { switch (event.getInputType()) { case PRIMARY: { - if (player.getItemInHand(HandSide.MAIN_HAND).getType().equals(getConfiguration().navigationWand)) { + if (player.getItemInHand(HandSide.MAIN_HAND).getType().getId().equals(getConfiguration().navigationWand)) { if (getConfiguration().navigationWandMaxDistance <= 0) { return; } @@ -497,16 +468,11 @@ public class PlatformManager { return; } - Tool tool = session.getTool(player); - if (tool != null && tool instanceof DoubleActionTraceTool) { + Tool tool = session.getTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + if (tool instanceof DoubleActionTraceTool) { if (tool.canUse(player)) { FawePlayer fp = FawePlayer.wrap(player); - fp.runAsyncIfFree(new Runnable() { - @Override - public void run() { - reset((DoubleActionTraceTool) tool).actSecondary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), player, session); - } - }); + fp.runAsyncIfFree(() -> reset((DoubleActionTraceTool) tool).actSecondary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), player, session)); event.setCancelled(true); return; } @@ -515,7 +481,7 @@ public class PlatformManager { } case SECONDARY: { - if (player.getItemInHand(HandSide.MAIN_HAND).getType().equals(getConfiguration().navigationWand)) { + if (player.getItemInHand(HandSide.MAIN_HAND).getType().getId().equals(getConfiguration().navigationWand)) { if (getConfiguration().navigationWandMaxDistance <= 0) { return; } @@ -532,16 +498,11 @@ public class PlatformManager { return; } - Tool tool = session.getTool(player); - if (tool != null && tool instanceof TraceTool) { + Tool tool = session.getTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); + if (tool instanceof TraceTool) { if (tool.canUse(player)) { FawePlayer fp = FawePlayer.wrap(player); - fp.runAction(new Runnable() { - @Override - public void run() { - reset((TraceTool) tool).actPrimary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), player, session); - } - }, false, true); + fp.runAction(() -> reset((TraceTool) tool).actPrimary(queryCapability(Capability.WORLD_EDITING), getConfiguration(), player, session), false, true); event.setCancelled(true); return; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/BlockPattern.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/BlockPattern.java index 7a41cc062..8f0560364 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/BlockPattern.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/BlockPattern.java @@ -27,8 +27,7 @@ import com.sk89q.worldedit.world.block.BlockStateHolder; /** * A pattern that returns the same {@link BaseBlock} each time. */ -@Deprecated -public class BlockPattern implements Pattern { +public class BlockPattern extends AbstractPattern { private BaseBlock block; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java b/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java index 367efdb69..d9dba5d55 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java @@ -29,7 +29,7 @@ import java.util.Comparator; /** * An immutable 3-dimensional vector. */ -public class BlockVector3 { +public final class BlockVector3 { public static final BlockVector3 ZERO = new BlockVector3(0, 0, 0); public static final BlockVector3 UNIT_X = new BlockVector3(1, 0, 0); @@ -641,4 +641,4 @@ public class BlockVector3 { return "(" + x + ", " + y + ", " + z + ")"; } -} \ No newline at end of file +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/math/Vector3.java b/worldedit-core/src/main/java/com/sk89q/worldedit/math/Vector3.java index 172a75687..37b626664 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/math/Vector3.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/math/Vector3.java @@ -31,7 +31,7 @@ import java.util.Comparator; /** * An immutable 3-dimensional vector. */ -public class Vector3 { +public final class Vector3 { public static final Vector3 ZERO = new Vector3(0, 0, 0); public static final Vector3 UNIT_X = new Vector3(1, 0, 0); @@ -40,6 +40,21 @@ public class Vector3 { public static final Vector3 ONE = new Vector3(1, 1, 1); public static Vector3 at(double x, double y, double z) { + // switch for efficiency on typical cases + // in MC y is rarely 0/1 on selections + int yTrunc = (int) y; + switch (yTrunc) { + case 0: + if (x == 0 && y == 0 && z == 0) { + return ZERO; + } + break; + case 1: + if (x == 1 && y == 1 && z == 1) { + return ONE; + } + break; + } return new Vector3(x, y, z); } @@ -622,26 +637,26 @@ public class Vector3 { @Override public boolean equals(Object obj) { - if (obj == this) return true; if (!(obj instanceof Vector3)) { return false; } Vector3 other = (Vector3) obj; - return other.getX() == this.getX() && other.getZ() == this.getZ() && other.getY() == this.getY(); + return other.x == this.x && other.y == this.y && other.z == this.z; } @Override public int hashCode() { - return ((int) getX() ^ ((int) getZ() << 16)) ^ ((int) getY() << 30); + int hash = 17; + hash = 31 * hash + Double.hashCode(x); + hash = 31 * hash + Double.hashCode(y); + hash = 31 * hash + Double.hashCode(z); + return hash; } @Override public String toString() { - String x = (getX() == getBlockX() ? "" + getBlockX() : "" + getX()); - String y = (getY() == getBlockY() ? "" + getBlockY() : "" + getY()); - String z = (getZ() == getBlockZ() ? "" + getBlockZ() : "" + getZ()); return "(" + x + ", " + y + ", " + z + ")"; } -} \ No newline at end of file +}