-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ThreadManager.java
145 lines (125 loc) · 4.99 KB
/
ThreadManager.java
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
135
136
137
138
139
140
141
142
143
144
145
package me.nallar.tickthreading.minecraft;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.minecraft.tickcallables.EntityTickCallable;
import me.nallar.tickthreading.minecraft.tickcallables.TickCallable;
import me.nallar.tickthreading.minecraft.tickcallables.TileEntityTickCallable;
import net.minecraft.src.Entity;
import net.minecraft.src.TileEntity;
import net.minecraft.src.World;
public class ThreadManager {
public final int tileEntityRegionSize;
public final int entityRegionSize;
private final Object changeProcessLock = new Object();
private final List<TileEntity> toRemoveTileEntities = new ArrayList<TileEntity>();
private final List<Entity> toRemoveEntities = new ArrayList<Entity>();
private final List<TileEntity> toAddTileEntities = new ArrayList<TileEntity>();
private final List<Entity> toAddEntities = new ArrayList<Entity>();
private final Map<Integer, TileEntityTickCallable> tileEntityRunnables = new HashMap<Integer, TileEntityTickCallable>();
private final Map<Integer, EntityTickCallable> entityRunnables = new HashMap<Integer, EntityTickCallable>();
private final List<TickCallable<Object>> tickCallables = new ArrayList<TickCallable<Object>>();
private final ExecutorService tickExecutor = Executors.newCachedThreadPool();
private final World world;
public ThreadManager(World world, int tileEntityRegionSize, int entityRegionSize) {
this.world = world;
this.tileEntityRegionSize = tileEntityRegionSize;
this.entityRegionSize = entityRegionSize;
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
private TileEntityTickCallable getRunnableForTileEntity(TileEntity tileEntity) {
int hashCode = getHashCode(tileEntity);
TileEntityTickCallable runnable = tileEntityRunnables.get(hashCode);
if (runnable == null) {
runnable = new TileEntityTickCallable<Object>(world, "region: x" + tileEntity.xCoord / tileEntityRegionSize + ",z" + tileEntity.zCoord / tileEntityRegionSize, this, hashCode);
tileEntityRunnables.put(hashCode, runnable);
tickCallables.add(runnable);
}
return runnable;
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
private EntityTickCallable getRunnableForEntity(Entity entity) {
int hashCode = getHashCode(entity);
EntityTickCallable runnable = entityRunnables.get(hashCode);
if (runnable == null) {
runnable = new EntityTickCallable<Object>(world, "region: x" + (int) entity.posX / tileEntityRegionSize + ",z" + (int) entity.posZ / tileEntityRegionSize, this, hashCode);
entityRunnables.put(hashCode, runnable);
tickCallables.add(runnable);
}
return runnable;
}
public int getHashCode(TileEntity tileEntity) {
return tileEntity.xCoord / tileEntityRegionSize + (tileEntity.zCoord / tileEntityRegionSize << 16);
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
public int getHashCode(Entity entity) {
return ((int) entity.posX) / entityRegionSize + (((int) entity.posY) / entityRegionSize << 16);
}
private synchronized void processChanges() {
try{
synchronized (changeProcessLock) {
for (TileEntity tileEntity : toAddTileEntities) {
getRunnableForTileEntity(tileEntity).add(tileEntity);
}
for (TileEntity tileEntity : toRemoveTileEntities) {
getRunnableForTileEntity(tileEntity).remove(tileEntity);
}
for (Entity entity : toAddEntities) {
getRunnableForEntity(entity).add(entity);
}
for (Entity entity : toRemoveEntities) {
getRunnableForEntity(entity).remove(entity);
}
Iterator<TickCallable<Object>> iterator = tickCallables.iterator();
while(iterator.hasNext()){
TickCallable tickCallable = iterator.next();
if(tickCallable.isEmpty()){
iterator.remove();
if(tickCallable instanceof EntityTickCallable){
entityRunnables.remove(tickCallable.hashCode);
} else {
tileEntityRunnables.remove(tickCallable.hashCode);
}
}
}
}
} catch(Exception e) {
Log.severe("Exception occured while processing entity changes: ", e);
}
}
public synchronized void add(TileEntity tileEntity) {
toAddTileEntities.add(tileEntity);
}
public synchronized void add(Entity entity) {
toAddEntities.add(entity);
}
public synchronized void remove(TileEntity tileEntity) {
toRemoveTileEntities.add(tileEntity);
}
public synchronized void remove(Entity entity) {
toRemoveEntities.add(entity);
}
public void doTick() {
synchronized (changeProcessLock) {
try {
tickExecutor.invokeAll(tickCallables);
} catch (InterruptedException e) {
Log.warning("Interrupted while ticking: ", e);
}
}
tickExecutor.submit(new Runnable() {
@Override
public void run() {
processChanges();
}
});
}
public void unload() {
tickExecutor.shutdown();
}
}