-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchWorldServer.java
226 lines (192 loc) · 7.63 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
package me.nallar.patched;
import java.util.Iterator;
import java.util.Random;
import me.nallar.tickthreading.Log;
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.profiler.Profiler;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.SpawnerAnimals;
import net.minecraft.world.Teleporter;
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.ExtendedBlockStorage;
import net.minecraft.world.storage.ISaveHandler;
public abstract class PatchWorldServer extends WorldServer implements Runnable {
private Iterator chunkCoordIterator;
private ThreadManager threadManager;
private ThreadLocal<Random> randoms;
@Declare
public int tickCount_;
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));
}
@Override
public void tick() {
int tickCount = this.tickCount++;
this.updateWeather();
if (this.difficultySetting < 3 && this.getWorldInfo().isHardcoreModeEnabled()) {
this.difficultySetting = 3;
}
if (tickCount % 600 == 0) {
this.provider.worldChunkMgr.cleanupCache();
}
if (this.areAllPlayersAsleep()) {
long var2 = this.worldInfo.getWorldTime();
this.worldInfo.setWorldTime(var2 + var2 % 24000L);
this.wakeAllPlayers();
}
this.theProfiler.startSection("mobSpawner");
if (this.getGameRules().getGameRuleBooleanValue("doMobSpawning")) {
SpawnerAnimals.findChunksForSpawning(this, this.spawnHostileMobs, this.spawnPeacefulMobs, this.worldInfo.getWorldTotalTime() % 400L == 0L);
}
this.theProfiler.endStartSection("chunkSource");
this.chunkProvider.unload100OldestChunks();
int var4 = this.calculateSkylightSubtracted(1.0F);
if (var4 != this.skylightSubtracted) {
this.skylightSubtracted = var4;
}
this.sendAndApplyBlockEvents();
this.worldInfo.incrementTotalWorldTime(this.worldInfo.getWorldTotalTime() + 1L);
this.worldInfo.setWorldTime(this.worldInfo.getWorldTime() + 1L);
this.theProfiler.endStartSection("tickPending");
this.tickUpdates(false);
this.theProfiler.endStartSection("tickTiles");
this.tickBlocksAndAmbiance();
this.theProfiler.endStartSection("chunkMap");
this.thePlayerManager.updatePlayerInstances();
this.theProfiler.endStartSection("village");
this.villageCollectionObj.tick();
this.villageSiegeObj.tick();
this.theProfiler.endStartSection("portalForcer");
this.field_85177_Q.func_85189_a(this.getTotalWorldTime());
for (Teleporter tele : customTeleporters) {
tele.func_85189_a(getTotalWorldTime());
}
this.theProfiler.endSection();
this.sendAndApplyBlockEvents();
}
@Override
protected void tickBlocksAndAmbiance() {
TickThreading tickThreading = TickThreading.instance;
boolean concurrentTicks = tickThreading.enableChunkTickThreading && !mcServer.theProfiler.profilingEnabled;
if (concurrentTicks) {
threadManager.waitForCompletion();
}
chunkCoordIterator = this.activeChunkSet.iterator();
if (concurrentTicks) {
for (int i = 0; i < threadManager.size(); i++) {
threadManager.run(this);
}
} else {
run();
}
}
@Override
public void run() {
double tpsFactor = MinecraftServer.getTPS() / MinecraftServer.getTargetTPS();
final Random rand = randoms.get();
// We use a random per thread - randoms are threadsafe, however synchronization is involved.
// This reduces contention -> slightly increased performance, woo! :P
while (true) {
ChunkCoordIntPair var4;
synchronized (chunkCoordIterator) {
if (!chunkCoordIterator.hasNext()) {
break;
}
var4 = (ChunkCoordIntPair) chunkCoordIterator.next();
}
int cX = var4.chunkXPos;
int cZ = var4.chunkZPos;
if ((tpsFactor < 1 && rand.nextFloat() > tpsFactor) || this.theChunkProviderServer.getChunksToUnloadSet().contains(ChunkCoordIntPair.chunkXZ2Int(cX, cZ))) {
continue;
}
int xPos = cX * 16;
int zPos = cZ * 16;
Chunk chunk = this.theChunkProviderServer.getChunkIfExists(cX, cZ);
if (chunk == null) {
continue;
}
this.moodSoundAndLightCheck(xPos, zPos, chunk);
theProfiler.endStartSection("chunkTick"); // endStart as moodSoundAndLightCheck starts a section.
chunk.updateSkylight();
int var8;
int var9;
int var10;
int var11;
theProfiler.startSection("lightning");
if (provider.canDoLightning(chunk) && rand.nextInt(100000) == 0 && this.isRaining() && this.isThundering()) {
this.updateLCG = this.updateLCG * 3 + 1013904223;
var8 = this.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 var13;
theProfiler.endStartSection("precipitation");
if (provider.canDoRainSnowIce(chunk) && rand.nextInt(16) == 0) {
this.updateLCG = this.updateLCG * 3 + 1013904223;
var8 = this.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 (this.isRaining() && this.canSnowAt(var9 + xPos, var11, var10 + zPos)) {
this.setBlockWithNotify(var9 + xPos, var11, var10 + zPos, Block.snow.blockID);
}
if (this.isRaining()) {
BiomeGenBase var12 = this.getBiomeGenForCoords(var9 + xPos, var10 + zPos);
if (var12.canSpawnLightningBolt()) {
var13 = this.getBlockId(var9 + xPos, var11 - 1, var10 + zPos);
if (var13 != 0) {
Block.blocksList[var13].fillWithRain(this, var9 + xPos, var11 - 1, var10 + zPos);
}
}
}
}
theProfiler.endStartSection("blockTick");
ExtendedBlockStorage[] var19 = chunk.getBlockStorageArray();
var9 = var19.length;
for (var10 = 0; var10 < var9; ++var10) {
ExtendedBlockStorage var21 = var19[var10];
if (var21 != null && var21.getNeedsRandomTick()) {
for (int var20 = 0; var20 < 3; ++var20) {
this.updateLCG = this.updateLCG * 3 + 1013904223;
var13 = this.updateLCG >> 2;
int var14 = var13 & 15;
int var15 = var13 >> 8 & 15;
int var16 = var13 >> 16 & 15;
int var17 = var21.getExtBlockID(var14, var16, var15);
Block var18 = Block.blocksList[var17];
if (var18 != null && var18.getTickRandomly()) {
var18.updateTick(this, var14 + xPos, var16 + var21.getYLocation(), var15 + zPos, rand);
}
}
}
}
theProfiler.endSection();
theProfiler.endStartSection("iterate");
}
}
public static class ThreadLocalRandom extends ThreadLocal<Random> {
@Override
public Random initialValue() {
return new Random();
}
}
}