-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TileEntityTickRegion.java
176 lines (164 loc) · 5.55 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
package me.nallar.tickthreading.minecraft.tickregion;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
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.chunk.IChunkProvider;
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() {
IChunkProvider chunkProvider = world.getChunkProvider();
int regionSize = manager.regionSize;
int maxPosition = (regionSize / 2) - 1;
int relativeXPos;
int relativeZPos;
boolean locked = false;
boolean xMinusLocked = false;
boolean xPlusLocked = false;
boolean zMinusLocked = false;
boolean zPlusLocked = false;
boolean profilingEnabled = manager.profilingEnabled;
EntityTickProfiler entityTickProfiler = null;
long startTime = 0;
if (profilingEnabled) {
entityTickProfiler = manager.entityTickProfiler;
}
//Lock classLock = null;
Iterator<TileEntity> tileEntitiesIterator = tileEntitySet.iterator();
while (tileEntitiesIterator.hasNext()) {
if (profilingEnabled) {
startTime = System.nanoTime();
}
TileEntity tileEntity = tileEntitiesIterator.next();
try {
//classLock = null;
relativeXPos = (tileEntity.xCoord % regionSize) / 2;
relativeZPos = (tileEntity.zCoord % regionSize) / 2;
xMinusLocked = relativeXPos == 0 && this.xMinusLock != null;
zMinusLocked = relativeZPos == 0 && this.zMinusLock != null;
xPlusLocked = relativeXPos == maxPosition && this.xPlusLock != null;
zPlusLocked = relativeZPos == maxPosition && this.zPlusLock != null;
locked = xMinusLocked || zMinusLocked || xPlusLocked || zPlusLocked;
if (locked) {
// ORDER MATTERS!
if (xPlusLocked) {
this.xPlusLock.lock();
}
if (zPlusLocked) {
this.zPlusLock.lock();
}
thisLock.lock();
if (zMinusLocked) {
this.zMinusLock.lock();
}
if (xMinusLocked) {
this.xMinusLock.lock();
}
}
if (manager.getHashCode(tileEntity) != hashCode) {
tileEntitiesIterator.remove();
manager.add(tileEntity);
if (hashCode != 0) {
Log.severe("Inconsistent state, a tile entity is in the wrong TickRegion"
+ "\n entity: " + tileEntity.getClass() + " at x,y,z:" + tileEntity.xCoord + ',' + tileEntity.yCoord + ',' + tileEntity.zCoord
+ "\n Has hashcode: " + manager.getHashCode(tileEntity)
+ "\n Region: " + toString());
}
continue;
}
if (!tileEntity.isInvalid() && tileEntity.func_70309_m() && world.blockExists(tileEntity.xCoord, tileEntity.yCoord, tileEntity.zCoord)) {
tileEntity.updateEntity();
}
//Yes, this is correct. Can't be simplified to else if, as it may be invalidated during updateEntity
if (tileEntity.isInvalid()) {
tileEntitiesIterator.remove();
manager.removed(tileEntity);
tileEntity.onChunkUnload();
//Log.fine("Removed tile entity: " + tileEntity.xCoord + ", " + tileEntity.yCoord + ", " + tileEntity.zCoord + "\ttype:" + tileEntity.getClass().toString());
if (chunkProvider.chunkExists(tileEntity.xCoord >> 4, tileEntity.zCoord >> 4)) {
Chunk chunk = world.getChunkFromChunkCoords(tileEntity.xCoord >> 4, tileEntity.zCoord >> 4);
if (chunk != null) {
chunk.cleanChunkBlockTileEntity(tileEntity.xCoord & 0xf, tileEntity.yCoord, tileEntity.zCoord & 0xf);
}
}
}
} catch (Throwable throwable) {
Log.severe("Exception during tile entity tick"
+ "\nticking: " + tileEntity.getClass() + " at x,y,z:" + tileEntity.xCoord + ',' + tileEntity.yCoord + ',' + tileEntity.zCoord
+ "\nTick region: " + toString() + ':', throwable);
} finally {
if (locked) {
if (xMinusLocked) {
this.xMinusLock.unlock();
}
if (zMinusLocked) {
this.zMinusLock.unlock();
}
thisLock.unlock();
if (zPlusLocked) {
this.zPlusLock.unlock();
}
if (xPlusLocked) {
this.xPlusLock.unlock();
}
}
if (profilingEnabled) {
entityTickProfiler.record(tileEntity.getClass(), System.nanoTime() - startTime);
}
}
}
}
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) {
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.getTileEntityCallable(TickManager.getHashCodeFromRegionCoords(regionX, regionY));
}
@Override
public boolean isEmpty() {
return tileEntitySet.isEmpty();
}
@Override
public int size() {
return tileEntitySet.size();
}
}