-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TileEntityTickRegion.java
186 lines (173 loc) · 5.97 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
package me.nallar.tickthreading.minecraft.tickregion;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.minecraft.TickManager;
import me.nallar.tickthreading.minecraft.profiling.EntityTickProfiler;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.gen.ChunkProviderServer;
public class TileEntityTickRegion extends TickRegion {
private final Set<TileEntity> tileEntitySet = new LinkedHashSet<TileEntity>();
public TileEntityTickRegion(World world, TickManager manager, int regionX, int regionY) {
super(world, manager, regionX, regionY);
}
@Override
public void doTick() {
final ChunkProviderServer chunkProvider = (ChunkProviderServer) world.getChunkProvider();
final World world = this.world;
final boolean profilingEnabled = manager.profilingEnabled || this.profilingEnabled;
Lock thisLock = null;
Lock xPlusLock = null;
Lock xMinusLock = null;
Lock zPlusLock = null;
Lock zMinusLock = null;
EntityTickProfiler entityTickProfiler = null;
long startTime = 0;
if (profilingEnabled) {
entityTickProfiler = manager.entityTickProfiler;
if (this.profilingEnabled) {
entityTickProfiler.tick();
}
}
// This code is performance critical.
// Locking calls are manipulated by the patcher,
// INVOKEVIRTUAL java.util.concurrent.locks.Lock.lock/unlock() calls are replaced with
// MONITORENTER/MONITOREXIT instructions.
// For this reason, although we do use interfaces which correctly implement lock/unlock depending on the TileEntity,
// it is critical that .lock() is not called on anything other than a NativeMutex instance in this code.
// Behaving in this manner also allows us to avoid the overhead of calling an interface method.
// Volatile reads are, if used only when necessary, fast. JVM will not optimize out repeat volatile reads, happens-before on field write.
// Just don't read fields repeatedly unnecessary. Best not to do this with non-volatile fields anyway.
final Iterator<TileEntity> tileEntitiesIterator = tileEntitySet.iterator();
while (tileEntitiesIterator.hasNext()) {
if (profilingEnabled) {
startTime = System.nanoTime();
}
final TileEntity tileEntity = tileEntitiesIterator.next();
final int xPos = tileEntity.xCoord;
final int zPos = tileEntity.zCoord;
if (tileEntity.lastTTX != xPos || tileEntity.lastTTY != tileEntity.yCoord || tileEntity.lastTTZ != zPos) {
manager.lock(tileEntity);
if (manager.getHashCode(xPos, zPos) != hashCode) {
tileEntitiesIterator.remove();
manager.add(tileEntity, false);
if (hashCode != 0) {
Log.fine("A tile entity is in the wrong TickRegion - was it moved by a player, or did something bug out?"
+ "\n entity: " + Log.toString(tileEntity) + " at x,y,z:" + xPos + ',' + tileEntity.yCoord + ',' + zPos
+ "\n Region: " + toString()
+ "\n World: " + Log.name(tileEntity.worldObj));
}
}
continue;
}
try {
xPlusLock = tileEntity.xPlusLock;
zPlusLock = tileEntity.zPlusLock;
thisLock = tileEntity.thisLock;
xMinusLock = tileEntity.xMinusLock;
zMinusLock = tileEntity.zMinusLock;
if (xPlusLock != null) {
xPlusLock.lock();
}
if (zPlusLock != null) {
zPlusLock.lock();
}
if (thisLock != null) {
thisLock.lock();
}
if (zMinusLock != null) {
zMinusLock.lock();
}
if (xMinusLock != null) {
xMinusLock.lock();
}
if (tileEntity.isInvalid()) {
tileEntitiesIterator.remove();
manager.removed(tileEntity);
//Log.fine("Removed tile entity: " + xPos + ", " + tileEntity.yCoord + ", " + zPos + "\ttype:" + tileEntity.getClass().toString());
if (chunkProvider.chunkExists(xPos >> 4, zPos >> 4)) {
Chunk chunk = world.getChunkFromChunkCoords(xPos >> 4, zPos >> 4);
if (chunk != null) {
chunk.cleanChunkBlockTileEntity(xPos, tileEntity.yCoord, zPos);
}
}
} else if (tileEntity.worldObj != null && chunkProvider.chunkExists(xPos >> 4, zPos >> 4)) {
tileEntity.tickTT();
}
} catch (Throwable throwable) {
Log.severe("Exception ticking TileEntity " + Log.toString(tileEntity) + " at x,y,z:" + xPos + ',' + tileEntity.yCoord + ',' + zPos
+ "\n World: " + Log.name(tileEntity.worldObj), throwable);
} finally {
if (xMinusLock != null) {
xMinusLock.unlock();
}
if (zMinusLock != null) {
zMinusLock.unlock();
}
if (thisLock != null) {
thisLock.unlock();
}
if (zPlusLock != null) {
zPlusLock.unlock();
}
if (xPlusLock != null) {
xPlusLock.unlock();
}
if (profilingEnabled) {
entityTickProfiler.record(tileEntity, System.nanoTime() - startTime);
}
}
}
}
@Override
protected String getShortTypeName() {
return "T";
}
public boolean add(TileEntity tileEntity) {
synchronized (tickStateLock) {
if (ticking) {
return toAdd.add(tileEntity) && !tileEntitySet.contains(tileEntity);
} else {
return tileEntitySet.add(tileEntity);
}
}
}
public boolean remove(TileEntity tileEntity) {
synchronized (tickStateLock) {
if (ticking) {
return toRemove.add(tileEntity) && tileEntitySet.contains(tileEntity);
} else {
return tileEntitySet.remove(tileEntity);
}
}
}
@Override
public void processChanges() {
synchronized (tickStateLock) {
if (ticking) {
return;
}
tileEntitySet.addAll(toAdd);
tileEntitySet.removeAll(toRemove);
toAdd.clear();
toRemove.clear();
}
}
@Override
public void die() {
super.die();
tileEntitySet.clear();
}
@Override
public boolean isEmpty() {
return tileEntitySet.isEmpty();
}
@Override
public int size() {
return tileEntitySet.size();
}
}