-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchWorldServer.java
464 lines (392 loc) · 16 KB
/
PatchWorldServer.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package me.nallar.patched;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import com.google.common.collect.ImmutableSetMultimap;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.collections.TreeHashSet;
import me.nallar.tickthreading.minecraft.DeadLockDetector;
import me.nallar.tickthreading.minecraft.ThreadManager;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.patcher.Declare;
import net.minecraft.block.Block;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.profiler.Profiler;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.IWorldAccess;
import net.minecraft.world.NextTickListEntry;
import net.minecraft.world.SpawnerAnimals;
import net.minecraft.world.Teleporter;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.WorldServer;
import net.minecraft.world.WorldSettings;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.AnvilChunkLoader;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraft.world.chunk.storage.IChunkLoader;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.common.ForgeChunkManager;
public abstract class PatchWorldServer extends WorldServer implements Runnable {
private Iterator chunkCoordIterator;
private ThreadManager threadManager;
private ThreadLocal<Random> randoms;
private ArrayList<NextTickListEntry> runningTickListEntries;
@Declare
public ThreadLocal<Boolean> worldGenInProgress_;
public PatchWorldServer(MinecraftServer par1MinecraftServer, ISaveHandler par2ISaveHandler, String par3Str, int par4, WorldSettings par5WorldSettings, Profiler par6Profiler) {
super(par1MinecraftServer, par2ISaveHandler, par3Str, par4, par5WorldSettings, par6Profiler);
}
public void construct() {
randoms = new ThreadLocalRandom();
threadManager = new ThreadManager(TickThreading.instance.getThreadCount(), "Chunk Updates for " + Log.name(this));
try {
field_73064_N = null;
} catch (NoSuchFieldError ignored) {
//MCPC+ compatibility - they also remove this.
}
pendingTickListEntries = new TreeHashSet();
worldGenInProgress = new BooleanThreadLocal();
runningTickListEntries = new ArrayList<NextTickListEntry>();
}
@Override
public void flush() {
DeadLockDetector.tick("Saving a world before unload", System.nanoTime() + 30000000000L);
this.saveHandler.flush();
IChunkLoader chunkLoader = theChunkProviderServer.currentChunkLoader;
if (chunkLoader instanceof AnvilChunkLoader) {
((AnvilChunkLoader) chunkLoader).close();
}
threadManager.stop();
}
@Override
protected void initialize(WorldSettings par1WorldSettings) {
if (this.entityIdMap == null) {
this.entityIdMap = new net.minecraft.util.IntHashMap();
}
if (pendingTickListEntries == null) {
pendingTickListEntries = new TreeHashSet();
}
this.createSpawnPosition(par1WorldSettings);
}
@Override
public List getPendingBlockUpdates(Chunk chunk, boolean remove) {
ArrayList<NextTickListEntry> var3 = null;
int minCX = chunk.xPosition << 4;
int maxCX = minCX + 16;
int minCZ = chunk.zPosition << 4;
int maxCZ = minCZ + 16;
TreeSet<NextTickListEntry> pendingTickListEntries = this.pendingTickListEntries;
synchronized (pendingTickListEntries) {
Iterator<NextTickListEntry> nextTickListEntryIterator = pendingTickListEntries.iterator();
while (nextTickListEntryIterator.hasNext()) {
NextTickListEntry var10 = nextTickListEntryIterator.next();
if (var10.xCoord >= minCX && var10.xCoord < maxCX && var10.zCoord >= minCZ && var10.zCoord < maxCZ) {
if (remove) {
nextTickListEntryIterator.remove();
}
if (var3 == null) {
var3 = new ArrayList<NextTickListEntry>();
}
var3.add(var10);
}
}
}
return var3;
}
@Override
public void func_82740_a(int x, int y, int z, int blockID, int timeOffset, int par6) {
NextTickListEntry nextTickListEntry = new NextTickListEntry(x, y, z, blockID);
boolean isForced = getPersistentChunks().containsKey(new ChunkCoordIntPair(nextTickListEntry.xCoord >> 4, nextTickListEntry.zCoord >> 4));
byte range = isForced ? (byte) 0 : 8;
if (this.scheduledUpdatesAreImmediate && blockID > 0) {
if (Block.blocksList[blockID].func_82506_l()) {
if (this.checkChunksExist(nextTickListEntry.xCoord - range, nextTickListEntry.yCoord - range, nextTickListEntry.zCoord - range, nextTickListEntry.xCoord + range, nextTickListEntry.yCoord + range, nextTickListEntry.zCoord + range)) {
int realBlockID = this.getBlockIdWithoutLoad(nextTickListEntry.xCoord, nextTickListEntry.yCoord, nextTickListEntry.zCoord);
if (realBlockID > 0 && realBlockID == nextTickListEntry.blockID) {
Block.blocksList[realBlockID].updateTick(this, nextTickListEntry.xCoord, nextTickListEntry.yCoord, nextTickListEntry.zCoord, this.rand);
}
}
return;
}
timeOffset = 1;
}
if (this.checkChunksExist(x - range, y - range, z - range, x + range, y + range, z + range)) {
if (blockID > 0) {
nextTickListEntry.setScheduledTime((long) timeOffset + worldInfo.getWorldTotalTime());
nextTickListEntry.func_82753_a(par6);
}
pendingTickListEntries.add(nextTickListEntry);
}
}
@Override
public void scheduleBlockUpdateFromLoad(int x, int y, int z, int blockID, int timeOffset) {
NextTickListEntry nextTickListEntry = new NextTickListEntry(x, y, z, blockID);
if (blockID > 0) {
nextTickListEntry.setScheduledTime((long) timeOffset + worldInfo.getWorldTotalTime());
}
pendingTickListEntries.add(nextTickListEntry);
}
@Override
public boolean tickUpdates(boolean runAll) {
boolean result;
final ArrayList<NextTickListEntry> runningTickListEntries = this.runningTickListEntries;
final TreeSet pendingTickListEntries = this.pendingTickListEntries;
final Random rand = this.rand;
final WorldInfo worldInfo = this.worldInfo;
synchronized (pendingTickListEntries) {
int var2 = Math.min(1000, pendingTickListEntries.size());
runningTickListEntries.ensureCapacity(var2);
for (int var3 = 0; var3 < var2; ++var3) {
NextTickListEntry nextTickListEntry = (NextTickListEntry) pendingTickListEntries.first();
if (!runAll && nextTickListEntry.scheduledTime > worldInfo.getWorldTotalTime()) {
break;
}
pendingTickListEntries.remove(nextTickListEntry);
runningTickListEntries.add(nextTickListEntry);
}
result = !pendingTickListEntries.isEmpty();
}
ImmutableSetMultimap<ChunkCoordIntPair, ForgeChunkManager.Ticket> persistentChunks = getPersistentChunks();
for (NextTickListEntry var4 : runningTickListEntries) {
boolean isForced = persistentChunks.containsKey(new ChunkCoordIntPair(var4.xCoord >> 4, var4.zCoord >> 4));
byte range = isForced ? (byte) 0 : 8;
if (this.checkChunksExist(var4.xCoord - range, var4.yCoord - range, var4.zCoord - range, var4.xCoord + range, var4.yCoord + range, var4.zCoord + range)) {
int blockID = this.getBlockIdWithoutLoad(var4.xCoord, var4.yCoord, var4.zCoord);
if (blockID == var4.blockID && blockID > 0) {
try {
Block.blocksList[blockID].updateTick(this, var4.xCoord, var4.yCoord, var4.zCoord, rand);
} catch (Throwable var13) {
Log.severe("Exception while ticking a block", var13);
}
}
}
}
runningTickListEntries.clear();
return result;
}
@Override
public void tick() {
final Profiler profiler = this.theProfiler;
final WorldInfo worldInfo = this.worldInfo;
int tickCount = this.tickCount++;
this.updateWeather();
if (this.difficultySetting < 3 && this.getWorldInfo().isHardcoreModeEnabled()) {
this.difficultySetting = 3;
}
if (tickCount % 200 == 0) {
this.provider.worldChunkMgr.cleanupCache();
}
if (this.areAllPlayersAsleep()) {
long var2 = worldInfo.getWorldTime();
worldInfo.setWorldTime(var2 + 24000L - (var2 % 24000L));
this.wakeAllPlayers();
}
profiler.startSection("mobSpawner");
if (this.getGameRules().getGameRuleBooleanValue("doMobSpawning")) {
SpawnerAnimals.findChunksForSpawning(this, this.spawnHostileMobs, this.spawnPeacefulMobs, worldInfo.getWorldTotalTime() % 400L == 0L);
}
profiler.endStartSection("chunkSource");
this.chunkProvider.unload100OldestChunks();
this.skylightSubtracted = this.calculateSkylightSubtracted(1.0F);
this.sendAndApplyBlockEvents();
worldInfo.incrementTotalWorldTime(worldInfo.getWorldTotalTime() + 1L);
worldInfo.setWorldTime(worldInfo.getWorldTime() + 1L);
profiler.endStartSection("tickPending");
this.tickUpdates(false);
profiler.endStartSection("tickTiles");
this.tickBlocksAndAmbiance();
profiler.endStartSection("chunkMap");
this.thePlayerManager.updatePlayerInstances();
profiler.endStartSection("village");
this.villageCollectionObj.tick();
this.villageSiegeObj.tick();
profiler.endStartSection("portalForcer");
this.field_85177_Q.func_85189_a(this.getTotalWorldTime());
for (Teleporter tele : customTeleporters) {
tele.func_85189_a(getTotalWorldTime());
}
profiler.endSection();
this.sendAndApplyBlockEvents();
}
@Override
protected void tickBlocksAndAmbiance() {
boolean concurrentTicks = TickThreading.instance.enableChunkTickThreading && !mcServer.theProfiler.profilingEnabled;
if (concurrentTicks) {
threadManager.waitForCompletion();
}
Set<ChunkCoordIntPair> activeChunkSet = this.activeChunkSet;
if (tickCount % 5 == 0) {
Profiler profiler = this.theProfiler;
profiler.startSection("buildList");
activeChunkSet.clear();
activeChunkSet.addAll(getPersistentChunks().keySet());
List<EntityPlayer> playerEntities = this.playerEntities;
for (EntityPlayer entityPlayer : playerEntities) {
int x = (int) (entityPlayer.posX / 16.0D);
int z = (int) (entityPlayer.posZ / 16.0D);
byte var5 = 6;
for (int var6 = -var5; var6 <= var5; ++var6) {
for (int var7 = -var5; var7 <= var5; ++var7) {
activeChunkSet.add(new ChunkCoordIntPair(var6 + x, var7 + z));
}
}
}
profiler.endSection();
if (this.ambientTickCountdown > 0) {
--this.ambientTickCountdown;
}
profiler.startSection("playerCheckLight");
Random rand = this.rand;
if (!playerEntities.isEmpty()) {
EntityPlayer entityPlayer = playerEntities.get(rand.nextInt(playerEntities.size()));
int x = ((int) entityPlayer.posX) + rand.nextInt(11) - 5;
int y = ((int) entityPlayer.posY) + rand.nextInt(11) - 5;
int z = ((int) entityPlayer.posZ) + rand.nextInt(11) - 5;
this.updateAllLightTypes(x, y, z);
}
profiler.endSection();
}
chunkCoordIterator = activeChunkSet.iterator();
if (concurrentTicks) {
threadManager.runAll(this);
} else {
run();
}
}
@Override
public void run() {
double tpsFactor = MinecraftServer.getTPS() / MinecraftServer.getTargetTPS();
final Random rand = randoms.get();
final Iterator<ChunkCoordIntPair> chunkCoordIterator = this.chunkCoordIterator;
final ChunkProviderServer chunkProviderServer = this.theChunkProviderServer;
final boolean isRaining = this.isRaining();
final boolean isThundering = this.isThundering();
final Profiler profiler = this.theProfiler;
final WorldProvider provider = this.provider;
int updateLCG = this.updateLCG;
// We use a random per thread - randoms are threadsafe, however it can result in some contention. See Random.nextInt - compareAndSet.
// This reduces contention -> slightly increased performance, woo! :P
while (true) {
ChunkCoordIntPair var4;
synchronized (chunkCoordIterator) {
if (!chunkCoordIterator.hasNext()) {
break;
}
try {
var4 = chunkCoordIterator.next();
} catch (ConcurrentModificationException e) {
break;
}
}
int cX = var4.chunkXPos;
int cZ = var4.chunkZPos;
if ((tpsFactor < 1 && rand.nextFloat() > tpsFactor) || chunkProviderServer.getChunksToUnloadSet().contains(ChunkCoordIntPair.chunkXZ2Int(cX, cZ))) {
continue;
}
int xPos = cX * 16;
int zPos = cZ * 16;
Chunk chunk = chunkProviderServer.getChunkIfExists(cX, cZ);
if (chunk == null) {
continue;
}
this.moodSoundAndLightCheck(xPos, zPos, chunk);
profiler.endStartSection("chunkTick"); // endStart as moodSoundAndLightCheck starts a section.
chunk.updateSkylight();
int var8;
int var9;
int var10;
int var11;
profiler.startSection("lightning");
if (isRaining && isThundering && provider.canDoLightning(chunk) && rand.nextInt(100000) == 0) {
updateLCG = updateLCG * 1664525 + 1013904223;
var8 = updateLCG >> 2;
var9 = xPos + (var8 & 15);
var10 = zPos + (var8 >> 8 & 15);
var11 = this.getPrecipitationHeight(var9, var10);
if (this.canLightningStrikeAt(var9, var11, var10)) {
this.addWeatherEffect(new EntityLightningBolt(this, (double) var9, (double) var11, (double) var10));
}
}
int blockID;
profiler.endStartSection("precipitation");
if (provider.canDoRainSnowIce(chunk) && rand.nextInt(16) == 0) {
updateLCG = updateLCG * 1664525 + 1013904223;
var8 = updateLCG >> 2;
var9 = var8 & 15;
var10 = var8 >> 8 & 15;
var11 = this.getPrecipitationHeight(var9 + xPos, var10 + zPos);
if (this.isBlockFreezableNaturally(var9 + xPos, var11 - 1, var10 + zPos)) {
this.setBlockWithNotify(var9 + xPos, var11 - 1, var10 + zPos, Block.ice.blockID);
}
if (isRaining) {
if (this.canSnowAt(var9 + xPos, var11, var10 + zPos)) {
this.setBlockWithNotify(var9 + xPos, var11, var10 + zPos, Block.snow.blockID);
}
BiomeGenBase var12 = this.getBiomeGenForCoords(var9 + xPos, var10 + zPos);
if (var12.canSpawnLightningBolt()) {
blockID = this.getBlockIdWithoutLoad(var9 + xPos, var11 - 1, var10 + zPos);
if (blockID > 0) {
Block.blocksList[blockID].fillWithRain(this, var9 + xPos, var11 - 1, var10 + zPos);
}
}
}
}
profiler.endStartSection("blockTick");
ExtendedBlockStorage[] var19 = chunk.getBlockStorageArray();
var9 = var19.length;
for (var10 = 0; var10 < var9; ++var10) {
ExtendedBlockStorage ebs = var19[var10];
if (ebs != null && ebs.getNeedsRandomTick()) {
for (int i = 0; i < 3; ++i) {
updateLCG = updateLCG * 1664525 + 1013904223;
blockID = updateLCG >> 2;
int x = blockID & 15;
int y = blockID >> 8 & 15;
int z = blockID >> 16 & 15;
Block var18 = Block.blocksList[ebs.getExtBlockID(x, z, y)];
if (var18 != null && var18.getTickRandomly()) {
try {
var18.updateTick(this, x + xPos, z + ebs.getYLocation(), y + zPos, rand);
} catch (Exception e) {
Log.severe("Exception ticking block " + var18 + " at x" + x + xPos + 'y' + z + ebs.getYLocation() + 'z' + y + zPos, e);
}
}
}
}
}
profiler.endSection();
profiler.endStartSection("iterate");
}
this.updateLCG += updateLCG * 1664525;
}
public static class ThreadLocalRandom extends ThreadLocal<Random> {
@Override
public Random initialValue() {
return new Random();
}
}
@Override
public void markBlockForUpdate(int x, int y, int z) {
if (worldGenInProgress == null || !worldGenInProgress.get()) {
final List<IWorldAccess> worldAccesses = this.worldAccesses;
for (int i = 0, size = worldAccesses.size(); i < size; ++i) {
worldAccesses.get(i).markBlockForUpdate(x, y, z);
}
}
}
public static class BooleanThreadLocal extends ThreadLocal<Boolean> {
@Override
public Boolean initialValue() {
return false;
}
}
}