Renames the blox populator and block populator thread as I finally understand what they actually do

This commit is contained in:
Kristian Knarvik 2021-09-11 15:33:45 +02:00
parent 87735e4935
commit 1c3dbbe81d
6 changed files with 159 additions and 152 deletions

View File

@ -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 <p>The location of the block to change</p>
* @param material <p>The new material to change the block to</p>
* @param axis <p>The new axis to orient the block along</p>
*/
public BlockChangeRequest(BlockLocation blockLocation, Material material, Axis axis) {
this.blockLocation = blockLocation;
newMaterial = material;
newAxis = axis;
}
/**
* Gets the location of the block to change
*
* @return <p>The location of the block</p>
*/
public BlockLocation getBlockLocation() {
return blockLocation;
}
/**
* Sets the location of the block
*
* @param blockLocation <p>The new location of the block</p>
*/
public void setBlockLocation(BlockLocation blockLocation) {
this.blockLocation = blockLocation;
}
/**
* Gets the material to change the block into
*
* @return <p>The material to change the block into</p>
*/
public Material getMaterial() {
return newMaterial;
}
/**
* Sets the material to change the block into
*
* @param material <p>The new material</p>
*/
public void setMaterial(Material material) {
newMaterial = material;
}
/**
* Gets the axis to orient the block along
*
* @return <p>The axis to orient the block along</p>
*/
public Axis getAxis() {
return newAxis;
}
/**
* Sets the axis to orient the block along
*
* @param axis <p>The new axis to orient the block along</p>
*/
public void setAxis(Axis axis) {
newAxis = axis;
}
}

View File

@ -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 <p>The location to start from</p>
* @param material <p>The material to populate</p>
* @param axis <p>The axis to populate along</p>
*/
public BloxPopulator(BlockLocation blockLocation, Material material, Axis axis) {
this.blockLocation = blockLocation;
nextMat = material;
nextAxis = axis;
}
/**
* Gets the location to start from
*
* @return <p>The location to start from</p>
*/
public BlockLocation getBlockLocation() {
return blockLocation;
}
/**
* Sets the location to start from
*
* @param blockLocation <p>The new start location</p>
*/
public void setBlockLocation(BlockLocation blockLocation) {
this.blockLocation = blockLocation;
}
/**
* Gets the material used for population
*
* @return <p>The material used for population</p>
*/
public Material getMaterial() {
return nextMat;
}
/**
* Sets the polulator material
*
* @param material <p>The new populator material</p>
*/
public void setMat(Material material) {
nextMat = material;
}
/**
* Gets the current population axis
*
* @return <p>The current population axis</p>
*/
public Axis getAxis() {
return nextAxis;
}
/**
* Sets the populator axis
*
* @param axis <p>The new populator axis</p>
*/
public void setAxis(Axis axis) {
nextAxis = axis;
}
}

View File

@ -16,7 +16,7 @@ import net.knarcraft.stargate.portal.GateHandler;
import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.Portal;
import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.PortalHandler;
import net.knarcraft.stargate.portal.PortalOption; 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.thread.StarGateThread;
import net.knarcraft.stargate.utility.EconomyHandler; import net.knarcraft.stargate.utility.EconomyHandler;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -70,7 +70,7 @@ public class Stargate extends JavaPlugin {
public static boolean permDebug = false; public static boolean permDebug = false;
public static final ConcurrentLinkedQueue<Portal> activeList = new ConcurrentLinkedQueue<>(); public static final ConcurrentLinkedQueue<Portal> activeList = new ConcurrentLinkedQueue<>();
// Used for populating gate open/closed material. // Used for populating gate open/closed material.
public static final Queue<BloxPopulator> blockPopulatorQueue = new LinkedList<>(); public static final Queue<BlockChangeRequest> blockChangeRequestQueue = new LinkedList<>();
// HashMap of player names for Bungee support // HashMap of player names for Bungee support
public static final Map<String, String> bungeeQueue = new HashMap<>(); public static final Map<String, String> bungeeQueue = new HashMap<>();
// World names that contain stargates // 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 StarGateThread(), 0L, 100L);
getServer().getScheduler().runTaskTimer(this, new BlockPopulatorThread(), 0L, 1L); getServer().getScheduler().runTaskTimer(this, new BlockChangeThread(), 0L, 1L);
this.registerCommands(); this.registerCommands();
} }

View File

@ -1,7 +1,7 @@
package net.knarcraft.stargate.portal; package net.knarcraft.stargate.portal;
import net.knarcraft.stargate.BlockLocation; import net.knarcraft.stargate.BlockLocation;
import net.knarcraft.stargate.BloxPopulator; import net.knarcraft.stargate.BlockChangeRequest;
import net.knarcraft.stargate.RelativeBlockVector; import net.knarcraft.stargate.RelativeBlockVector;
import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.event.StargateActivateEvent; import net.knarcraft.stargate.event.StargateActivateEvent;
@ -49,7 +49,8 @@ public class Portal {
private final int modX; private final int modX;
private final int modZ; private final int modZ;
private final float yaw; 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 // Block references
private final BlockLocation id; private final BlockLocation id;
@ -102,7 +103,7 @@ public class Portal {
this.modX = modX; this.modX = modX;
this.modZ = modZ; this.modZ = modZ;
this.yaw = yaw; 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.id = id;
this.destination = destination; this.destination = destination;
this.button = button; this.button = button;
@ -232,18 +233,6 @@ public class Portal {
return yaw; return yaw;
} }
/**
* Gets the axis the portal follows
*
* <p>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</p>
*
* @return <p>The axis the portal follows</p>
*/
public Axis getAxis() {
return rot;
}
/** /**
* Gets the player currently using this portal * Gets the player currently using this portal
* *
@ -502,9 +491,9 @@ public class Portal {
//Change the opening blocks to the correct type //Change the opening blocks to the correct type
Material openType = gate.getPortalOpenBlock(); Material openType = gate.getPortalOpenBlock();
Axis axis = (openType.createBlockData() instanceof Orientable) ? rot : null; Axis axis = (openType.createBlockData() instanceof Orientable) ? rotationAxis : null;
for (BlockLocation inside : getEntrances()) { for (BlockLocation inside : getEntrances()) {
Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, openType, axis)); Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, openType, axis));
} }
updatePortalOpenState(openFor); updatePortalOpenState(openFor);
@ -556,7 +545,7 @@ public class Portal {
// Close this gate, then the dest gate. // Close this gate, then the dest gate.
Material closedType = gate.getPortalClosedBlock(); Material closedType = gate.getPortalClosedBlock();
for (BlockLocation inside : getEntrances()) { for (BlockLocation inside : getEntrances()) {
Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, closedType, null)); Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, closedType, null));
} }
updatePortalClosedState(); updatePortalClosedState();

View File

@ -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
*
* <p>This thread fetches some entries from blockPopulateQueue each time it's called.</p>
*/
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 <p>The block to fix</p>
*/
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 <p>The block to orient</p>
* @param axis <p>The axis to use for orienting the block</p>
*/
private void orientBlock(Block block, Axis axis) {
Orientable orientable = (Orientable) block.getBlockData();
orientable.setAxis(axis);
block.setBlockData(orientable);
}
}

View File

@ -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
*
* <p>This thread fetches some entries from blockPopulatorQueue each time it's called.</p>
*/
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);
}
}
}
}