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

Commit 8530df3

Browse files
authored
Use Fabric's world load event. (#149)
* Use World Load event in fabric api introduced by 0.15.0 * Workaround: Fire overworld load event later * Add dep on fabric api to patchwork-events-world. TODO: we should go through and accurately state our fabric api dependencies for all modules.
1 parent 0e22a5b commit 8530df3

File tree

4 files changed

+24
-39
lines changed

4 files changed

+24
-39
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ allprojects {
5656
minecraft "com.mojang:minecraft:$Globals.mcVersion"
5757
mappings "net.fabricmc:yarn:${Globals.mcVersion}${Globals.yarnVersion}:v2"
5858
modCompile "net.fabricmc:fabric-loader:0.8.4+build.198"
59-
modCompile "net.fabricmc.fabric-api:fabric-api:0.4.2+build.246-1.14"
59+
modCompile "net.fabricmc.fabric-api:fabric-api:0.15.1+build.260-1.14"
6060

6161
implementation 'net.patchworkmc:patchwork-eventbus:2.0.1:all'
6262
implementation 'com.google.code.findbugs:jsr305:3.0.2'

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@
2929
import net.minecraft.util.math.BlockPos;
3030
import net.minecraft.world.IWorld;
3131
import net.minecraft.world.biome.Biome;
32+
import net.minecraft.world.dimension.DimensionType;
3233
import net.minecraft.world.level.LevelInfo;
3334

34-
public class WorldEvents {
35+
import net.fabricmc.api.ModInitializer;
36+
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
37+
38+
public class WorldEvents implements ModInitializer {
3539
public static boolean onCreateWorldSpawn(IWorld world, LevelInfo settings) {
3640
return MinecraftForge.EVENT_BUS.post(new WorldEvent.CreateSpawnPosition(world, settings));
3741
}
@@ -57,4 +61,15 @@ public static void onWorldUnload(IWorld world) {
5761
public static void onWorldSave(IWorld world) {
5862
MinecraftForge.EVENT_BUS.post(new WorldEvent.Save(world));
5963
}
64+
65+
@Override
66+
public void onInitialize() {
67+
ServerWorldEvents.LOAD.register((server, world) -> {
68+
// Fabric fires this much earlier than Forge does for the overworld
69+
// So, we're going to manually fire it for the overworld.
70+
if (world.getDimension().getType() != DimensionType.OVERWORLD) {
71+
onWorldLoad(world);
72+
}
73+
});
74+
}
6075
}

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

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
package net.patchworkmc.mixin.event.world;
2121

2222
import java.io.IOException;
23-
import java.util.Iterator;
2423
import java.util.Map;
2524

2625
import org.spongepowered.asm.mixin.Final;
2726
import org.spongepowered.asm.mixin.Mixin;
2827
import org.spongepowered.asm.mixin.Shadow;
2928
import org.spongepowered.asm.mixin.injection.At;
29+
import org.spongepowered.asm.mixin.injection.Inject;
3030
import org.spongepowered.asm.mixin.injection.Redirect;
31+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
3132

3233
import net.minecraft.server.MinecraftServer;
3334
import net.minecraft.server.ServerTask;
@@ -47,42 +48,10 @@ public MixinMinecraftServer(String name) {
4748
@Final
4849
private Map<DimensionType, ServerWorld> worlds;
4950

50-
/*
51-
// This is a variant of the world load hook that is less likely to break mods and more likely to break on updates.
52-
// Should get called once per loop, regardless of which if branch it takes.
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.
68-
@Redirect(method = "createWorlds", at = @At(value = "INVOKE", target = "java/util/Iterator.next ()Ljava/lang/Object;"))
69-
private Object proxyNextWorldToSpecialCaseOverworld(Iterator<DimensionType> iterator) {
70-
DimensionType type = iterator.next();
71-
72-
if (type == DimensionType.OVERWORLD) {
73-
WorldEvents.onWorldLoad(this.worlds.get(type));
74-
}
75-
76-
return type;
77-
}
78-
79-
// This injection handles every other dimension type.
80-
@Redirect(method = "createWorlds", at = @At(value = "INVOKE", target = "java/util/Map.put (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", ordinal = 1))
81-
private Object proxyPutWorld(Map<Object, Object> worlds, Object type, Object world) {
82-
worlds.put(type, world);
83-
WorldEvents.onWorldLoad((ServerWorld) world);
84-
85-
return world;
51+
// Fabric usually fires the event much earlier in the method, so this is just picking a point closer to when Forge would fire it.
52+
@Inject(method = "createWorlds", at = @At(value = "INVOKE", target = "net/minecraft/world/dimension/DimensionType.getAll ()Ljava/lang/Iterable;"))
53+
private void fireLoadForOverworld(CallbackInfo info) {
54+
WorldEvents.onWorldLoad(worlds.get(DimensionType.OVERWORLD));
8655
}
8756

8857
@Redirect(method = "shutdown", at = @At(value = "INVOKE", target = "net/minecraft/server/world/ServerWorld.close ()V"))

patchwork-events-world/src/main/resources/fabric.mod.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
],
1515
"depends": {
1616
"fabricloader": ">=0.8.4",
17+
"fabric": ">=0.15.0",
1718
"patchwork-fml": "*"
1819
},
1920
"mixins": [

0 commit comments

Comments
 (0)