1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package gregtech.api.threads;
import gregtech.GT_Mod;
import gregtech.api.GregTech_API;
import gregtech.api.interfaces.tileentity.IMachineBlockUpdateable;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.world.World;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
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<>();
// Threading
private static final ThreadFactory THREAD_FACTORY = r -> {
Thread thread = new Thread(r);
thread.setName("GT_MachineBlockUpdate");
return thread;
};
private static ExecutorService EXECUTOR_SERVICE;
// This class should never be initiated outside of this class!
private GT_Runnable_MachineBlockUpdate(World aWorld, ChunkCoordinates aCoords) {
this.world = aWorld;
this.mCoords = aCoords;
visited.add(aCoords);
tQueue.add(aCoords);
}
public static boolean isEnabled() {
return isEnabled;
}
public static void setEnabled() {
GT_Runnable_MachineBlockUpdate.isEnabled = true;
}
public static void setDisabled() {
GT_Runnable_MachineBlockUpdate.isEnabled = false;
}
public static void setEnabled(boolean isEnabled) {
GT_Runnable_MachineBlockUpdate.isEnabled = isEnabled;
}
private 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));
}
}
public static void initExecutorService() {
EXECUTOR_SERVICE = Executors.newFixedThreadPool((Runtime.getRuntime().availableProcessors() * 2 / 3), THREAD_FACTORY);
}
public static void shutdownExecutorService() {
try {
GT_Mod.GT_FML_LOGGER.info("Shutting down Machine block update executor service");
EXECUTOR_SERVICE.shutdown(); // Disable new tasks from being submitted
// Wait a while for existing tasks to terminate
if (!EXECUTOR_SERVICE.awaitTermination(60, TimeUnit.SECONDS)) {
EXECUTOR_SERVICE.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!EXECUTOR_SERVICE.awaitTermination(60, TimeUnit.SECONDS)) {
GT_Mod.GT_FML_LOGGER.error("Well this didn't terminated well... GT_Runnable_MachineBlockUpdate.shutdownExecutorService");
}
}
} catch (InterruptedException ie) {
GT_Mod.GT_FML_LOGGER.error("Well this interruption got interrupted...", ie);
// (Re-)Cancel if current thread also interrupted
EXECUTOR_SERVICE.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}catch (Exception e){
GT_Mod.GT_FML_LOGGER.error("Well this didn't terminated well...", e);
// (Re-)Cancel in case
EXECUTOR_SERVICE.shutdownNow();
}finally {
GT_Mod.GT_FML_LOGGER.info("Leaving... GT_Runnable_MachineBlockUpdate.shutdownExecutorService");
}
}
@Override
public void run() {
try {
while (!tQueue.isEmpty()) {
final ChunkCoordinates aCoords = tQueue.poll();
TileEntity tTileEntity = world.getTileEntity(aCoords.posX, aCoords.posY, aCoords.posZ);
// 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:
// 1) If we've visited less than 5 blocks, then yes
// 2) If the tile says we should recursively updated (pipes don't, machine blocks do)
// 3) If the block at the coordinates is marked as a machine block
if (visited.size() < 5
|| (tTileEntity instanceof IMachineBlockUpdateable && ((IMachineBlockUpdateable) tTileEntity).isMachineBlockUpdateRecursive())
|| GregTech_API.isMachineBlock(world.getBlock(aCoords.posX, aCoords.posY, aCoords.posZ), world.getBlockMetadata(aCoords.posX, aCoords.posY, aCoords.posZ)))
{
ChunkCoordinates tCoords;
if (visited.add(tCoords = new ChunkCoordinates(aCoords.posX + 1, aCoords.posY, aCoords.posZ))) tQueue.add(tCoords);
if (visited.add(tCoords = new ChunkCoordinates(aCoords.posX - 1, aCoords.posY, aCoords.posZ))) tQueue.add(tCoords);
if (visited.add(tCoords = new ChunkCoordinates(aCoords.posX, aCoords.posY + 1, aCoords.posZ))) tQueue.add(tCoords);
if (visited.add(tCoords = new ChunkCoordinates(aCoords.posX, aCoords.posY - 1, aCoords.posZ))) tQueue.add(tCoords);
if (visited.add(tCoords = new ChunkCoordinates(aCoords.posX, aCoords.posY, aCoords.posZ + 1))) tQueue.add(tCoords);
if (visited.add(tCoords = new ChunkCoordinates(aCoords.posX, aCoords.posY, aCoords.posZ - 1))) 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);
}
}
}
|