Skip to content

Commit

Permalink
Include data packs from mod in in-world selector
Browse files Browse the repository at this point in the history
  • Loading branch information
MacaylaMarvelous81 committed Aug 3, 2024
1 parent 250d549 commit 1131a60
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.GridWidget;
import net.minecraft.text.Text;
import net.minecraft.util.WorldSavePath;
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.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -15,14 +19,19 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.function.Supplier;

@Mixin(GameMenuScreen.class)
public abstract class GameMenuScreenMixin extends Screen {
@Shadow private ButtonWidget createButton(Text text, Supplier<Screen> screenSupplier) { return null; };
@Unique private final Logger LOGGER = LogManager.getLogger(GameMenuScreenMixin.class);
@Unique private static final Text DATA_PACK_TEXT = Text.translatable("dataPack.title");

@Shadow private ButtonWidget createButton(Text text, Supplier<Screen> screenSupplier) { return null; };

private GameMenuScreenMixin(Text title) {
super(title);
}
Expand All @@ -32,6 +41,15 @@ private void onInitWidgets(CallbackInfo ci, GridWidget gridWidget, GridWidget.Ad
if (!this.client.isIntegratedServerRunning() || this.client.getServer().isRemote()) return;

adder.add(this.createButton(DATA_PACK_TEXT, () -> {
Path worldDataPackPath = ((MinecraftServerAccessor)this.client.getServer()).getSession().getDirectory(WorldSavePath.DATAPACKS);
File modDatapackDir = DatapackInstallerClient.MAIN_PATH.toFile();
File worldDataPackDir = worldDataPackPath.toFile();
try {
FileUtils.copyDirectory(modDatapackDir, worldDataPackDir);
} catch(IOException exception) {
LOGGER.error("Unable to install new data packs to world. Data packs not yet seen by this world may be missing.", exception);
}

return new PackScreen(this.client.getServer().getDataPackManager(), (dataPackManager) -> {
Collection<String> enabledProfiles = dataPackManager.getEnabledIds();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ml.unbreakinggold.datapackinstaller.mixin;

import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.storage.LevelStorage;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(MinecraftServer.class)
public interface MinecraftServerAccessor {
@Accessor
LevelStorage.Session getSession();
}
1 change: 1 addition & 0 deletions src/main/resources/DatapackInstaller.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"package": "ml.unbreakinggold.datapackinstaller.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"MinecraftServerAccessor"
],
"client": [
"CreateWorldScreenMixin",
Expand Down

0 comments on commit 1131a60

Please sign in to comment.