Sort Custom Presets to Front

This commit is contained in:
Frank 2022-05-20 01:59:30 +02:00
parent fdc068f6bb
commit 9f8409ebe0
7 changed files with 90 additions and 43 deletions

View file

@ -5,7 +5,6 @@ import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.tags.TagEntry;
import net.minecraft.tags.TagKey; import net.minecraft.tags.TagKey;
import net.minecraft.tags.TagLoader; import net.minecraft.tags.TagLoader;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
@ -17,13 +16,11 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Blocks;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.biomes.BiomeAPI; import org.betterx.bclib.api.biomes.BiomeAPI;
import org.betterx.bclib.mixin.common.DiggerItemAccessor; import org.betterx.bclib.mixin.common.DiggerItemAccessor;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
public class TagAPI { public class TagAPI {
@ -259,19 +256,6 @@ public class TagAPI {
return tagsMap; return tagsMap;
} }
/**
* Adds all {@code ids} to the {@code builder}.
*
* @param builder
* @param ids
* @return The Builder passed as {@code builder}.
*/
public static List<TagLoader.EntryWithSource> apply(List<TagLoader.EntryWithSource> builder,
Set<ResourceLocation> ids) {
ids.forEach(value -> builder.add(new TagLoader.EntryWithSource(TagEntry.element(value), BCLib.MOD_ID)));
return builder;
}
public static boolean isToolWithMineableTag(ItemStack stack, TagKey<Block> tag) { public static boolean isToolWithMineableTag(ItemStack stack, TagKey<Block> tag) {
if (stack.getItem() instanceof DiggerItemAccessor dig) { if (stack.getItem() instanceof DiggerItemAccessor dig) {

View file

@ -4,6 +4,7 @@ import net.minecraft.core.DefaultedRegistry;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagEntry;
import net.minecraft.tags.TagKey; import net.minecraft.tags.TagKey;
import net.minecraft.tags.TagLoader; import net.minecraft.tags.TagLoader;
import net.minecraft.tags.TagManager; import net.minecraft.tags.TagManager;
@ -196,6 +197,12 @@ public class TagType<T> {
if (Registry.BIOME_REGISTRY.equals(registryKey)) BiomeAPI._runTagAdders(); if (Registry.BIOME_REGISTRY.equals(registryKey)) BiomeAPI._runTagAdders();
//this.isFrozen = true; //this.isFrozen = true;
this.forEach((id, ids) -> TagAPI.apply(tagsMap.computeIfAbsent(id, key -> Lists.newArrayList()), ids)); this.forEach((id, ids) -> apply(tagsMap.computeIfAbsent(id, key -> Lists.newArrayList()), ids));
}
private static List<TagLoader.EntryWithSource> apply(List<TagLoader.EntryWithSource> builder,
Set<ResourceLocation> ids) {
ids.forEach(value -> builder.add(new TagLoader.EntryWithSource(TagEntry.element(value), BCLib.MOD_ID)));
return builder;
} }
} }

View file

@ -4,14 +4,20 @@ import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen; import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen;
import net.minecraft.client.gui.screens.worldselection.WorldGenSettingsComponent; import net.minecraft.client.gui.screens.worldselection.WorldGenSettingsComponent;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.DataPackConfig; import net.minecraft.world.level.DataPackConfig;
import net.minecraft.world.level.levelgen.presets.WorldPreset;
import org.betterx.bclib.api.biomes.BiomeAPI; import org.betterx.bclib.api.biomes.BiomeAPI;
import org.betterx.bclib.presets.WorldPresets;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Optional;
@Mixin(CreateWorldScreen.class) @Mixin(CreateWorldScreen.class)
public class CreateWorldScreenMixin { public class CreateWorldScreenMixin {
@Inject(method = "<init>", at = @At("TAIL")) @Inject(method = "<init>", at = @At("TAIL"))
@ -21,4 +27,9 @@ public class CreateWorldScreenMixin {
CallbackInfo ci) { CallbackInfo ci) {
BiomeAPI.initRegistry(worldGenSettingsComponent.registryHolder().registryOrThrow(Registry.BIOME_REGISTRY)); BiomeAPI.initRegistry(worldGenSettingsComponent.registryHolder().registryOrThrow(Registry.BIOME_REGISTRY));
} }
@ModifyArg(method = "openFresh", index = 1, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/worldselection/WorldGenSettingsComponent;<init>(Lnet/minecraft/client/gui/screens/worldselection/WorldCreationContext;Ljava/util/Optional;Ljava/util/OptionalLong;)V"))
private static Optional<ResourceKey<WorldPreset>> bcl_NewDefault(Optional<ResourceKey<WorldPreset>> preset) {
return Optional.of(WorldPresets.BCL_WORLD);
}
} }

View file

@ -0,0 +1,33 @@
package org.betterx.bclib.mixin.client;
import net.minecraft.client.gui.screens.worldselection.WorldGenSettingsComponent;
import net.minecraft.core.Holder;
import net.minecraft.world.level.levelgen.presets.WorldPreset;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@Mixin(WorldGenSettingsComponent.class)
public class WorldGenSettingsComponentMixin {
@ModifyArg(method = "init", index = 0, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/CycleButton$Builder;withValues(Ljava/util/List;Ljava/util/List;)Lnet/minecraft/client/gui/components/CycleButton$Builder;"))
public List<Holder<WorldPreset>> bcl_SortLists(List<Holder<WorldPreset>> list) {
final Predicate<Holder<WorldPreset>> vanilla = (p -> p.unwrapKey()
.orElseThrow()
.location()
.getNamespace()
.equals("minecraft"));
List<Holder<WorldPreset>> custom = list
.stream()
.filter(p -> !vanilla.test(p))
.collect(Collectors.toCollection(LinkedList::new));
custom.addAll(list.stream().filter(vanilla).toList());
return custom;
}
}

View file

@ -74,12 +74,12 @@ public abstract class WorldPresetsBootstrapMixin {
endSource, endSource,
this.endNoiseSettings) this.endNoiseSettings)
); );
WorldPreset preset = new WorldPreset(Map.of(LevelStem.OVERWORLD, WorldPreset preset = new org.betterx.bclib.presets.WorldPresets.SortableWorldPreset(Map.of(LevelStem.OVERWORLD,
overworldStem, overworldStem,
LevelStem.NETHER, LevelStem.NETHER,
bclNether, bclNether,
LevelStem.END, LevelStem.END,
bclEnd)); bclEnd), 0);
BuiltinRegistries.register(this.presets, org.betterx.bclib.presets.WorldPresets.BCL_WORLD, preset); BuiltinRegistries.register(this.presets, org.betterx.bclib.presets.WorldPresets.BCL_WORLD, preset);

