Skip to content

Commit

Permalink
Implement server data pack installing
Browse files Browse the repository at this point in the history
  • Loading branch information
MacaylaMarvelous81 committed Aug 5, 2024
1 parent 1c72eed commit 85d7490
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx1G
# check these on https://fabricmc.net/develop/
minecraft_version=1.20.5
yarn_mappings=1.20.5+build.1
loader_version=0.14.21
loader_version=0.15.0
# Mod Properties
mod_version=1.1.0
maven_group=ml.unbreakinggold
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
package ml.unbreakinggold.datapackinstaller.mixin.server;

import com.llamalad7.mixinextras.sugar.Local;
import ml.unbreakinggold.datapackinstaller.server.DatapackInstallerServer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.server.Main;
import net.minecraft.util.WorldSavePath;
import net.minecraft.world.level.storage.LevelStorage;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

@Mixin(Main.class)
@Environment(EnvType.SERVER)
public class ServerMainMixin {
@Unique
private static final Logger LOGGER = LogManager.getLogger(ServerMainMixin.class);

@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/VanillaDataPackProvider;createManager(Lnet/minecraft/world/level/storage/LevelStorage$Session;)Lnet/minecraft/resource/ResourcePackManager;"), method = "main")
private static void onMain(CallbackInfo ci, @Local LevelStorage.Session session) {
Path worldDataPackPath = session.getDirectory(WorldSavePath.DATAPACKS);
File worldDataPackDir = worldDataPackPath.toFile();
File modDataPackDir = DatapackInstallerServer.MAIN_PATH.toFile();
try {
FileUtils.copyDirectory(modDataPackDir, worldDataPackDir);
} catch(IOException exception) {
LOGGER.error("Failed to copy data packs to the world.", exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;

import java.io.File;
import java.nio.file.Path;

@Environment(EnvType.SERVER)
public class DatapackInstallerServer implements DedicatedServerModInitializer {
public static final Path MAIN_PATH = FabricLoader.getInstance().getGameDir().resolve("datapacks");

@Override
public void onInitializeServer() {
File mainFile = MAIN_PATH.toFile();

if (!mainFile.exists()) mainFile.mkdirs();
}
}
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"license": "MIT",
"icon": "assets/DatapackInstaller/icon.png",
"environment": "client",
"environment": "*",
"entrypoints": {
"client": [
"ml.unbreakinggold.datapackinstaller.client.DatapackInstallerClient"
Expand All @@ -25,7 +25,7 @@
"DatapackInstaller.mixins.json"
],
"depends": {
"fabricloader": ">=0.14.21",
"fabricloader": ">=0.15.0",
"minecraft": ">=1.20.5"
}
}

0 comments on commit 85d7490

Please sign in to comment.