Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 4a5eeb7

Browse files
committed
Make changes according to the PR comments.
1 parent c2a5e23 commit 4a5eeb7

7 files changed

Lines changed: 107 additions & 62 deletions

File tree

patchwork-events-world/src/main/java/com/patchworkmc/impl/event/world/WorldEvents.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,16 @@ public static List<Biome.SpawnEntry> getPotentialSpawns(IWorld world, EntityCate
4545

4646
return event.getList();
4747
}
48+
49+
public static void onWorldLoad(IWorld world) {
50+
MinecraftForge.EVENT_BUS.post(new WorldEvent.Load(world));
51+
}
52+
53+
public static void onWorldUnload(IWorld world) {
54+
MinecraftForge.EVENT_BUS.post(new WorldEvent.Unload(world));
55+
}
56+
57+
public static void onWorldSave(IWorld world) {
58+
MinecraftForge.EVENT_BUS.post(new WorldEvent.Save(world));
59+
}
4860
}

patchwork-events-world/src/main/java/com/patchworkmc/mixin/event/world/MixinClientWorld.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import java.util.function.BiFunction;
2323

24-
import net.minecraftforge.common.MinecraftForge;
25-
import net.minecraftforge.event.world.WorldEvent;
2624
import org.spongepowered.asm.mixin.Mixin;
2725
import org.spongepowered.asm.mixin.injection.At;
2826
import org.spongepowered.asm.mixin.injection.Inject;
@@ -36,15 +34,16 @@
3634
import net.minecraft.world.World;
3735
import net.minecraft.client.world.ClientWorld;
3836

