aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/threads
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/gregtech/api/threads')
-rw-r--r--src/main/java/gregtech/api/threads/GT_Runnable_Cable_Update.java71
-rw-r--r--src/main/java/gregtech/api/threads/GT_Runnable_MachineBlockUpdate.java16
2 files changed, 78 insertions, 9 deletions
diff --git a/src/main/java/gregtech/api/threads/GT_Runnable_Cable_Update.java b/src/main/java/gregtech/api/threads/GT_Runnable_Cable_Update.java
new file mode 100644
index 0000000000..6bc42e9cc8
--- /dev/null
+++ b/src/main/java/gregtech/api/threads/GT_Runnable_Cable_Update.java
@@ -0,0 +1,71 @@
+package gregtech.api.threads;
+
+import gregtech.GT_Mod;
+import gregtech.api.interfaces.tileentity.IMachineBlockUpdateable;
+import gregtech.api.metatileentity.BaseMetaPipeEntity;
+import gregtech.api.metatileentity.implementations.GT_MetaPipeEntity_Cable;
+import gregtech.common.GT_Proxy;
+import net.minecraft.tileentity.TileEntity;
+import net.minecraft.util.ChunkCoordinates;
+import net.minecraft.world.World;
+import net.minecraftforge.common.util.ForgeDirection;
+
+public class GT_Runnable_Cable_Update extends GT_Runnable_MachineBlockUpdate {
+ protected GT_Runnable_Cable_Update(World aWorld, ChunkCoordinates aCoords) {
+ super(aWorld, aCoords);
+ }
+
+ public static void setCableUpdateValues(World aWorld, ChunkCoordinates aCoords) {
+ if (isEnabled) {
+ EXECUTOR_SERVICE.submit(new GT_Runnable_Cable_Update(aWorld, aCoords));
+ }
+ }
+
+
+ @Override
+ public void run() {
+ try {
+ while (!tQueue.isEmpty()) {
+ final ChunkCoordinates aCoords = tQueue.poll();
+ final TileEntity tTileEntity;
+ final boolean isMachineBlock;
+
+ GT_Proxy.TICK_LOCK.lock();
+ try {
+ //we dont want to go over cables that are in unloaded chuncks
+ //keeping the lock just to make sure no CME happens
+ if (world.blockExists(aCoords.posX, aCoords.posY, aCoords.posZ)) {
+ tTileEntity = world.getTileEntity(aCoords.posX, aCoords.posY, aCoords.posZ);
+ } else {
+ tTileEntity = null;
+ }
+ } finally {
+ GT_Proxy.TICK_LOCK.unlock();
+ }
+
+ // See if the block itself needs an update
+ if (tTileEntity instanceof IMachineBlockUpdateable)
+ ((IMachineBlockUpdateable) tTileEntity).onMachineBlockUpdate();
+
+ // Now see if we should add the nearby blocks to the queue:
+ //only add blocks wich the cable is conected too
+ if (tTileEntity instanceof BaseMetaPipeEntity &&
+ ((BaseMetaPipeEntity) tTileEntity).getMetaTileEntity() instanceof GT_MetaPipeEntity_Cable)
+ {
+ ChunkCoordinates tCoords;
+ for (byte i = 0;i<6;i++) {
+ if (((GT_MetaPipeEntity_Cable) ((BaseMetaPipeEntity) tTileEntity).getMetaTileEntity()).isConnectedAtSide(i)) {
+ ForgeDirection offset = ForgeDirection.getOrientation(i);
+ if (visited.add(tCoords = new ChunkCoordinates(aCoords.posX + offset.offsetX,
+ aCoords.posY + offset.offsetY, aCoords.posZ + offset.offsetZ)))
+ tQueue.add(tCoords);
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ GT_Mod.GT_FML_LOGGER.error(
+ "Well this update was broken... " + mCoords + ", mWorld={" + world.getProviderName() + " @dimId " + world.provider.dimensionId + "}", e);
+ }
+ }
+}
diff --git a/src/main/java/gregtech/api/threads/GT_Runnable_MachineBlockUpdate.java b/src/main/java/gregtech/api/threads/GT_Runnable_MachineBlockUpdate.java
index 3ce1daf9b2..cc224b977d 100644
--- a/src/main/java/gregtech/api/threads/GT_Runnable_MachineBlockUpdate.java
+++ b/src/main/java/gregtech/api/threads/GT_Runnable_MachineBlockUpdate.java
@@ -19,10 +19,10 @@ import java.util.concurrent.TimeUnit;
public class GT_Runnable_MachineBlockUpdate implements Runnable {
// used by runner thread
- private final ChunkCoordinates mCoords;
- private final World world;
- private final Set<ChunkCoordinates> visited = new HashSet<>(80);
- private final Queue<ChunkCoordinates> tQueue = new LinkedList<>();
+ protected final ChunkCoordinates mCoords;
+ protected final World world;
+ protected final Set<ChunkCoordinates> visited = new HashSet<>(80);
+ protected final Queue<ChunkCoordinates> tQueue = new LinkedList<>();
// Threading
private static final ThreadFactory THREAD_FACTORY = r -> {
@@ -30,15 +30,14 @@ public class GT_Runnable_MachineBlockUpdate implements Runnable {
thread.setName("GT_MachineBlockUpdate");
return thread;
};
- private static ExecutorService EXECUTOR_SERVICE;
+ protected static ExecutorService EXECUTOR_SERVICE;
// This class should never be initiated outside of this class!
- private GT_Runnable_MachineBlockUpdate(World aWorld, ChunkCoordinates aCoords) {
+ protected GT_Runnable_MachineBlockUpdate(World aWorld, ChunkCoordinates aCoords) {
this.world = aWorld;
this.mCoords = aCoords;
visited.add(aCoords);
tQueue.add(aCoords);
-
}
public static boolean isEnabled() {
@@ -57,11 +56,10 @@ public class GT_Runnable_MachineBlockUpdate implements Runnable {
GT_Runnable_MachineBlockUpdate.isEnabled = isEnabled;
}
- private static boolean isEnabled = true;
+ protected static boolean isEnabled = true;
public static void setMachineUpdateValues(World aWorld, ChunkCoordinates aCoords) {
if (isEnabled) {
- aWorld.getTileEntity(aCoords.posX, aCoords.posY, aCoords.posZ);
EXECUTOR_SERVICE.submit(new GT_Runnable_MachineBlockUpdate(aWorld, aCoords));
}
}