Skip to content

Commit

Permalink
Get enabled data packs settings
Browse files Browse the repository at this point in the history
  • Loading branch information
MacaylaMarvelous81 committed Aug 3, 2024
1 parent ca47c5a commit 6670a9d
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ml.unbreakinggold.datapackinstaller.mixin;

import com.google.common.collect.ImmutableList;
import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import ml.unbreakinggold.datapackinstaller.client.DatapackInstallerClient;
import net.minecraft.client.MinecraftClient;
Expand All @@ -9,11 +11,18 @@
import net.minecraft.client.gui.screen.world.WorldCreator;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.DirectionalLayoutWidget;
import net.minecraft.client.world.GeneratorOptionsHolder;
import net.minecraft.resource.DataConfiguration;
import net.minecraft.resource.DataPackSettings;
import net.minecraft.resource.ResourcePackManager;
import net.minecraft.resource.VanillaDataPackProvider;
import net.minecraft.server.SaveLoading;
import net.minecraft.server.command.CommandManager;
import net.minecraft.text.Text;
import net.minecraft.world.level.LevelInfo;
import net.minecraft.world.level.LevelProperties;
import net.minecraft.world.level.storage.LevelStorage;
import net.minecraft.world.level.storage.LevelSummary;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
Expand All @@ -26,6 +35,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.io.IOException;
import java.util.List;

@Mixin(EditWorldScreen.class)
public class EditWorldScreenMixin extends Screen {
Expand All @@ -35,6 +45,7 @@ public class EditWorldScreenMixin extends Screen {
@Unique private static final Text SELECT_DATAPACKS_TEXT = Text.translatable("dataPack.title");
@Unique @Nullable private ResourcePackManager packManager;
@Unique @Nullable private DataConfiguration dataConfiguration;
@Unique @Nullable private LevelSummary levelSummary;

protected EditWorldScreenMixin(Text title) {
super(title);
Expand All @@ -44,25 +55,33 @@ protected EditWorldScreenMixin(Text title) {
private void addButton(CallbackInfo info) {
this.layout.add(ButtonWidget.builder(SELECT_DATAPACKS_TEXT, (button) -> {
try {
this.dataConfiguration = this.storageSession.getLevelSummary(this.storageSession.readLevelProperties()).getLevelInfo().getDataConfiguration();
this.levelSummary = this.storageSession.getLevelSummary(this.storageSession.readLevelProperties());
} catch (IOException primaryException) {
LOGGER.warn("Failed to load world data from main level.dat. Attempting to load from fallback.", primaryException);

try {
this.dataConfiguration = this.storageSession.getLevelSummary(this.storageSession.readOldLevelProperties()).getLevelInfo().getDataConfiguration();
this.levelSummary = this.storageSession.getLevelSummary(this.storageSession.readOldLevelProperties());
} catch (IOException fallbackException) {
LOGGER.error("Failed to load world data from fallback level.dat.", fallbackException);
return;
}
}

this.dataConfiguration = this.levelSummary.getLevelInfo().getDataConfiguration();

if (this.packManager == null) {
this.packManager = VanillaDataPackProvider.createManager(DatapackInstallerClient.MAIN_PATH, client.getSymlinkFinder());
this.packManager.scanPacks();
}

this.packManager.setEnabledProfiles(this.dataConfiguration.dataPacks().getEnabled());

this.client.setScreen(new PackScreen(this.packManager, (resourcePackManager) -> {}, DatapackInstallerClient.MAIN_PATH, Text.translatable("dataPack.title")));
this.client.setScreen(new PackScreen(this.packManager, (resourcePackManager) -> {
List<String> enabledIds = ImmutableList.copyOf(resourcePackManager.getEnabledIds());
List<String> disabledIds = resourcePackManager.getIds().stream().filter((name) -> !enabledIds.contains(name)).collect(ImmutableList.toImmutableList());

this.dataConfiguration = new DataConfiguration(new DataPackSettings(enabledIds, disabledIds), this.dataConfiguration.enabledFeatures());
}, DatapackInstallerClient.MAIN_PATH, Text.translatable("dataPack.title")));
}).width(200).build());

// Reduce spacing so vanilla buttons remain on screen.
Expand Down

0 comments on commit 6670a9d

Please sign in to comment.