Skip to content

Commit f702e80

Browse files
committed
add load order configuration
1 parent 9e56d70 commit f702e80

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
# Data Loader
22

3-
For more info visit the modrinth [page](https://modrinth.com/mod/dataloader).
3+
This mod adds a `datapacks` folder inside your game directory where you can put datapacks that will apply to all worlds.
4+
5+
## Configuration
6+
7+
Unlike sargunv's original mod, this port has a completely optional load order configuration located in `config/data-loader.json`.
8+
<br><br><br>
9+
`onlyLoadSpecified`: When set to true Minecraft will only load the datapacks in the `loadOrder` list and the fabric datapack. Unless the vanilla datapack or a suitable replacement is provided the game will crash upon launch.
10+
11+
`loadOrder`: Minecraft will load the datapacks in the order specified in this list. Datapacks in the ´datapacks´ folder needs to be prefixed with ´file/´, the `vanilla`, `fabric` and experimental datapacks don't need to be prefixed with anything.
412

513
## Setup
614

715
For setup instructions please see the [fabric wiki page](https://fabricmc.net/wiki/tutorial:setup) that relates to the IDE that you are using.
816

917
## License
1018

11-
Data loader is a port of [Sargun Vohra's Data Loader](https://gitlab.com/sargunv-mc-mods/data-loader/-/tree/1.16) for 1.18.x, Which is licensed under [Apache License 2.0](https://gitlab.com/sargunv-mc-mods/data-loader/-/blob/1.16/LICENSE)
19+
Data loader started as port of [Sargun Vohra's Data Loader](https://gitlab.com/sargunv-mc-mods/data-loader/-/tree/1.16) for 1.18.x, Which is licensed under [Apache License 2.0](https://gitlab.com/sargunv-mc-mods/data-loader/-/blob/1.16/LICENSE)
1220

1321
Data Loader is built on top of [FabricMC's Example mod](https://github.com/FabricMC/fabric-example-mod) which is licensed under [CC0-1.0](https://github.com/FabricMC/fabric-example-mod/blob/1.18/LICENSE)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dataloader;
2+
3+
import com.google.gson.Gson;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.util.ArrayList;
8+
9+
public class Config {
10+
private static final Gson gson = new Gson();
11+
public Boolean onlyLoadSpecified = false;
12+
public ArrayList<String> loadOrder = new ArrayList<>();
13+
14+
public static Config load() throws IOException {
15+
Config config;
16+
if (Files.isRegularFile(DataLoader.CONFIG_PATH)) {
17+
config = gson.fromJson(new String(Files.readAllBytes(DataLoader.CONFIG_PATH)), Config.class);
18+
} else {
19+
config = new Config();
20+
Files.createDirectories(DataLoader.CONFIG_PATH.getParent());
21+
Files.createFile(DataLoader.CONFIG_PATH);
22+
Files.write(DataLoader.CONFIG_PATH, gson.toJson(config).getBytes());
23+
}
24+
return config;
25+
}
26+
}

src/main/java/dataloader/DataLoader.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,25 @@
1616
public class DataLoader implements ModInitializer {
1717
public static UnaryOperator<Text> getSourceTextSupplier(String translationKey) {
1818
Text text = Text.translatable(translationKey);
19-
return (name) -> {
20-
return Text.translatable("pack.nameAndSource", new Object[]{name, text}).formatted(Formatting.GRAY);
21-
};
19+
return (name) -> Text.translatable("pack.nameAndSource", name, text).formatted(Formatting.GRAY);
2220
}
2321

2422
public static final ResourcePackSource RESOURCE_PACK_SOURCE = ResourcePackSource.create(getSourceTextSupplier("pack.source.dataloader"), true);
2523
public static final Path DATAPACKS_PATH = FabricLoader.getInstance().getGameDir().resolve("datapacks");
26-
public static final Logger LOGGER = LogManager.getLogger("dataloader");
24+
public static final Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("data-loader.json");
25+
public static final Logger LOGGER = LogManager.getLogger("data-loader");
26+
public static Config CONFIG;
2727

2828
@Override
2929
public void onInitialize() {
3030
try {
31+
CONFIG = Config.load();
3132
Path path = DATAPACKS_PATH;
3233
if (!Files.exists(path)) {
3334
Files.createDirectory(path);
3435
}
3536
} catch (IOException e) {
36-
e.printStackTrace();
37+
LOGGER.error("Failed to initialize data loader: ", e);
3738
}
3839

3940
}

src/main/java/dataloader/mixin/MinecraftServerMixin.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dataloader.mixin;
22

3+
import com.google.common.collect.ImmutableSet;
34
import dataloader.DataLoader;
45
import net.minecraft.client.MinecraftClient;
56
import net.minecraft.resource.*;
@@ -10,6 +11,8 @@
1011
import org.spongepowered.asm.mixin.injection.Inject;
1112
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1213

14+
import java.util.ArrayList;
15+
1316
@Mixin(value = MinecraftServer.class, priority = 1001)
1417
public class MinecraftServerMixin {
1518
@Inject(method = "loadDataPacks", at = @At("HEAD"))
@@ -26,4 +29,18 @@ private static void loadDataPacks(
2629
);
2730
}
2831

32+
@Inject(method = "createDataPackSettings", at = @At("HEAD"))
33+
private static void createDataPackSettings(ResourcePackManager dataPackManager, CallbackInfoReturnable<DataPackSettings> cir) {
34+
ArrayList<String> enabledPacks = new ArrayList<>();
35+
if (!DataLoader.CONFIG.onlyLoadSpecified)
36+
enabledPacks.addAll(
37+
dataPackManager.getEnabledNames().stream().filter(x -> !DataLoader.CONFIG.loadOrder.contains(x)).collect(ImmutableSet.toImmutableSet())
38+
);
39+
else if (!DataLoader.CONFIG.loadOrder.contains("fabric"))
40+
enabledPacks.add("fabric");
41+
enabledPacks.addAll(DataLoader.CONFIG.loadOrder);
42+
43+
DataLoader.LOGGER.info("Applying datapacks: {}", enabledPacks);
44+
dataPackManager.setEnabledProfiles(enabledPacks);
45+
}
2946
}

src/main/resources/fabric.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"sources": "https://github.com/Commander07/data-loader",
1313
"issues": "https://github.com/Commander07/data-loader/issues"
1414
},
15-
"license": "CC0-1.0",
15+
"license": "Apache-2.0",
1616
"icon": "assets/dataloader/icon.png",
1717
"environment": "*",
1818
"entrypoints": {

0 commit comments

Comments
 (0)