diff --git a/src/main/java/net/knarcraft/stargate/BlockChangeRequest.java b/src/main/java/net/knarcraft/stargate/BlockChangeRequest.java new file mode 100644 index 0000000..b6b0c3d --- /dev/null +++ b/src/main/java/net/knarcraft/stargate/BlockChangeRequest.java @@ -0,0 +1,82 @@ +package net.knarcraft.stargate; + +import org.bukkit.Axis; +import org.bukkit.Material; + +/** + * Represents a request for changing a block into another material + */ +public class BlockChangeRequest { + + private BlockLocation blockLocation; + private Material newMaterial; + private Axis newAxis; + + /** + * Instantiates a new block change request + * + * @param blockLocation
The location of the block to change
+ * @param materialThe new material to change the block to
+ * @param axisThe new axis to orient the block along
+ */ + public BlockChangeRequest(BlockLocation blockLocation, Material material, Axis axis) { + this.blockLocation = blockLocation; + newMaterial = material; + newAxis = axis; + } + + /** + * Gets the location of the block to change + * + * @returnThe location of the block
+ */ + public BlockLocation getBlockLocation() { + return blockLocation; + } + + /** + * Sets the location of the block + * + * @param blockLocationThe new location of the block
+ */ + public void setBlockLocation(BlockLocation blockLocation) { + this.blockLocation = blockLocation; + } + + /** + * Gets the material to change the block into + * + * @returnThe material to change the block into
+ */ + public Material getMaterial() { + return newMaterial; + } + + /** + * Sets the material to change the block into + * + * @param materialThe new material
+ */ + public void setMaterial(Material material) { + newMaterial = material; + } + + /** + * Gets the axis to orient the block along + * + * @returnThe axis to orient the block along
+ */ + public Axis getAxis() { + return newAxis; + } + + /** + * Sets the axis to orient the block along + * + * @param axisThe new axis to orient the block along
+ */ + public void setAxis(Axis axis) { + newAxis = axis; + } + +} diff --git a/src/main/java/net/knarcraft/stargate/BloxPopulator.java b/src/main/java/net/knarcraft/stargate/BloxPopulator.java deleted file mode 100644 index a375762..0000000 --- a/src/main/java/net/knarcraft/stargate/BloxPopulator.java +++ /dev/null @@ -1,82 +0,0 @@ -package net.knarcraft.stargate; - -import org.bukkit.Axis; -import org.bukkit.Material; - -/** - * Used to store information about a custom block populator - */ -public class BloxPopulator { - - private BlockLocation blockLocation; - private Material nextMat; - private Axis nextAxis; - - /** - * Instantiates a new block populator - * - * @param blockLocationThe location to start from
- * @param materialThe material to populate
- * @param axisThe axis to populate along
- */ - public BloxPopulator(BlockLocation blockLocation, Material material, Axis axis) { - this.blockLocation = blockLocation; - nextMat = material; - nextAxis = axis; - } - - /** - * Gets the location to start from - * - * @returnThe location to start from
- */ - public BlockLocation getBlockLocation() { - return blockLocation; - } - - /** - * Sets the location to start from - * - * @param blockLocationThe new start location
- */ - public void setBlockLocation(BlockLocation blockLocation) { - this.blockLocation = blockLocation; - } - - /** - * Gets the material used for population - * - * @returnThe material used for population
- */ - public Material getMaterial() { - return nextMat; - } - - /** - * Sets the polulator material - * - * @param materialThe new populator material
- */ - public void setMat(Material material) { - nextMat = material; - } - - /** - * Gets the current population axis - * - * @returnThe current population axis
- */ - public Axis getAxis() { - return nextAxis; - } - - /** - * Sets the populator axis - * - * @param axisThe new populator axis
- */ - public void setAxis(Axis axis) { - nextAxis = axis; - } - -} diff --git a/src/main/java/net/knarcraft/stargate/Stargate.java b/src/main/java/net/knarcraft/stargate/Stargate.java index 5686cf7..b584bff 100644 --- a/src/main/java/net/knarcraft/stargate/Stargate.java +++ b/src/main/java/net/knarcraft/stargate/Stargate.java @@ -16,7 +16,7 @@ import net.knarcraft.stargate.portal.GateHandler; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.PortalOption; -import net.knarcraft.stargate.thread.BlockPopulatorThread; +import net.knarcraft.stargate.thread.BlockChangeThread; import net.knarcraft.stargate.thread.StarGateThread; import net.knarcraft.stargate.utility.EconomyHandler; import org.bukkit.Bukkit; @@ -70,7 +70,7 @@ public class Stargate extends JavaPlugin { public static boolean permDebug = false; public static final ConcurrentLinkedQueueIf a relative vector's right is not zero, it will move along this axis. - * The axis is used to place the portal's open blocks correctly
- * - * @returnThe axis the portal follows
- */ - public Axis getAxis() { - return rot; - } - /** * Gets the player currently using this portal * @@ -502,9 +491,9 @@ public class Portal { //Change the opening blocks to the correct type Material openType = gate.getPortalOpenBlock(); - Axis axis = (openType.createBlockData() instanceof Orientable) ? rot : null; + Axis axis = (openType.createBlockData() instanceof Orientable) ? rotationAxis : null; for (BlockLocation inside : getEntrances()) { - Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, openType, axis)); + Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, openType, axis)); } updatePortalOpenState(openFor); @@ -556,7 +545,7 @@ public class Portal { // Close this gate, then the dest gate. Material closedType = gate.getPortalClosedBlock(); for (BlockLocation inside : getEntrances()) { - Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, closedType, null)); + Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, closedType, null)); } updatePortalClosedState(); diff --git a/src/main/java/net/knarcraft/stargate/thread/BlockChangeThread.java b/src/main/java/net/knarcraft/stargate/thread/BlockChangeThread.java new file mode 100644 index 0000000..7a4ab10 --- /dev/null +++ b/src/main/java/net/knarcraft/stargate/thread/BlockChangeThread.java @@ -0,0 +1,67 @@ +package net.knarcraft.stargate.thread; + +import net.knarcraft.stargate.BlockChangeRequest; +import net.knarcraft.stargate.Stargate; +import org.bukkit.Axis; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.block.EndGateway; +import org.bukkit.block.data.Orientable; + +/** + * This thread changes gate blocks to display a gate as open or closed + * + *This thread fetches some entries from blockPopulateQueue each time it's called.
+ */ +public class BlockChangeThread implements Runnable { + + @Override + public void run() { + long sTime = System.nanoTime(); + //Repeat for at most 0.025 seconds + while (System.nanoTime() - sTime < 25000000) { + //Abort if there's no work to be done + BlockChangeRequest blockChangeRequest = Stargate.blockChangeRequestQueue.poll(); + if (blockChangeRequest == null) { + return; + } + + //Change the material of the pulled block + Block block = blockChangeRequest.getBlockLocation().getBlock(); + block.setType(blockChangeRequest.getMaterial(), false); + + if (blockChangeRequest.getMaterial() == Material.END_GATEWAY && + block.getWorld().getEnvironment() == World.Environment.THE_END) { + //Force a specific location to prevent exit gateway generation + fixEndGatewayGate(block); + } else if (blockChangeRequest.getAxis() != null) { + //If orientation is relevant, adjust the block's orientation + orientBlock(block, blockChangeRequest.getAxis()); + } + } + } + + /** + * Prevents end gateway portal from behaving strangely + * @param blockThe block to fix
+ */ + private void fixEndGatewayGate(Block block) { + EndGateway gateway = (EndGateway) block.getState(); + gateway.setExitLocation(block.getLocation()); + gateway.setExactTeleport(true); + gateway.update(false, false); + } + + /** + * Sets the orientation axis of the placed block + * @param blockThe block to orient
+ * @param axisThe axis to use for orienting the block
+ */ + private void orientBlock(Block block, Axis axis) { + Orientable orientable = (Orientable) block.getBlockData(); + orientable.setAxis(axis); + block.setBlockData(orientable); + } + +} diff --git a/src/main/java/net/knarcraft/stargate/thread/BlockPopulatorThread.java b/src/main/java/net/knarcraft/stargate/thread/BlockPopulatorThread.java deleted file mode 100644 index 61be215..0000000 --- a/src/main/java/net/knarcraft/stargate/thread/BlockPopulatorThread.java +++ /dev/null @@ -1,49 +0,0 @@ -package net.knarcraft.stargate.thread; - -import net.knarcraft.stargate.BloxPopulator; -import net.knarcraft.stargate.Stargate; -import org.bukkit.Material; -import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.block.EndGateway; -import org.bukkit.block.data.Orientable; - -/** - * This thread changes gate blocks to display a gate as open or closed - * - *This thread fetches some entries from blockPopulatorQueue each time it's called.
- */ -public class BlockPopulatorThread implements Runnable { - - @Override - public void run() { - long sTime = System.nanoTime(); - //Repeat for at most 0.025 seconds - while (System.nanoTime() - sTime < 25000000) { - //Abort if there's no work to be done - BloxPopulator bloxPopulator = Stargate.blockPopulatorQueue.poll(); - if (bloxPopulator == null) { - return; - } - - //Change the material of the pulled block - Block block = bloxPopulator.getBlockLocation().getBlock(); - block.setType(bloxPopulator.getMaterial(), false); - - if (bloxPopulator.getMaterial() == Material.END_GATEWAY && - block.getWorld().getEnvironment() == World.Environment.THE_END) { - //Force a specific location to prevent exit gateway generation - EndGateway gateway = (EndGateway) block.getState(); - gateway.setExitLocation(block.getLocation()); - gateway.setExactTeleport(true); - gateway.update(false, false); - } else if (bloxPopulator.getAxis() != null) { - //If orientation is relevant, adjust the block's orientation - Orientable orientable = (Orientable) block.getBlockData(); - orientable.setAxis(bloxPopulator.getAxis()); - block.setBlockData(orientable); - } - } - } - -}