/
PatchPlayerInstance.java
297 lines (260 loc) · 10.1 KB
/
PatchPlayerInstance.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package nallar.patched.world.tracking;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.concurrent.ConcurrentLinkedQueue;
import nallar.tickthreading.Log;
import nallar.tickthreading.minecraft.TickThreading;
import nallar.tickthreading.patcher.Declare;
import nallar.tickthreading.util.ChunkLoadRunnable;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet51MapChunk;
import net.minecraft.network.packet.Packet52MultiBlockChange;
import net.minecraft.network.packet.Packet53BlockChange;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PlayerInstance;
import net.minecraft.server.management.PlayerManager;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.ForgeDummyContainer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.ChunkWatchEvent;
public abstract class PatchPlayerInstance extends PlayerInstance {
private ConcurrentLinkedQueue<TileEntity> tilesToUpdate;
private static final boolean rateLimitChunkUpdates = TickThreading.instance.rateLimitChunkUpdates;
private static final byte[] unloadSequence = {0x78, (byte) 0x9C, 0x63, 0x64, 0x1C, (byte) 0xD9, 0x00, 0x00, (byte) 0x81, (byte) 0x80, 0x01, 0x01};
@Declare
public boolean loaded_;
private boolean watched;
@Declare
public net.minecraft.world.chunk.Chunk chunk_;
private int startTime;
private int sentUpdates;
public PatchPlayerInstance(PlayerManager par1PlayerManager, int par2, int par3) {
super(par1PlayerManager, par2, par3);
}
public void construct() {
tilesToUpdate = new ConcurrentLinkedQueue<TileEntity>();
myManager.getWorldServer().theChunkProviderServer.getChunkAt(chunkLocation.chunkXPos, chunkLocation.chunkZPos, new LoadRunnable(this));
startTime = MinecraftServer.currentTick + 30;
}
@Override
public void addPlayerToChunkWatchingList(final EntityPlayerMP entityPlayerMP) {
if (this.playersInChunk.contains(entityPlayerMP)) {
throw new IllegalStateException("Failed to add player. " + entityPlayerMP + " already is in chunk " + this.chunkLocation.chunkXPos + ", " + this.chunkLocation.chunkZPos);
} else {
this.playersInChunk.add(entityPlayerMP);
if (loaded) {
Collection<ChunkCoordIntPair> loadedChunks = entityPlayerMP.loadedChunks;
synchronized (loadedChunks) {
loadedChunks.add(chunkLocation);
}
} else {
myManager.getWorldServer().theChunkProviderServer.getChunkAt(chunkLocation.chunkXPos, chunkLocation.chunkZPos, new AddToPlayerRunnable(entityPlayerMP, chunkLocation));
}
}
}
@Override
@Declare
public synchronized void clearTileCount() {
this.numberOfTilesToUpdate = 0;
this.field_73260_f = 0;
this.watched = false;
}
@Override
public void sendThisChunkToPlayer(EntityPlayerMP entityPlayerMP) {
if (this.playersInChunk.remove(entityPlayerMP)) {
Packet51MapChunk packet51MapChunk = new Packet51MapChunk();
packet51MapChunk.includeInitialize = true;
packet51MapChunk.xCh = chunkLocation.chunkXPos;
packet51MapChunk.zCh = chunkLocation.chunkZPos;
packet51MapChunk.yChMax = 0;
packet51MapChunk.yChMin = 0;
packet51MapChunk.setData(unloadSequence);
entityPlayerMP.playerNetServerHandler.sendPacketToPlayer(packet51MapChunk);
Collection<ChunkCoordIntPair> loadedChunks = entityPlayerMP.loadedChunks;
synchronized (loadedChunks) {
loadedChunks.remove(chunkLocation);
}
MinecraftForge.EVENT_BUS.post(new ChunkWatchEvent.UnWatch(chunkLocation, entityPlayerMP));
if (this.playersInChunk.isEmpty()) {
long var2 = (long) this.chunkLocation.chunkXPos + 2147483647L | (long) this.chunkLocation.chunkZPos + 2147483647L << 32;
this.myManager.getChunkWatchers().remove(var2);
if (watched) {
this.myManager.getChunkWatcherWithPlayers().remove(this);
}
this.myManager.getWorldServer().theChunkProviderServer.unloadChunksIfNotNearSpawn(this.chunkLocation.chunkXPos, this.chunkLocation.chunkZPos);
}
}
}
@Override
public String toString() {
return chunkLocation + " watched by " + Arrays.toString(playersInChunk.toArray());
}
@Override
@Declare
public void forceUpdate() {
this.sendToAllPlayersWatchingChunk(new Packet51MapChunk(myManager.getWorldServer().getChunkFromChunkCoords(this.chunkLocation.chunkXPos, this.chunkLocation.chunkZPos), true, Integer.MAX_VALUE));
}
public void sendTiles() {
HashSet<TileEntity> tileEntities = new HashSet<TileEntity>();
for (TileEntity tileEntity = tilesToUpdate.poll(); tileEntity != null; tileEntity = tilesToUpdate.poll()) {
tileEntities.add(tileEntity);
}
for (TileEntity tileEntity : tileEntities) {
this.sendTileToAllPlayersWatchingChunk(tileEntity);
}
tileEntities.clear();
}
@Override
public void flagChunkForUpdate(int par1, int par2, int par3) {
if (noUpdateRequired()) {
return;
}
markRequiresUpdate();
synchronized (this) {
this.field_73260_f |= 1 << (par2 >> 4);
short mask = (short) (par1 << 12 | par3 << 8 | par2);
short[] locationOfBlockChange = this.locationOfBlockChange;
int tiles = numberOfTilesToUpdate;
for (int var5 = 0; var5 < tiles; ++var5) {
if (locationOfBlockChange[var5] == mask) {
return;
}
}
if (tiles == locationOfBlockChange.length) {
this.locationOfBlockChange = locationOfBlockChange = Arrays.copyOf(locationOfBlockChange, locationOfBlockChange.length << 1);
}
locationOfBlockChange[tiles++] = mask;
numberOfTilesToUpdate = tiles;
}
}
private void markRequiresUpdate() {
boolean requiresWatch = false;
synchronized (this) {
if (!watched) {
watched = requiresWatch = true;
}
}
if (requiresWatch) {
this.myManager.getChunkWatcherWithPlayers().add(this);
}
}
@Override
@Declare
public void updateTile(TileEntity tileEntity) {
if (noUpdateRequired()) {
return;
}
markRequiresUpdate();
tilesToUpdate.add(tileEntity);
}
@Override
protected void sendTileToAllPlayersWatchingChunk(TileEntity tileEntity) {
if (tileEntity != null) {
Packet descriptionPacket;
try {
descriptionPacket = tileEntity.getDescriptionPacket();
} catch (Throwable t) {
Log.severe("Failed to send TileEntity description for " + Log.toString(tileEntity) + " at chunk coords " + chunkLocation, t);
return;
}
if (descriptionPacket != null) {
this.sendToAllPlayersWatchingChunk(descriptionPacket);
}
}
}
private boolean noUpdateRequired() {
Chunk chunk = this.chunk;
if (chunk == null || !chunk.isTerrainPopulated || playersInChunk.isEmpty()) {
return true;
}
if (chunk.partiallyUnloaded) {
this.loaded = false;
this.chunk = null;
myManager.getWorldServer().theChunkProviderServer.getChunkAt(chunkLocation.chunkXPos, chunkLocation.chunkZPos, new LoadRunnable(this));
return true;
}
return false;
}
@Override
@Declare
public boolean shouldPostPone(boolean squash, int currentTick) {
if (!rateLimitChunkUpdates) {
return false;
}
int runningTicks = currentTick - startTime;
if (squash) {
startTime = currentTick - (runningTicks /= 2);
sentUpdates /= 2;
}
return (sentUpdates / (float) runningTicks) > 0.1f;
}
@Override
public void sendChunkUpdate() {
if (noUpdateRequired()) {
clearTileCount();
return;
}
sentUpdates++;
synchronized (this) {
int numberOfTilesToUpdate = this.numberOfTilesToUpdate;
if (numberOfTilesToUpdate != 0) {
short[] locationOfBlockChange = this.locationOfBlockChange;
if (numberOfTilesToUpdate > locationOfBlockChange.length) {
Log.warning("numberOfTilesToUpdate set too high. Got " + numberOfTilesToUpdate + " should be <= " + locationOfBlockChange.length);
numberOfTilesToUpdate = locationOfBlockChange.length;
}
WorldServer worldServer = myManager.getWorldServer();
Chunk chunk = this.chunk;
if (numberOfTilesToUpdate == 1) {
int x = chunkLocation.chunkXPos * 16 + (locationOfBlockChange[0] >> 12 & 15);
int y = locationOfBlockChange[0] & 255;
int z = chunkLocation.chunkZPos * 16 + (locationOfBlockChange[0] >> 8 & 15);
sendToAllPlayersWatchingChunk(new Packet53BlockChange(x, y, z, worldServer));
tilesToUpdate.add(chunk.getChunkBlockTileEntity(locationOfBlockChange[0] >> 12 & 15, locationOfBlockChange[0] & 255, locationOfBlockChange[0] >> 8 & 15));
} else {
if (numberOfTilesToUpdate >= ForgeDummyContainer.clumpingThreshold) {
sendToAllPlayersWatchingChunk(new Packet51MapChunk(chunk, false, field_73260_f));
} else {
sendToAllPlayersWatchingChunk(new Packet52MultiBlockChange(chunkLocation.chunkXPos, chunkLocation.chunkZPos, locationOfBlockChange, numberOfTilesToUpdate, worldServer));
}
for (int i = 0; i < numberOfTilesToUpdate; ++i) {
tilesToUpdate.add(chunk.getChunkBlockTileEntity(locationOfBlockChange[i] >> 12 & 15, locationOfBlockChange[i] & 255, locationOfBlockChange[i] >> 8 & 15));
}
}
}
clearTileCount();
}
sendTiles();
}
public static class AddToPlayerRunnable implements Runnable {
private final EntityPlayerMP entityPlayerMP;
private final ChunkCoordIntPair chunkLocation;
public AddToPlayerRunnable(EntityPlayerMP entityPlayerMP, ChunkCoordIntPair chunkLocation) {
this.entityPlayerMP = entityPlayerMP;
this.chunkLocation = chunkLocation;
}
@Override
public void run() {
Collection<ChunkCoordIntPair> loadedChunks = entityPlayerMP.loadedChunks;
synchronized (loadedChunks) {
loadedChunks.add(chunkLocation);
}
}
}
public static class LoadRunnable extends ChunkLoadRunnable {
final PlayerInstance playerInstance;
public LoadRunnable(PlayerInstance playerInstance) {
this.playerInstance = playerInstance;
}
@Override
public void onLoad(Chunk chunk) {
playerInstance.loaded = true;
playerInstance.chunk = chunk;
}
}
}