View file

@ -6,6 +6,7 @@ import net.minecraft.data.BuiltinRegistries;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.WorldPresetTags; import net.minecraft.tags.WorldPresetTags;
import net.minecraft.world.level.dimension.LevelStem;
import net.minecraft.world.level.levelgen.presets.WorldPreset; import net.minecraft.world.level.levelgen.presets.WorldPreset;
import org.betterx.bclib.BCLib; import org.betterx.bclib.BCLib;
@ -13,9 +14,19 @@ import org.betterx.bclib.api.tag.TagAPI;
import org.betterx.bclib.api.tag.TagType; import org.betterx.bclib.api.tag.TagType;
import org.betterx.bclib.gui.modmenu.MainScreen; import org.betterx.bclib.gui.modmenu.MainScreen;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
public class WorldPresets { public class WorldPresets {
public static class SortableWorldPreset extends WorldPreset {
public final int sortOrder;
public SortableWorldPreset(Map<ResourceKey<LevelStem>, LevelStem> map, int sortOrder) {
super(map);
this.sortOrder = sortOrder;
}
}
public static TagType.Simple<WorldPreset> WORLD_PRESETS = public static TagType.Simple<WorldPreset> WORLD_PRESETS =
TagAPI.registerType(BuiltinRegistries.WORLD_PRESET, "tags/worldgen/world_preset"); TagAPI.registerType(BuiltinRegistries.WORLD_PRESET, "tags/worldgen/world_preset");

View file

@ -1,22 +1,23 @@
{ {
"required": true, "required": true,
"minVersion": "0.8", "minVersion": "0.8",
"package": "org.betterx.bclib.mixin.client", "package": "org.betterx.bclib.mixin.client",
"compatibilityLevel": "JAVA_17", "compatibilityLevel": "JAVA_17",
"client": [ "client": [
"ClientRecipeBookMixin", "AnvilScreenMixin",
"SignEditScreenMixin", "BlockMixin",
"ModelManagerMixin", "ClientRecipeBookMixin",
"TextureAtlasMixin", "CreateWorldScreenMixin",
"AnvilScreenMixin", "FogRendererMixin",
"FogRendererMixin", "GameMixin",
"ModelBakeryMixin", "MinecraftMixin",
"CreateWorldScreenMixin", "ModelBakeryMixin",
"MinecraftMixin", "ModelManagerMixin",
"BlockMixin", "SignEditScreenMixin",
"GameMixin" "TextureAtlasMixin",
], "WorldGenSettingsComponentMixin"
"injectors": { ],
"defaultRequire": 1 "injectors": {
} "defaultRequire": 1
}
} }