-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TileEntityTickRegion.java
189 lines (176 loc) · 5.58 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
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.TickThreading;
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 lockable = TickThreading.instance.lockRegionBorders;
final boolean profilingEnabled = manager.profilingEnabled || this.profilingEnabled;
final int regionSize = manager.regionSize;
final int maxPosition = (regionSize / 2) - 1;
int relativeXPos;
int relativeZPos;
boolean xMinusLocked = false;
boolean xPlusLocked = false;
boolean zMinusLocked = false;
boolean zPlusLocked = false;
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();
}
}
//Lock classLock = null;
int xPos = 0;
int zPos = 0;
final Iterator<TileEntity> tileEntitiesIterator = tileEntitySet.iterator();
while (tileEntitiesIterator.hasNext()) {
if (profilingEnabled) {
startTime = System.nanoTime();
}
TileEntity tileEntity = tileEntitiesIterator.next();
try {
xPos = tileEntity.xCoord;
zPos = tileEntity.zCoord;
if (lockable) {
if (tileEntity.lastTTX != xPos || tileEntity.lastTTY != tileEntity.yCoord || tileEntity.lastTTZ != zPos) {
manager.lock(tileEntity);
}
xPlusLock = tileEntity.xPlusLock;
zPlusLock = tileEntity.zPlusLock;
zMinusLock = tileEntity.zMinusLock;
xMinusLock = tileEntity.xMinusLock;
if (xPlusLock != null) {
xPlusLock.lock();
}
if (zPlusLock != null) {
zPlusLock.lock();
}
if (zMinusLock != null) {
zMinusLock.lock();
}
if (xMinusLock != null) {
xMinusLock.lock();
}
}
if (manager.getHashCode(xPos, zPos) != hashCode) {
tileEntitiesIterator.remove();
manager.add(tileEntity);
if (hashCode != 0) {
Log.severe("Inconsistent state, a tile entity is in the wrong TickRegion"
+ "\n entity: " + Log.toString(tileEntity) + " at x,y,z:" + xPos + ',' + tileEntity.yCoord + ',' + zPos
+ "\n Has hashcode: " + manager.getHashCode(tileEntity)
+ "\n Region: " + toString());
}
} else 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.updateEntity();
}
} catch (Throwable throwable) {
Log.severe("Exception during tile entity tick"
+ "\nticking: " + Log.toString(tileEntity) + " at x,y,z:" + xPos + ',' + tileEntity.yCoord + ',' + zPos
+ "\nTick region: " + toString() + ':', throwable);
} finally {
if (lockable) {
if (xMinusLock != null) {
xMinusLock.unlock();
}
if (zMinusLock != null) {
zMinusLock.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 void add(TileEntity tileEntity) {
synchronized (tickStateLock) {
if (ticking) {
toAdd.add(tileEntity);
} else {
tileEntitySet.add(tileEntity);
}
}
}
public boolean remove(TileEntity tileEntity) {
synchronized (tickStateLock) {
if (ticking) {
return toRemove.add(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
protected TickRegion getCallable(int regionX, int regionY) {
return manager.getTileEntityRegion(TickManager.getHashCodeFromRegionCoords(regionX, regionY));
}
@Override
public boolean isEmpty() {
return tileEntitySet.isEmpty();
}
@Override
public int size() {
return tileEntitySet.size();
}
}