-
-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathServerLevel.java.patch
More file actions
334 lines (303 loc) · 16.5 KB
/
Copy pathServerLevel.java.patch
File metadata and controls
334 lines (303 loc) · 16.5 KB
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
--- a/net/minecraft/server/level/ServerLevel.java
+++ b/net/minecraft/server/level/ServerLevel.java
@@ -215,11 +_,17 @@
private boolean handlingTick;
private final List<CustomSpawner> customSpawners;
private @Nullable EnderDragonFight dragonFight;
- private final Int2ObjectMap<EnderDragonPart> dragonParts = new Int2ObjectOpenHashMap<>();
+ final Int2ObjectMap<net.neoforged.neoforge.entity.PartEntity<?>> dragonParts = new Int2ObjectOpenHashMap<>();
private final StructureManager structureManager;
private final StructureCheck structureCheck;
private final boolean tickTime;
private final LevelDebugSynchronizers debugSynchronizers = new LevelDebugSynchronizers(this);
+ // NeoForge: If present, this level uses its own seed instead of the server's global seed
+ private final java.util.OptionalLong seedOverride;
+ /// {@return the override for the seed, specified in the [LevelStem] this level was constructed from}, if any
+ public java.util.OptionalLong getSeedOverride() {
+ return this.seedOverride;
+ }
public ServerLevel(
MinecraftServer server,
@@ -233,10 +_,12 @@
List<CustomSpawner> customSpawners,
boolean tickTime
) {
+ // NeoForge: set seed override asap because things like the chunk cache constructor check level.getSeed()
+ this.seedOverride = levelStem.seedOverride();
+ biomeZoomSeed = net.minecraft.util.ExtraCodecs.fromOptionalLong.apply(levelStem.seedOverride()).map(net.minecraft.world.level.biome.BiomeManager::obfuscateSeed).orElse(biomeZoomSeed);
super(levelData, dimension, server.registryAccess(), levelStem.type(), false, isDebug, biomeZoomSeed, server.getMaxChainedNeighborUpdates());
this.tickTime = tickTime;
this.server = server;
- this.customSpawners = customSpawners;
this.serverLevelData = levelData;
ChunkGenerator generator = levelStem.generator();
boolean syncWrites = server.forceSynchronousWrites();
@@ -279,7 +_,7 @@
WorldGenSettings worldGenSettings = server.getWorldGenSettings();
WorldOptions options = worldGenSettings.options();
- long seed = options.seed();
+ long seed = seedOverride.orElse(options.seed()); // NeoForge: use the seed override here too
this.structureCheck = new StructureCheck(
this.chunkSource.chunkScanner(),
this.registryAccess(),
@@ -303,6 +_,10 @@
this.waypointManager = new ServerWaypointManager();
this.environmentAttributes = EnvironmentAttributeSystem.builder().addDefaultLayers(this).build();
this.updateSkyBrightness();
+
+ // Neo: Move the initialization of customSpawners to the end of costructor
+ // Providing a fully initialized ServerLevel instance for the ServerLevelEvent.CustomSpawners
+ this.customSpawners = net.neoforged.neoforge.event.EventHooks.getCustomSpawners(this, customSpawners);
}
@Deprecated
@@ -354,7 +_,11 @@
if (this.sleepStatus.areEnoughSleeping(percentage) && this.sleepStatus.areEnoughDeepSleeping(percentage, this.players)) {
Optional<Holder<WorldClock>> defaultClock = this.dimensionType().defaultClock();
if (this.getGameRules().get(GameRules.ADVANCE_TIME) && defaultClock.isPresent()) {
- this.server.clockManager().moveToTimeMarker(defaultClock.get(), ClockTimeMarkers.WAKE_UP_FROM_SLEEP);
+ // Neo: Allow mods to control how the clock is adjusted after sleep
+ var adjustment = net.neoforged.neoforge.event.EventHooks.onSleepFinished(this, new net.neoforged.neoforge.common.util.ClockAdjustment.Marker(ClockTimeMarkers.WAKE_UP_FROM_SLEEP));
+ if (adjustment != null) {
+ adjustment.apply(this.server.clockManager(), defaultClock.get());
+ }
}
this.wakeUpAllPlayers();
@@ -392,7 +_,7 @@
this.handlingTick = false;
profiler.pop();
- boolean isActive = this.chunkSource.hasActiveTickets();
+ boolean isActive = this.chunkSource.hasActiveTickets() || net.neoforged.neoforge.common.world.chunk.ForcedChunkManager.hasForcedChunks(this);
if (isActive) {
this.resetEmptyTime();
}
@@ -429,7 +_,9 @@
}
profiler.push("tick");
- this.guardEntityTick(this::tickNonPassenger, entity);
+ if (!entity.isRemoved() && !(entity instanceof net.neoforged.neoforge.entity.PartEntity)) {
+ this.guardEntityTick(this::tickNonPassenger, entity);
+ }
profiler.pop();
}
}
@@ -569,6 +_,7 @@
BlockPos topPos = this.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING, pos);
BlockPos belowPos = topPos.below();
Biome biome = this.getBiome(topPos).value();
+ if (this.isAreaLoaded(belowPos, 1)) // Forge: check area to avoid loading neighbors in unloaded chunks
if (biome.shouldFreeze(this, belowPos)) {
this.setBlockAndUpdate(belowPos, Blocks.ICE.defaultBlockState());
}
@@ -771,15 +_,19 @@
.broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.THUNDER_LEVEL_CHANGE, this.thunderLevel), this.dimension());
}
+ /* The function in use here has been replaced in order to only send the weather info to players in the correct dimension,
+ * rather than to all players on the server. This is what causes the client-side rain, as the
+ * client believes that it has started raining locally, rather than in another dimension.
+ */
if (wasRaining != this.isRaining()) {
if (wasRaining) {
- this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.STOP_RAINING, 0.0F));
+ this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.STOP_RAINING, 0.0F), this.dimension());
} else {
- this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.START_RAINING, 0.0F));
+ this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.START_RAINING, 0.0F), this.dimension());
}
- this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.RAIN_LEVEL_CHANGE, this.rainLevel));
- this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.THUNDER_LEVEL_CHANGE, this.thunderLevel));
+ this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.RAIN_LEVEL_CHANGE, this.rainLevel), this.dimension());
+ this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.THUNDER_LEVEL_CHANGE, this.thunderLevel), this.dimension());
}
}
@@ -817,7 +_,11 @@
entity.tickCount++;
profiler.push(entity.typeHolder()::getRegisteredName);
profiler.incrementCounter("tickNonPassenger");
- entity.tick();
+ // Neo: Permit cancellation of Entity#tick via EntityTickEvent.Pre
+ if (!net.neoforged.neoforge.event.EventHooks.fireEntityTickPre(entity).isCanceled()) {
+ entity.tick();
+ net.neoforged.neoforge.event.EventHooks.fireEntityTickPost(entity);
+ }
profiler.pop();
for (Entity passenger : entity.getPassengers()) {
@@ -880,6 +_,7 @@
} else {
this.entityManager.autoSave();
}
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.event.level.LevelEvent.Save(this));
}
}
@@ -969,6 +_,7 @@
}
private void addPlayer(ServerPlayer player) {
+ if (net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.event.entity.EntityJoinLevelEvent(player, this)).isCanceled()) return;
Entity existing = this.getEntity(player.getUUID());
if (existing != null) {
LOGGER.warn("Force-added player with duplicate UUID {}", player.getUUID());
@@ -976,7 +_,8 @@
this.removePlayerImmediately((ServerPlayer)existing, Entity.RemovalReason.DISCARDED);
}
- this.entityManager.addNewEntity(player);
+ this.entityManager.addNewEntityWithoutEvent(player);
+ player.onAddedToLevel();
}
private boolean addEntity(Entity entity) {
@@ -984,7 +_,12 @@
LOGGER.warn("Tried to add entity {} but it was marked as removed already", entity.typeHolder().getRegisteredName());
return false;
} else {
- return this.entityManager.addNewEntity(entity);
+ if (this.entityManager.addNewEntity(entity)) {
+ entity.onAddedToLevel();
+ return true;
+ } else {
+ return false;
+ }
}
}
@@ -1025,6 +_,12 @@
public void playSeededSound(
@Nullable Entity except, double x, double y, double z, Holder<SoundEvent> sound, SoundSource source, float volume, float pitch, long seed
) {
+ net.neoforged.neoforge.event.PlayLevelSoundEvent.AtPosition event = net.neoforged.neoforge.event.EventHooks.onPlaySoundAtPosition(this, x, y, z, sound, source, volume, pitch);
+ if (event.isCanceled() || event.getSound() == null) return;
+ sound = event.getSound();
+ source = event.getSource();
+ volume = event.getNewVolume();
+ pitch = event.getNewPitch();
this.server
.getPlayerList()
.broadcast(
@@ -1042,6 +_,12 @@
public void playSeededSound(
@Nullable Entity except, Entity sourceEntity, Holder<SoundEvent> sound, SoundSource source, float volume, float pitch, long seed
) {
+ net.neoforged.neoforge.event.PlayLevelSoundEvent.AtEntity event = net.neoforged.neoforge.event.EventHooks.onPlaySoundAtEntity(sourceEntity, sound, source, volume, pitch);
+ if (event.isCanceled() || event.getSound() == null) return;
+ sound = event.getSound();
+ source = event.getSource();
+ volume = event.getNewVolume();
+ pitch = event.getNewPitch();
this.server
.getPlayerList()
.broadcast(
@@ -1100,6 +_,7 @@
@Override
public void gameEvent(Holder<GameEvent> gameEvent, Vec3 position, GameEvent.Context context) {
+ if (!net.neoforged.neoforge.common.CommonHooks.onVanillaGameEvent(this, gameEvent, position, context)) return;
this.gameEventDispatcher.post(gameEvent, position, context);
}
@@ -1138,6 +_,7 @@
@Override
public void updateNeighborsAt(BlockPos pos, Block sourceBlock) {
+ net.neoforged.neoforge.event.EventHooks.onNeighborNotify(this, pos, this.getBlockState(pos), java.util.EnumSet.allOf(Direction.class), false).isCanceled();
this.updateNeighborsAt(pos, sourceBlock, ExperimentalRedstoneUtils.initialOrientation(this, null, null));
}
@@ -1148,6 +_,10 @@
@Override
public void updateNeighborsAtExceptFromFacing(BlockPos pos, Block blockObject, Direction skipDirection, @Nullable Orientation orientation) {
+ java.util.EnumSet<Direction> directions = java.util.EnumSet.allOf(Direction.class);
+ directions.remove(skipDirection);
+ if (net.neoforged.neoforge.event.EventHooks.onNeighborNotify(this, pos, this.getBlockState(pos), directions, false).isCanceled())
+ return;
this.neighborUpdater.updateNeighborsAtExceptFromFacing(pos, blockObject, skipDirection, orientation);
}
@@ -1194,7 +_,7 @@
Explosion.BlockInteraction blockInteraction = switch (interactionType) {
case NONE -> Explosion.BlockInteraction.KEEP;
case BLOCK -> this.getDestroyType(GameRules.BLOCK_EXPLOSION_DROP_DECAY);
- case MOB -> this.getGameRules().get(GameRules.MOB_GRIEFING)
+ case MOB -> net.neoforged.neoforge.event.EventHooks.canEntityGrief(this, source)
? this.getDestroyType(GameRules.MOB_EXPLOSION_DROP_DECAY)
: Explosion.BlockInteraction.KEEP;
case TNT -> this.getDestroyType(GameRules.TNT_EXPLOSION_DROP_DECAY);
@@ -1202,6 +_,7 @@
};
Vec3 center = new Vec3(x, y, z);
ServerExplosion explosion = new ServerExplosion(this, source, damageSource, damageCalculator, center, r, fire, blockInteraction);
+ if (net.neoforged.neoforge.event.EventHooks.onExplosionStart(this, explosion)) return;
int blockCount = explosion.explode();
ParticleOptions explosionParticle = explosion.isSmall() ? smallExplosionParticles : largeExplosionParticles;
@@ -1379,7 +_,7 @@
}
@Override
- public Collection<EnderDragonPart> dragonParts() {
+ public Collection<net.neoforged.neoforge.entity.PartEntity<?>> dragonParts() {
return this.dragonParts.values();
}
@@ -1640,7 +_,8 @@
@Override
public long getSeed() {
- return this.server.getWorldGenSettings().options().seed();
+ // NeoForge: use seed override if present, otherwise default to vanilla global seed
+ return this.getSeedOverride().orElseGet(() -> this.server.getWorldGenSettings().options().seed());
}
public @Nullable EnderDragonFight getDragonFight() {
@@ -1906,8 +_,8 @@
ServerLevel.this.navigatingMobs.add(mob);
}
- if (entity instanceof EnderDragon dragon) {
- for (EnderDragonPart subEntity : dragon.getSubEntities()) {
+ if (entity.isMultipartEntity()) {
+ for(net.neoforged.neoforge.entity.PartEntity<?> subEntity : entity.getParts()) {
ServerLevel.this.dragonParts.put(subEntity.getId(), subEntity);
}
}
@@ -1927,25 +_,57 @@
if (ServerLevel.this.isUpdatingNavigations) {
String message = "onTrackingStart called during navigation iteration";
Util.logAndPauseIfInIde(
- "onTrackingStart called during navigation iteration", new IllegalStateException("onTrackingStart called during navigation iteration")
+ "onTrackingStart called during navigation iteration", new IllegalStateException("onTrackingStart called during navigation iteration")
);
}
ServerLevel.this.navigatingMobs.remove(mob);
}
- if (entity instanceof EnderDragon dragon) {
- for (EnderDragonPart subEntity : dragon.getSubEntities()) {
+ if (entity.isMultipartEntity()) {
+ for(net.neoforged.neoforge.entity.PartEntity<?> subEntity : entity.getParts()) {
ServerLevel.this.dragonParts.remove(subEntity.getId());
}
}
entity.updateDynamicGameEventListener(DynamicGameEventListener::remove);
ServerLevel.this.debugSynchronizers.dropEntity(entity);
+
+ entity.onRemovedFromLevel();
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.event.entity.EntityLeaveLevelEvent(entity, ServerLevel.this));
}
public void onSectionChange(Entity entity) {
entity.updateDynamicGameEventListener(DynamicGameEventListener::move);
}
+ }
+
+ private final net.neoforged.neoforge.capabilities.CapabilityListenerHolder capListenerHolder = new net.neoforged.neoforge.capabilities.CapabilityListenerHolder();
+
+ @Override
+ public void invalidateCapabilities(BlockPos pos) {
+ capListenerHolder.invalidatePos(pos);
+ }
+
+ @Override
+ public void invalidateCapabilities(ChunkPos pos) {
+ capListenerHolder.invalidateChunk(pos);
+ }
+
+ /**
+ * Register a listener for capability invalidation.
+ * @see net.neoforged.neoforge.capabilities.ICapabilityInvalidationListener
+ */
+ public void registerCapabilityListener(BlockPos pos, net.neoforged.neoforge.capabilities.ICapabilityInvalidationListener listener) {
+ capListenerHolder.addListener(pos, listener);
+ }
+
+ /**
+ * Internal method, used to clean capability listeners that are not referenced.
+ * Do not call.
+ */
+ @org.jetbrains.annotations.ApiStatus.Internal
+ public void cleanCapabilityListenerReferences() {
+ capListenerHolder.clean();
}
}