2021-09-11 15:33:45 +02:00
|
|
|
package net.knarcraft.stargate.thread;
|
|
|
|
|
|
|
|
import net.knarcraft.stargate.Stargate;
|
2021-09-20 13:56:30 +02:00
|
|
|
import net.knarcraft.stargate.container.BlockChangeRequest;
|
2021-09-11 15:33:45 +02:00
|
|
|
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
|
2021-09-20 13:56:30 +02:00
|
|
|
*
|
2021-09-11 15:33:45 +02:00
|
|
|
* @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
|
2021-09-20 13:56:30 +02:00
|
|
|
*
|
2021-09-11 15:33:45 +02:00
|
|
|
* @param block <p>The block to orient</p>
|
2021-09-20 13:56:30 +02:00
|
|
|
* @param axis <p>The axis to use for orienting the block</p>
|
2021-09-11 15:33:45 +02:00
|
|
|
*/
|
|
|
|
private void orientBlock(Block block, Axis axis) {
|
|
|
|
Orientable orientable = (Orientable) block.getBlockData();
|
|
|
|
orientable.setAxis(axis);
|
|
|
|
block.setBlockData(orientable);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|