37+
import com.patchworkmc.impl.event.world.WorldEvents;
38+
3939
@Mixin(ClientWorld.class)
4040
public abstract class MixinClientWorld extends World {
4141
protected MixinClientWorld(LevelProperties levelProperties, DimensionType dimensionType, BiFunction<World, Dimension, ChunkManager> chunkManagerProvider, Profiler profiler, boolean isClient) {
4242
super(levelProperties, dimensionType, chunkManagerProvider, profiler, isClient);
4343
}
4444

4545
@Inject(method = "<init>", at = @At(value = "TAIL"))
46-
private void hookConstructor(CallbackInfo info) {
47-
ClientWorld world = (ClientWorld) (Object) this;
48-
MinecraftForge.EVENT_BUS.post(new WorldEvent.Load(world));
46+
private void postConstruct(CallbackInfo info) {
47+
WorldEvents.onWorldLoad((ClientWorld) (Object) this);
4948
}
5049
}

patchwork-events-world/src/main/java/com/patchworkmc/mixin/event/world/MixinMinecraftClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package com.patchworkmc.mixin.event.world;
2121

22-
import net.minecraftforge.common.MinecraftForge;
23-
import net.minecraftforge.event.world.WorldEvent;
2422
import org.objectweb.asm.Opcodes;
2523
import org.spongepowered.asm.mixin.Mixin;
2624
import org.spongepowered.asm.mixin.Shadow;
@@ -31,6 +29,8 @@
3129
import net.minecraft.client.MinecraftClient;
3230
import net.minecraft.client.world.ClientWorld;
3331

32+
import com.patchworkmc.impl.event.world.WorldEvents;
33+
3434
@Mixin(MinecraftClient.class)
3535
public class MixinMinecraftClient {
3636
@Shadow
@@ -39,14 +39,14 @@ public class MixinMinecraftClient {
3939
@Inject(method = "joinWorld", at = @At(value = "HEAD"))
4040
private void hookJoinWorld(CallbackInfo info) {
4141
if (this.world != null) {
42-
MinecraftForge.EVENT_BUS.post(new WorldEvent.Unload(this.world));
42+
WorldEvents.onWorldUnload(this.world);
4343
}
4444
}
4545

4646
@Inject(method = "disconnect(Lnet/minecraft/client/gui/screen/Screen;)V", at = @At(value = "FIELD", opcode = Opcodes.GETFIELD, target = "net/minecraft/client/MinecraftClient.world : Lnet/minecraft/client/world/ClientWorld;"))
4747
private void hookDisconnect(CallbackInfo info) {
4848
if (this.world != null) {
49-
MinecraftForge.EVENT_BUS.post(new WorldEvent.Unload(this.world));
49+
WorldEvents.onWorldUnload(this.world);
5050
}
5151
}
5252
}

patchwork-events-world/src/main/java/com/patchworkmc/mixin/event/world/MixinMinecraftServer.java

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import java.util.Iterator;
2424
import java.util.Map;
2525

26-
import net.minecraftforge.common.MinecraftForge;
27-
import net.minecraftforge.event.world.WorldEvent;
2826
import org.spongepowered.asm.mixin.Final;
2927
import org.spongepowered.asm.mixin.Mixin;
3028
import org.spongepowered.asm.mixin.Shadow;
@@ -37,6 +35,8 @@
3735
import net.minecraft.util.NonBlockingThreadExecutor;
3836
import net.minecraft.world.dimension.DimensionType;
3937

38+
import com.patchworkmc.impl.event.world.WorldEvents;
39+
4040
@Mixin(MinecraftServer.class)
4141
public abstract class MixinMinecraftServer extends NonBlockingThreadExecutor<ServerTask> {
4242
public MixinMinecraftServer(String name) {
@@ -47,66 +47,72 @@ public MixinMinecraftServer(String name) {
4747
@Final
4848
private Map<DimensionType, ServerWorld> worlds;
4949

50+
/*
51+
// This is a varient of the world load hook that is less likely to break mods and more likely to break on updates.
5052
// Should get called once per loop, regardless of which if branch it takes.
51-
// @Inject(
52-
// method = "createWorlds",
53-
// slice = @Slice(
54-
// from = @At(value = "INVOKE", target = "java/util/Iterator.hasNext ()Z")
55-
// ),
56-
// at = @At(value = "JUMP", opcode = Opcodes.GOTO),
57-
// locals = LocalCapture.CAPTURE_FAILHARD
58-
// )
59-
// private void hookCreateWorlds(WorldSaveHandler worldSaveHandler, LevelProperties properties, LevelInfo levelInfo, WorldGenerationProgressListener worldGenerationProgressListener, CallbackInfo ci, ServerWorld serverWorld, ServerWorld serverWorld2, Iterator var7, DimensionType dimensionType) {
60-
// MinecraftForge.EVENT_BUS.post(new WorldEvent.Load(this.worlds.get(dimensionType)));
61-
// }
62-
63-
// Alternatively, mixin to the put call, and special case overworld.
64-
// Perhaps move the special case outside of the loop?
53+
@Inject(
54+
method = "createWorlds",
55+
slice = @Slice(
56+
from = @At(value = "INVOKE", target = "java/util/Iterator.hasNext ()Z")
57+
),
58+
at = @At(value = "JUMP", opcode = Opcodes.GOTO),
59+
locals = LocalCapture.CAPTURE_FAILHARD
60+
)
61+
private void hookCreateWorlds(WorldSaveHandler worldSaveHandler, LevelProperties properties, LevelInfo levelInfo, WorldGenerationProgressListener worldGenerationProgressListener, CallbackInfo ci, ServerWorld serverWorld, ServerWorld serverWorld2, Iterator var7, DimensionType dimensionType) {
62+
WorldEvents.onWorldLoad(this.worlds.get(dimensionType));
63+
}
64+
65+
*/
66+
67+
// This injection gets called at the beginning of each loop, and is used to special case the overworld dimension type.
6568
@Redirect(method = "createWorlds", at = @At(value = "INVOKE", target = "java/util/Iterator.next ()Ljava/lang/Object;"))
6669
private Object proxyNextWorldToSpecialCaseOverworld(Iterator iterator) {
6770
DimensionType type = (DimensionType) iterator.next();
6871

6972
if (type == DimensionType.OVERWORLD) {
70-
MinecraftForge.EVENT_BUS.post(new WorldEvent.Load(this.worlds.get(type)));
73+
WorldEvents.onWorldLoad(this.worlds.get(type));
7174
}
7275

7376
return type;
7477
}
7578

79+
// This injection handles every other dimension type.
7680
@Redirect(method = "createWorlds", at = @At(value = "INVOKE", target = "java/util/Map.put (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", ordinal = 1))
7781
private Object proxyPutWorld(Map worlds, Object type, Object world) {
7882
worlds.put(type, world);
79-
MinecraftForge.EVENT_BUS.post(new WorldEvent.Load((ServerWorld) world));
83+
WorldEvents.onWorldLoad((ServerWorld) world);
8084
return world;
8185
}
8286

83-
// TODO: Should we Inject into ServerWorld.close instead? Currently, this follows Forge's patch location.
8487
@Redirect(method = "shutdown", at = @At(value = "INVOKE", target = "net/minecraft/server/world/ServerWorld.close ()V"))
8588
private void proxyClose(ServerWorld world) throws IOException {
86-
MinecraftForge.EVENT_BUS.post(new WorldEvent.Unload(world));
89+
WorldEvents.onWorldUnload(world);
8790
world.close();
8891
}
8992

93+
/*
94+
// TODO: DimensionManager, and move this into a seperate module
95+
@Inject(method = "createWorlds", at = @At(value = "HEAD"))
96+
private void hookCreateWorldsForDimensionRegistration(CallbackInfo info) {
97+
DimensionManager.fireRegister();
98+
}
99+
90100
@Shadow
91101
private int ticks;
92102
93-
// TODO: DimensionManager, and move this into a seperate module
94-
// @Inject(method = "createWorlds", at = @At(value = "HEAD"))
95-
// private void hookCreateWorldsForDimensionRegistration(CallbackInfo info) {
96-
// DimensionManager.fireRegister();
97-
// }
98-
99-
// @Redirect(method = "tickWorlds", at = @At(value = "INVOKE_STRING", target = "net/minecraft/util/profiler/DisableableProfiler.swap (Ljava/lang/String;)V", args = { "ldc=connection" }))
100-
// private void hookTickWorldsForDimensionUnload(DisableableProfiler profiler, String section) {
101-
// MinecraftServer server = (MinecraftServer) (Object) this;
102-
// profiler.swap("dim_unloading");
103-
// DimensionManager.unloadWorlds(server, this.ticks % 200);
104-
// profiler.swap(section);
105-
// }
106-
107-
// @Redirect(method = "getWorld", at = @At(value = "INVOKE", target = "java/util/Map.get (Ljava/lang/Object;)Ljava/lang/Object;"))
108-
// private Object hookGetWorld(Map worlds, Object type) {
109-
// MinecraftServer server = (MinecraftServer) (Object) this;
110-
// return DimensionManager.getWorld(server, type, true, true);
111-
// }
103+
@Redirect(method = "tickWorlds", at = @At(value = "INVOKE_STRING", target = "net/minecraft/util/profiler/DisableableProfiler.swap (Ljava/lang/String;)V", args = { "ldc=connection" }))
104+
private void hookTickWorldsForDimensionUnload(DisableableProfiler profiler, String section) {
105+
MinecraftServer server = (MinecraftServer) (Object) this;
106+
profiler.swap("dim_unloading");
107+
DimensionManager.unloadWorlds(server, this.ticks % 200);
108+
profiler.swap(section);
109+
}
110+
111+
@Redirect(method = "getWorld", at = @At(value = "INVOKE", target = "java/util/Map.get (Ljava/lang/Object;)Ljava/lang/Object;"))
112+
private Object hookGetWorld(Map worlds, Object type) {
113+
MinecraftServer server = (MinecraftServer) (Object) this;
114+
return DimensionManager.getWorld(server, type, true, true);
115+
}
116+
117+
*/
112118
}

patchwork-events-world/src/main/java/com/patchworkmc/mixin/event/world/MixinServerWorld.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import java.util.function.BiFunction;
2323

24-
import net.minecraftforge.common.MinecraftForge;
25-
import net.minecraftforge.event.world.WorldEvent;
2624
import org.spongepowered.asm.mixin.Mixin;
2725
import org.spongepowered.asm.mixin.injection.At;
2826
import org.spongepowered.asm.mixin.injection.Inject;
@@ -47,8 +45,7 @@ protected MixinServerWorld(LevelProperties levelProperties, DimensionType dimens
4745

4846
@Inject(method = "save", at = @At(value = "INVOKE", target = "net/minecraft/server/world/ServerChunkManager.save (Z)V"))
4947
private void hookSave(CallbackInfo info) {
50-
ServerWorld world = (ServerWorld) (Object) this;
51-
MinecraftForge.EVENT_BUS.post(new WorldEvent.Save(world));
48+
WorldEvents.onWorldSave((ServerWorld) (Object) this);
5249
}
5350

5451
// TODO: consider adding a shift to before obtaining the ChunkManager to match forge more closely

patchwork-events-world/src/main/java/net/minecraftforge/event/world/WorldEvent.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,18 @@ public IWorld getWorld() {
8080
* {@link MinecraftServer#createWorlds(WorldSaveHandler, LevelProperties, LevelInfo, WorldGenerationProgressListener)},
8181
* TODO: {@link DimensionManager#initDimension(int)}</p>
8282
*
83-
* <p>This event is not Cancelable.</p>
83+
* <p>This event is not cancellable.</p>
8484
*
8585
* <p>This event does not have a result.</p>
8686
*
8787
* <p>This event is fired on the {@link MinecraftForge#EVENT_BUS}.</p>
8888
*/
8989
public static class Load extends WorldEvent {
90+
// For EventBus
91+
public Load() {
92+
super();
93+
}
94+
9095
public Load(IWorld world) {
9196
super(world);
9297
}
@@ -101,13 +106,18 @@ public Load(IWorld world) {
101106
* {@link MinecraftServer#shutdown()},
102107
* TODO: {@link DimensionManager#unloadWorlds()}</p>
103108
*
104-
* <p>This event is not Cancelable.</p>
109+
* <p>This event is not cancellable.</p>
105110
*
106111
* <p>This event does not have a result.</p>
107112
*
108113
* <p>This event is fired on the {@link MinecraftForge#EVENT_BUS}.</p>
109114
*/
110115
public static class Unload extends WorldEvent {
116+
// For EventBus
117+
public Unload() {
118+
super();
119+
}
120+
111121
public Unload(IWorld world) {
112122
super(world);
113123
}
@@ -119,20 +129,25 @@ public Unload(IWorld world) {
119129
* <p>This event is fired when a world is saved in
120130
* {@link ServerWorld#save(ProgressListener, boolean, boolean)}.</p>
121131
*
122-
* <p>This event is not Cancelable.</p>
132+
* <p>This event is not cancellable.</p>
123133
*
124134
* <p>This event does not have a result.</p>
125135
*
126136
* <p>This event is fired on the {@link MinecraftForge#EVENT_BUS}.</p>
127137
*/
128138
public static class Save extends WorldEvent {
139+
// For EventBus
140+
public Save() {
141+
super();
142+
}
143+
129144
public Save(IWorld world) {
130145
super(world);
131146
}
132147
}
133148

134149
/**
135-
* Called by ServerWorld to gather a list of all possible entities that can spawn at the specified location.
150+
* Called by {@link ServerWorld} to gather a list of all possible entities that can spawn at the specified location.
136151
* If an entry is added to the list, it needs to be a globally unique instance.
137152
* The event is called in {@link SpawnHelper#method_8664(ChunkGenerator, EntityCategory, Random, BlockPos)} as well as
138153
* {@link SpawnHelper#method_8659(ChunkGenerator, EntityCategory, SpawnEntry, BlockPos)}
@@ -144,6 +159,14 @@ public static class PotentialSpawns extends WorldEvent {
144159
private final BlockPos pos;
145160
private final List<SpawnEntry> list;
146161

162+
// For EventBus
163+
public PotentialSpawns() {
164+
super();
165+
type = null;
166+
pos = null;
167+
list = null;
168+
}
169+
147170
public PotentialSpawns(IWorld world, EntityCategory type, BlockPos pos, List<SpawnEntry> oldList) {
148171
super(world);
149172
this.pos = pos;
@@ -178,9 +201,15 @@ public boolean isCancelable() {
178201
* Called by ServerWorld when it attempts to create a spawnpoint for a dimension.
179202
* Canceling the event will prevent the vanilla code from running.
180203
*/
181-
182204
public static class CreateSpawnPosition extends WorldEvent {
183205
private final LevelInfo settings;
206+
207+
// For EventBus
208+
public CreateSpawnPosition() {
209+
super();
210+
settings = null;
211+
}
212+
184213
public CreateSpawnPosition(IWorld world, LevelInfo settings) {
185214
super(world);
186215
this.settings = settings;

patchwork-events-world/src/main/resources/patchwork-events-world.mixins.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
"package": "com.patchworkmc.mixin.event.world",
44
"compatibilityLevel": "JAVA_8",
55
"mixins": [
6+
"MixinChunkGenerator",
7+
"MixinMinecraftServer",
68
"MixinServerPlayerInteractionManager",
7-
"MixinThreadedAnvilChunkStorage",
8-
"MixinClientWorld",
99
"MixinServerWorld",
10-
"MixinMinecraftServer",
11-
"MixinMinecraftClient",
1210
"MixinSpawnHelper",
13-
"MixinChunkGenerator"
11+
"MixinThreadedAnvilChunkStorage",
12+
],
13+
"client": [
14+
"MixinClientWorld",
15+
"MixinMinecraftClient"
1416
],
1517
"injectors": {
1618
"defaultRequire": 1

0 commit comments

Comments
 (0)