-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchChunk.java
213 lines (180 loc) · 6.66 KB
/
PatchChunk.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
package me.nallar.patched.storage;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.logging.Level;
import cpw.mods.fml.common.FMLLog;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.patcher.Declare;
import me.nallar.tickthreading.util.concurrent.TwoWayReentrantReadWriteLock;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.world.ChunkPosition;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.world.ChunkEvent;
@SuppressWarnings ("unchecked")
public abstract class PatchChunk extends Chunk {
@Declare
public List pendingBlockUpdates_;
@Declare
public boolean unloading_;
@Declare
public boolean alreadySavedAfterUnload_;
public Lock entityListWriteLock;
public Lock entityListReadLock;
public PatchChunk(World par1World, int par2, int par3) {
super(par1World, par2, par3);
}
private List<TileEntity> toInvalidate;
public void construct() {
toInvalidate = new ArrayList<TileEntity>();
TwoWayReentrantReadWriteLock twoWayReentrantReadWriteLock = new TwoWayReentrantReadWriteLock();
entityListWriteLock = twoWayReentrantReadWriteLock.writeLock();
entityListReadLock = twoWayReentrantReadWriteLock.readLock();
}
@Override
public boolean needsSaving(boolean force) {
return force || isModified || (hasEntities && (lastSaveTime + 9000 < worldObj.getTotalWorldTime()));
}
@Override
public String toString() {
return "chunk at " + xPosition + ',' + zPosition;
}
@SuppressWarnings ("FieldRepeatedlyAccessedInMethod") // Patcher makes entityLists final
@Override
@Declare
public void getEntitiesWithinAABBForEntity(Entity excludedEntity, AxisAlignedBB collisionArea, List collidingAABBs, int limit) {
entityListReadLock.lock();
try {
int var4 = MathHelper.floor_double((collisionArea.minY - World.MAX_ENTITY_RADIUS) / 16.0D);
int var5 = MathHelper.floor_double((collisionArea.maxY + World.MAX_ENTITY_RADIUS) / 16.0D);
if (var4 < 0) {
var4 = 0;
}
if (var5 >= this.entityLists.length) {
var5 = this.entityLists.length - 1;
}
for (int var6 = var4; var6 <= var5; ++var6) {
List var7 = this.entityLists[var6];
for (Object aVar7 : var7) {
Entity var9 = (Entity) aVar7;
if (var9 != excludedEntity && var9.boundingBox.intersectsWith(collisionArea)) {
collidingAABBs.add(var9);
if (--limit == 0) {
return;
}
Entity[] var10 = var9.getParts();
if (var10 != null) {
for (Entity aVar10 : var10) {
var9 = aVar10;
if (var9 != excludedEntity && var9.boundingBox.intersectsWith(collisionArea)) {
collidingAABBs.add(var9);
if (--limit == 0) {
return;
}
}
}
}
}
}
}
} finally {
entityListReadLock.unlock();
}
}
@Override
public void addTileEntity(TileEntity tileEntity) {
int x = tileEntity.xCoord - this.xPosition * 16;
int y = tileEntity.yCoord;
int z = tileEntity.zCoord - this.zPosition * 16;
if (this.isChunkLoaded) {
this.setChunkBlockTileEntity(x, y, z, tileEntity);
this.worldObj.addTileEntity(tileEntity);
} else {
ChunkPosition chunkPosition = new ChunkPosition(x, y, z);
tileEntity.setWorldObj(this.worldObj);
Block block = Block.blocksList[getBlockID(x, y, z)];
if (block != null && block.hasTileEntity(getBlockMetadata(x, y, z))) {
TileEntity old = (TileEntity) chunkTileEntityMap.put(chunkPosition, tileEntity);
if (old != null) {
toInvalidate.add(old);
}
}
}
}
@SuppressWarnings ("FieldRepeatedlyAccessedInMethod") // Patcher makes worldObj final
@Override
public void onChunkUnload() {
Set<TileEntity> removalSet = worldObj.tileEntityRemovalSet;
for (TileEntity var2 : (Iterable<TileEntity>) this.chunkTileEntityMap.values()) {
removalSet.add(var2);
}
for (List entityList : this.entityLists) {
this.worldObj.unloadEntities(entityList);
}
synchronized (ChunkEvent.class) {
MinecraftForge.EVENT_BUS.post(new ChunkEvent.Unload(this));
}
}
@Override
public void onChunkLoad() {
for (TileEntity tileEntity : toInvalidate) {
tileEntity.invalidate();
}
toInvalidate.clear();
}
@SuppressWarnings ("FieldRepeatedlyAccessedInMethod") // Patcher makes worldObj final
@Override
@Declare
public void threadUnsafeChunkLoad() {
this.isChunkLoaded = true;
worldObj.addTileEntity(this.chunkTileEntityMap.values());
for (List entityList : this.entityLists) {
worldObj.addLoadedEntities(entityList);
}
synchronized (ChunkEvent.class) {
MinecraftForge.EVENT_BUS.post(new ChunkEvent.Load(this));
}
}
@SuppressWarnings ("FieldRepeatedlyAccessedInMethod") // Patcher makes x/zPosition and worldObj final
@Override
public void addEntity(Entity par1Entity) {
isModified = true;
int entityChunkX = MathHelper.floor_double(par1Entity.posX / 16.0D);
int entityChunkZ = MathHelper.floor_double(par1Entity.posZ / 16.0D);
if (entityChunkX != this.xPosition || entityChunkZ != this.zPosition) {
FMLLog.log(Level.FINE, new Throwable(), "Entity %s added to the wrong chunk - expected x%d z%d, got x%d z%d", par1Entity.toString(), this.xPosition, this.zPosition, entityChunkX, entityChunkZ);
if (worldObj instanceof WorldServer) {
Chunk correctChunk = ((WorldServer) worldObj).theChunkProviderServer.getChunkIfExists(entityChunkX, entityChunkZ);
if (correctChunk == this) {
Log.severe("What?! This chunk isn't at the position it says it's at...: " + this + " was added at " + entityChunkX + ", " + entityChunkZ);
} else if (correctChunk != null) {
correctChunk.addEntity(par1Entity);
return;
}
}
}
this.hasEntities = true;
int var4 = MathHelper.floor_double(par1Entity.posY / 16.0D);
if (var4 < 0) {
var4 = 0;
}
if (var4 >= this.entityLists.length) {
var4 = this.entityLists.length - 1;
}
MinecraftForge.EVENT_BUS.post(new EntityEvent.EnteringChunk(par1Entity, this.xPosition, this.zPosition, par1Entity.chunkCoordX, par1Entity.chunkCoordZ));
par1Entity.addedToChunk = true;
par1Entity.chunkCoordX = this.xPosition;
par1Entity.chunkCoordY = var4;
par1Entity.chunkCoordZ = this.zPosition;
this.entityLists[var4].add(par1Entity);
}
}