-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TickManager.java
163 lines (144 loc) · 5.49 KB
/
TickManager.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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 TickManager {
public final int tileEntityRegionSize;
public final int entityRegionSize;
private final Object processChangesLock = 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> tileEntityCallables = new HashMap<Integer, TileEntityTickCallable>();
private final Map<Integer, EntityTickCallable> entityCallables = 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 TickManager(World world, int tileEntityRegionSize, int entityRegionSize) {
this.world = world;
this.tileEntityRegionSize = tileEntityRegionSize;
this.entityRegionSize = entityRegionSize;
}
public TickCallable getTileEntityCallable(int hashCode) {
return tileEntityCallables.get(hashCode);
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
private TileEntityTickCallable getOrCreateCallable(TileEntity tileEntity) {
int hashCode = getHashCode(tileEntity);
TileEntityTickCallable callable = tileEntityCallables.get(hashCode);
if (callable == null) {
callable = new TileEntityTickCallable<Object>(world, this, tileEntity.xCoord / tileEntityRegionSize, tileEntity.yCoord / tileEntityRegionSize);
tileEntityCallables.put(hashCode, callable);
tickCallables.add(callable);
}
return callable;
}
public TickCallable getEntityCallable(int hashCode) {
return entityCallables.get(hashCode);
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
private EntityTickCallable getOrCreateCallable(Entity entity) {
int regionX = (int) entity.posX / entityRegionSize;
int regionY = (int) entity.posY / entityRegionSize;
int hashCode = getHashCodeFromRegionCoords(regionX, regionY);
EntityTickCallable callable = entityCallables.get(hashCode);
if (callable == null) {
callable = new EntityTickCallable<Object>(world, this, regionX, regionY);
entityCallables.put(hashCode, callable);
tickCallables.add(callable);
}
return callable;
}
public int getHashCode(TileEntity tileEntity) {
return getHashCode(tileEntity.xCoord, tileEntity.yCoord);
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
public int getHashCode(Entity entity) {
return getHashCode((int) entity.posX, (int) entity.posY);
}
public int getHashCode(int x, int y) {
return getHashCodeFromRegionCoords(x / tileEntityRegionSize, y / tileEntityRegionSize);
}
public static int getHashCodeFromRegionCoords(int x, int y) {
return x + (y << 16);
}
private synchronized void processChanges() {
try {
synchronized (processChangesLock) {
for (TileEntity tileEntity : toRemoveTileEntities) {
getOrCreateCallable(tileEntity).remove(tileEntity);
}
for (TileEntity tileEntity : toAddTileEntities) {
getOrCreateCallable(tileEntity).add(tileEntity);
}
for (Entity entity : toRemoveEntities) {
getOrCreateCallable(entity).remove(entity);
}
for (Entity entity : toAddEntities) {
getOrCreateCallable(entity).add(entity);
}
toAddEntities.clear();
toAddTileEntities.clear();
toRemoveEntities.clear();
toRemoveTileEntities.clear();
Iterator<TickCallable<Object>> iterator = tickCallables.iterator();
while (iterator.hasNext()) {
TickCallable<Object> tickCallable = iterator.next();
if (tickCallable.isEmpty()) {
iterator.remove();
if (tickCallable instanceof EntityTickCallable) {
entityCallables.remove(tickCallable.hashCode);
} else {
tileEntityCallables.remove(tickCallable.hashCode);
}
tickCallable.die();
}
}
}
} catch (Exception e) {
Log.severe("Exception occurred 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 (processChangesLock) {
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();
}
}