-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchMinecraftServer.java
202 lines (168 loc) · 6.57 KB
/
PatchMinecraftServer.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
package me.nallar.tickthreading.minecraft.patched;
import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import me.nallar.tickthreading.minecraft.ThreadManager;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.patcher.Declare;
import net.minecraft.crash.CrashReport;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet4UpdateTime;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.gui.IUpdatePlayerListBox;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ReportedException;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.DimensionManager;
public abstract class PatchMinecraftServer extends MinecraftServer {
public ThreadManager threadManager;
private static int tickTime = 0;
private AtomicInteger currentWorld;
private Integer[] dimensionIdsToTick;
private Runnable tickRunnable;
public PatchMinecraftServer(File par1File) {
super(par1File);
}
@Override
public void tick() {
FMLCommonHandler.instance().rescheduleTicks(Side.SERVER);
long var1 = System.nanoTime();
AxisAlignedBB.getAABBPool().cleanPool();
FMLCommonHandler.instance().onPreServerTick();
++this.tickCounter;
if (this.startProfiling) {
this.startProfiling = false;
this.theProfiler.profilingEnabled = true;
this.theProfiler.clearProfiling();
}
this.theProfiler.startSection("root");
this.updateTimeLightAndEntities();
if (this.tickCounter % TickThreading.instance.saveInterval == 0) {
this.theProfiler.startSection("save");
this.serverConfigManager.saveAllPlayerData();
this.saveAllWorlds(true);
this.theProfiler.endSection();
}
this.theProfiler.startSection("tallying");
this.tickTimeArray[this.tickCounter % 100] = System.nanoTime() - var1;
this.sentPacketCountArray[this.tickCounter % 100] = Packet.sentID - this.lastSentPacketID;
this.lastSentPacketID = Packet.sentID;
this.sentPacketSizeArray[this.tickCounter % 100] = Packet.sentSize - this.lastSentPacketSize;
this.lastSentPacketSize = Packet.sentSize;
this.receivedPacketCountArray[this.tickCounter % 100] = Packet.receivedID - this.lastReceivedID;
this.lastReceivedID = Packet.receivedID;
this.receivedPacketSizeArray[this.tickCounter % 100] = Packet.receivedSize - this.lastReceivedSize;
this.lastReceivedSize = Packet.receivedSize;
this.theProfiler.endStartSection("snooper");
if (!this.usageSnooper.isSnooperRunning() && this.tickCounter > 100) {
this.usageSnooper.startSnooper();
}
if (this.tickCounter % 6000 == 0) {
this.usageSnooper.addMemoryStatsToSnooper();
}
this.theProfiler.endSection();
this.theProfiler.endSection();
FMLCommonHandler.instance().onPostServerTick();
}
@Override
public void updateTimeLightAndEntities() {
this.theProfiler.startSection("levels");
int var1;
long startTime = System.nanoTime();
dimensionIdsToTick = DimensionManager.getIDs();
if (threadManager == null) {
threadManager = new ThreadManager(8, "World Tick");
currentWorld = new AtomicInteger(0);
tickRunnable = new TickRunnable(this);
}
currentWorld.set(0);
if (tickCounter < 100 || theProfiler.profilingEnabled || !TickThreading.instance.enableWorldTickThreading) {
doTick();
} else {
int count = Math.min(Runtime.getRuntime().availableProcessors(), dimensionIdsToTick.length);
for (int i = 0; i < count; i++) {
threadManager.run(tickRunnable);
}
threadManager.waitForCompletion();
}
this.theProfiler.endStartSection("dim_unloading");
DimensionManager.unloadWorlds(worldTickTimes);
this.theProfiler.endStartSection("connection");
this.getNetworkThread().networkTick();
this.theProfiler.endStartSection("players");
this.serverConfigManager.sendPlayerInfoToAllPlayers();
this.theProfiler.endStartSection("tickables");
for (var1 = 0; var1 < this.tickables.size(); ++var1) {
((IUpdatePlayerListBox) this.tickables.get(var1)).update();
}
int usedTimeMilliseconds = (int) ((System.nanoTime() - startTime) / 1000000);
tickTime = (tickTime * 127 + usedTimeMilliseconds) / 128;
this.theProfiler.endSection();
}
@Declare
public static int getTickTime() {
return tickTime;
}
@Declare
public void doTick() {
int i;
while ((i = currentWorld.getAndIncrement()) < dimensionIdsToTick.length) {
int id = dimensionIdsToTick[i];
long var2 = System.nanoTime();
if (id == 0 || this.getAllowNether()) {
WorldServer var4 = DimensionManager.getWorld(id);
this.theProfiler.startSection(var4.getWorldInfo().getWorldName());
this.theProfiler.startSection("pools");
var4.getWorldVec3Pool().clear();
this.theProfiler.endSection();
if (this.tickCounter % 60 == 0) {
this.theProfiler.startSection("timeSync");
this.serverConfigManager.sendPacketToAllPlayersInDimension(new Packet4UpdateTime(var4.getTotalWorldTime(), var4.getWorldTime()), var4.provider.dimensionId);
this.theProfiler.endSection();
}
this.theProfiler.startSection("forgeTick");
FMLCommonHandler.instance().onPreWorldTick(var4);
CrashReport var6;
this.theProfiler.endStartSection("worldTick");
try {
var4.tick();
} catch (Throwable var8) {
var6 = CrashReport.makeCrashReport(var8, "Exception ticking world");
var4.addWorldInfoToCrashReport(var6);
throw new ReportedException(var6);
}
this.theProfiler.endStartSection("entityTick");
try {
var4.updateEntities();
} catch (Throwable var7) {
var6 = CrashReport.makeCrashReport(var7, "Exception ticking world entities");
var4.addWorldInfoToCrashReport(var6);
throw new ReportedException(var6);
}
this.theProfiler.endStartSection("postForgeTick");
FMLCommonHandler.instance().onPostWorldTick(var4);
this.theProfiler.endSection();
this.theProfiler.startSection("tracker");
var4.getEntityTracker().updateTrackedEntities();
this.theProfiler.endSection();
this.theProfiler.endSection();
}
worldTickTimes.get(id)[this.tickCounter % 100] = System.nanoTime() - var2;
}
}
@Declare
public void save() {
this.saveAllWorlds(false);
}
public static class TickRunnable implements Runnable {
final MinecraftServer minecraftServer;
public TickRunnable(MinecraftServer minecraftServer) {
this.minecraftServer = minecraftServer;
}
@Override
public void run() {
minecraftServer.doTick();
}
}
}