|
1 | 1 | package me.isaiah.multiworld.fabric; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
| 4 | +import java.nio.file.Path; |
4 | 5 | import java.util.Collections; |
5 | 6 | import java.util.HashMap; |
6 | 7 | import java.util.List; |
|
11 | 12 | import dimapi.FabricDimensionInternals; |
12 | 13 | import me.isaiah.multiworld.ICreator; |
13 | 14 | import me.isaiah.multiworld.MultiworldMod; |
| 15 | +import me.isaiah.multiworld.Utils; |
| 16 | +import multiworld.api.WorldFolderMode; |
14 | 17 | import net.minecraft.registry.Registry; |
15 | 18 | import net.minecraft.registry.RegistryKey; |
16 | 19 | import net.minecraft.registry.RegistryKeys; |
|
25 | 28 | import net.minecraft.util.Identifier; |
26 | 29 | import net.minecraft.util.math.BlockPos; |
27 | 30 | import net.minecraft.util.math.Vec3d; |
| 31 | +import net.minecraft.util.path.SymlinkValidationException; |
28 | 32 | import net.minecraft.world.Difficulty; |
29 | 33 | import net.minecraft.world.GameRules; |
30 | 34 | import net.minecraft.world.SaveProperties; |
@@ -88,30 +92,63 @@ public ServerWorld create_world(String id, Identifier dim, ChunkGenerator gen, D |
88 | 92 | return world; |
89 | 93 | } |
90 | 94 |
|
| 95 | + /** |
| 96 | + * Reads the gamerules from a level.dat file in the given world folder. |
| 97 | + * @param savesDir Path to the root saves directory (e.g. ./saves). |
| 98 | + * @param worldName Name of the world folder. |
| 99 | + * @param dataFixer The server's DataFixer instance. |
| 100 | + * @return A GameRules object containing the rules from level.dat. |
| 101 | + * @throws IOException if the file cannot be read. |
| 102 | + * @throws SymlinkValidationException |
| 103 | + */ |
91 | 104 | public static GameRules readGameRules(Identifier id) throws IOException { |
| 105 | + try { |
| 106 | + return mw$readGameRules(MultiworldMod.mc, id); |
| 107 | + } catch (IOException | SymlinkValidationException e) { |
| 108 | + // TODO Auto-generated catch block |
| 109 | + // e.printStackTrace(); |
| 110 | + throw new IOException(e); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Reads gamerules from a world's level.dat |
| 116 | + */ |
| 117 | + public static GameRules mw$readGameRules(MinecraftServer server, Identifier worldId) |
| 118 | + throws IOException, SymlinkValidationException { |
| 119 | + |
| 120 | + String name = Utils.getWorldName(worldId); |
| 121 | + Path customWorldPath = Utils.getWorldStoragePath(); |
92 | 122 |
|
93 | | - try (Session session = MultiworldWorld.mw$getSession(MultiworldMod.mc, id)) { |
| 123 | + Optional<WorldFolderMode> mode = Utils.getFolderMode(worldId); |
| 124 | + if (!mode.isEmpty()) { |
| 125 | + customWorldPath = Utils.getWorldPath(worldId, mode.get()).getParent(); |
| 126 | + |
| 127 | + if (mode.get() == WorldFolderMode.VANILLA) { |
| 128 | + name = worldId.getPath(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + LevelStorage storage = LevelStorage.create(customWorldPath); |
| 133 | + |
| 134 | + try (Session session = storage.createSession(name)) { |
94 | 135 | Dynamic<?> dynamic = session.readLevelProperties(); |
95 | | - |
96 | | - RegistryWrapper.WrapperLookup lookup = MultiworldMod.mc.getRegistryManager(); |
97 | | - |
98 | | - Registry<DimensionOptions> dimensionRegistry = MultiworldMod.mc.getRegistryManager().get(RegistryKeys.DIMENSION); |
99 | | - |
100 | | - DataConfiguration dataConfig = MultiworldMod.mc.getSaveProperties().getDataConfiguration(); |
| 136 | + |
| 137 | + Registry<DimensionOptions> dimensionRegistry = server.getRegistryManager().get(RegistryKeys.DIMENSION); |
| 138 | + DataConfiguration dataConfig = server.getSaveProperties().getDataConfiguration(); |
101 | 139 |
|
102 | 140 | SaveProperties props = LevelStorage.parseSaveProperties( |
103 | 141 | dynamic, |
104 | 142 | dataConfig, |
105 | 143 | dimensionRegistry, |
106 | | - MultiworldMod.mc.getRegistryManager() |
| 144 | + server.getRegistryManager() |
107 | 145 | ).properties(); |
108 | | - |
| 146 | + |
109 | 147 | if (!(props instanceof LevelProperties levelProps)) { |
110 | 148 | throw new IllegalStateException("SaveProperties is not a LevelProperties"); |
111 | 149 | } |
112 | | - |
113 | | - session.close(); |
114 | | - // Return the gamerules object |
| 150 | + |
| 151 | + // Return the gamerules directly |
115 | 152 | return levelProps.getGameRules(); |
116 | 153 | } |
117 | 154 | } |
|
0 commit comments