From 1c3dbbe81d3b2db53e6249e39cda4a4d59d38414 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 11 Sep 2021 15:33:45 +0200 Subject: [PATCH] Renames the blox populator and block populator thread as I finally understand what they actually do --- .../stargate/BlockChangeRequest.java | 82 +++++++++++++++++++ .../net/knarcraft/stargate/BloxPopulator.java | 82 ------------------- .../java/net/knarcraft/stargate/Stargate.java | 6 +- .../net/knarcraft/stargate/portal/Portal.java | 25 ++---- .../stargate/thread/BlockChangeThread.java | 67 +++++++++++++++ .../stargate/thread/BlockPopulatorThread.java | 49 ----------- 6 files changed, 159 insertions(+), 152 deletions(-) create mode 100644 src/main/java/net/knarcraft/stargate/BlockChangeRequest.java delete mode 100644 src/main/java/net/knarcraft/stargate/BloxPopulator.java create mode 100644 src/main/java/net/knarcraft/stargate/thread/BlockChangeThread.java delete mode 100644 src/main/java/net/knarcraft/stargate/thread/BlockPopulatorThread.java 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 material

The new material to change the block to

+ * @param axis

The 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 + * + * @return

The location of the block

+ */ + public BlockLocation getBlockLocation() { + return blockLocation; + } + + /** + * Sets the location of the block + * + * @param blockLocation

The new location of the block

+ */ + public void setBlockLocation(BlockLocation blockLocation) { + this.blockLocation = blockLocation; + } + + /** + * Gets the material to change the block into + * + * @return

The material to change the block into

+ */ + public Material getMaterial() { + return newMaterial; + } + + /** + * Sets the material to change the block into + * + * @param material

The new material

+ */ + public void setMaterial(Material material) { + newMaterial = material; + } + + /** + * Gets the axis to orient the block along + * + * @return

The axis to orient the block along

+ */ + public Axis getAxis() { + return newAxis; + } + + /** + * Sets the axis to orient the block along + * + * @param axis

The 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 blockLocation

The location to start from

- * @param material

The material to populate

- * @param axis

The 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 - * - * @return

The location to start from

- */ - public BlockLocation getBlockLocation() { - return blockLocation; - } - - /** - * Sets the location to start from - * - * @param blockLocation

The new start location

- */ - public void setBlockLocation(BlockLocation blockLocation) { - this.blockLocation = blockLocation; - } - - /** - * Gets the material used for population - * - * @return

The material used for population

- */ - public Material getMaterial() { - return nextMat; - } - - /** - * Sets the polulator material - * - * @param material

The new populator material

- */ - public void setMat(Material material) { - nextMat = material; - } - - /** - * Gets the current population axis - * - * @return

The current population axis

- */ - public Axis getAxis() { - return nextAxis; - } - - /** - * Sets the populator axis - * - * @param axis

The 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 ConcurrentLinkedQueue activeList = new ConcurrentLinkedQueue<>(); // Used for populating gate open/closed material. - public static final Queue blockPopulatorQueue = new LinkedList<>(); + public static final Queue blockChangeRequestQueue = new LinkedList<>(); // HashMap of player names for Bungee support public static final Map bungeeQueue = new HashMap<>(); // World names that contain stargates @@ -570,7 +570,7 @@ public class Stargate extends JavaPlugin { } getServer().getScheduler().runTaskTimer(this, new StarGateThread(), 0L, 100L); - getServer().getScheduler().runTaskTimer(this, new BlockPopulatorThread(), 0L, 1L); + getServer().getScheduler().runTaskTimer(this, new BlockChangeThread(), 0L, 1L); this.registerCommands(); } diff --git a/src/main/java/net/knarcraft/stargate/portal/Portal.java b/src/main/java/net/knarcraft/stargate/portal/Portal.java index 6069dbd..663e778 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Portal.java +++ b/src/main/java/net/knarcraft/stargate/portal/Portal.java @@ -1,7 +1,7 @@ package net.knarcraft.stargate.portal; import net.knarcraft.stargate.BlockLocation; -import net.knarcraft.stargate.BloxPopulator; +import net.knarcraft.stargate.BlockChangeRequest; import net.knarcraft.stargate.RelativeBlockVector; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.event.StargateActivateEvent; @@ -49,7 +49,8 @@ public class Portal { private final int modX; private final int modZ; private final float yaw; - private final Axis rot; + //The rotation axis is the axis along which the gate is placed. It's the cross axis of the button's axis + private final Axis rotationAxis; // Block references private final BlockLocation id; @@ -102,7 +103,7 @@ public class Portal { this.modX = modX; this.modZ = modZ; this.yaw = yaw; - this.rot = yaw == 0.0F || yaw == 180.0F ? Axis.X : Axis.Z; + this.rotationAxis = yaw == 0.0F || yaw == 180.0F ? Axis.X : Axis.Z; this.id = id; this.destination = destination; this.button = button; @@ -232,18 +233,6 @@ public class Portal { return yaw; } - /** - * Gets the axis the portal follows - * - *

If 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

- * - * @return

The 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 block

The 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 block

The block to orient

+ * @param axis

The 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); - } - } - } - -}