-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TileEntityTickRegion.java
261 lines (243 loc) · 7.79 KB
/
TileEntityTickRegion.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package nallar.tickthreading.minecraft.tickregion;
import nallar.collections.LinkedHashSetTempSetNoClear;
import nallar.tickthreading.Log;
import nallar.tickthreading.minecraft.TickManager;
import nallar.tickthreading.minecraft.profiling.EntityTickProfiler;
import nallar.tickthreading.util.TableFormatter;
import nallar.tickthreading.util.concurrent.SimpleMutex;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import java.util.*;
public class TileEntityTickRegion extends TickRegion {
private int checkTime = 0;
private final LinkedHashSetTempSetNoClear<TileEntity> tileEntitySet = new LinkedHashSetTempSetNoClear<TileEntity>();
public final SimpleMutex lock = new SimpleMutex();
public SimpleMutex xPlusLock;
public SimpleMutex xMinusLock;
public SimpleMutex zPlusLock;
public SimpleMutex zMinusLock;
public TileEntityTickRegion(World world, TickManager manager, int regionX, int regionY) {
super(world, manager, regionX, regionY);
setupLocks();
}
public synchronized void setupLocks() {
{
TileEntityTickRegion xPlus = manager.getTileEntityRegion(TickManager.getHashCodeFromRegionCoords(regionX + 1, regionZ));
if (xPlus != null) {
synchronized (xPlus) {
xPlus.xMinusLock = this.lock;
xPlusLock = xPlus.lock;
}
}
}
{
TileEntityTickRegion xMinus = manager.getTileEntityRegion(TickManager.getHashCodeFromRegionCoords(regionX - 1, regionZ));
if (xMinus != null) {
synchronized (xMinus) {
xMinus.xPlusLock = this.lock;
xMinusLock = xMinus.lock;
}
}
}
{
TileEntityTickRegion zPlus = manager.getTileEntityRegion(TickManager.getHashCodeFromRegionCoords(regionX, regionZ + 1));
if (zPlus != null) {
synchronized (zPlus) {
zPlus.zMinusLock = this.lock;
zPlusLock = zPlus.lock;
}
}
}
{
TileEntityTickRegion zMinus = manager.getTileEntityRegion(TickManager.getHashCodeFromRegionCoords(regionX, regionZ - 1));
if (zMinus != null) {
synchronized (zMinus) {
zMinus.zPlusLock = this.lock;
zMinusLock = zMinus.lock;
}
}
}
}
public synchronized void removeLocks() {
{
TileEntityTickRegion xPlus = manager.getTileEntityRegion(TickManager.getHashCodeFromRegionCoords(regionX + 1, regionZ));
if (xPlus != null) {
synchronized (xPlus) {
xPlus.xMinusLock = null;
xPlusLock = null;
}
}
}
{
TileEntityTickRegion xMinus = manager.getTileEntityRegion(TickManager.getHashCodeFromRegionCoords(regionX - 1, regionZ));
if (xMinus != null) {
synchronized (xMinus) {
xMinus.xPlusLock = null;
xMinusLock = null;
}
}
}
{
TileEntityTickRegion zPlus = manager.getTileEntityRegion(TickManager.getHashCodeFromRegionCoords(regionX, regionZ + 1));
if (zPlus != null) {
synchronized (zPlus) {
zPlus.zMinusLock = null;
zPlusLock = null;
}
}
}
{
TileEntityTickRegion zMinus = manager.getTileEntityRegion(TickManager.getHashCodeFromRegionCoords(regionX, regionZ - 1));
if (zMinus != null) {
synchronized (zMinus) {
zMinus.zPlusLock = null;
zMinusLock = null;
}
}
}
}
@Override
public void onRemove() {
removeLocks();
}
@Override
public synchronized boolean run() {
SimpleMutex xPlusLock = this.xPlusLock;
if (xPlusLock == null || xPlusLock.tryLock()) {
try {
SimpleMutex xMinusLock = this.xMinusLock;
if (xMinusLock == null || xMinusLock.tryLock()) {
try {
SimpleMutex zPlusLock = this.zPlusLock;
if (zPlusLock == null || zPlusLock.tryLock()) {
try {
SimpleMutex zMinusLock = this.zMinusLock;
if (zMinusLock == null || zMinusLock.tryLock()) {
try {
return super.run();
} finally {
if (zMinusLock != null) {
zMinusLock.unlock();
}
}
}
} finally {
if (zPlusLock != null) {
zPlusLock.unlock();
}
}
}
} finally {
if (xMinusLock != null) {
xMinusLock.unlock();
}
}
}
} finally {
if (xPlusLock != null) {
xPlusLock.unlock();
}
}
}
return false;
}
@Override
public void doTick() {
final TickManager manager = this.manager;
final boolean check = checkTime++ % 60 == 0;
final boolean profilingEnabled = manager.profilingEnabled || this.profilingEnabled;
EntityTickProfiler entityTickProfiler = profilingEnabled ? EntityTickProfiler.ENTITY_TICK_PROFILER : null;
long startTime = profilingEnabled ? System.nanoTime() : 0;
final Iterator<TileEntity> tileEntitiesIterator = tileEntitySet.startIteration();
try {
while (tileEntitiesIterator.hasNext()) {
final TileEntity tileEntity = tileEntitiesIterator.next();
if (check && check(tileEntity, tileEntitiesIterator)) {
continue;
}
try {
if (tileEntity.isInvalid()) {
tileEntitiesIterator.remove();
invalidate(tileEntity);
} else if (tileEntity.worldObj != null) {
tileEntity.updateEntity();
}
} catch (Throwable throwable) {
Log.severe("Exception ticking TileEntity " + Log.toString(tileEntity), throwable);
} finally {
if (profilingEnabled) {
long oldStartTime = startTime;
entityTickProfiler.record(tileEntity, (startTime = System.nanoTime()) - oldStartTime);
}
}
}
} finally {
tileEntitySet.done();
}
}
private boolean check(final TileEntity tileEntity, final Iterator tileEntitiesIterator) {
final int xPos = tileEntity.xCoord;
final int zPos = tileEntity.zCoord;
if (TickManager.getHashCode(xPos, zPos) != hashCode) {
tileEntitiesIterator.remove();
if (tileEntity.isInvalid() || !world.getChunkProvider().chunkExists(xPos >> 4, zPos >> 4)) {
if (Log.debug) {
Log.debug("A tile entity is invalid or unloaded."
+ "\n entity: " + Log.toString(tileEntity)
+ "\n In " + hashCode + "\t.tickRegion: " + tileEntity.tickRegion.hashCode + "\texpected: " + TickManager.getHashCode(xPos, zPos));
}
invalidate(tileEntity);
return true;
}
if (Log.debug) {
Log.debug("A tile entity is in the wrong TickRegion - was it moved by a player, or did something break?"
+ "\n entity: " + Log.toString(tileEntity)
+ "\n In " + hashCode + "\t.tickRegion: " + tileEntity.tickRegion.hashCode + "\texpected: " + TickManager.getHashCode(xPos, zPos));
}
manager.add(tileEntity, false);
return true;
}
return tileEntity.lastTTX != xPos || tileEntity.lastTTY != tileEntity.yCoord || tileEntity.lastTTZ != zPos;
}
private void invalidate(TileEntity tileEntity) {
int xPos = tileEntity.xCoord;
int zPos = tileEntity.zCoord;
manager.removed(tileEntity);
Chunk chunk = world.getChunkIfExists(xPos >> 4, zPos >> 4);
if (chunk != null) {
chunk.cleanChunkBlockTileEntity(xPos, tileEntity.yCoord, zPos);
}
}
@Override
protected String getShortTypeName() {
return "T";
}
public boolean add(TileEntity tileEntity) {
return tileEntitySet.add(tileEntity);
}
public boolean remove(TileEntity tileEntity) {
return tileEntitySet.remove(tileEntity);
}
@Override
public boolean isEmpty() {
return tileEntitySet.isEmpty();
}
@Override
public int size() {
return tileEntitySet.size();
}
@Override
public void die() {
tileEntitySet.clear();
}
@Override
public void dump(final TableFormatter tf) {
synchronized (tileEntitySet) {
for (TileEntity e : tileEntitySet) {
//DumpCommand.dump(tf, e, tf.stringFiller == StringFiller.CHAT ? 35 : 70);
tf.sb.append("TileEntity ").append(Log.toString(e)).append(" in ").append(hashCode).append(", new ").append(TickManager.getHashCode(e)).append('\n');
}
}
}
}