From f4b143965f4151007b5c61841a470591c08a1549 Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 1 Jul 2022 11:28:14 +0200 Subject: [PATCH] More conversions --- .../generator/map/square/SquareBiomeMap.java | 4 +- .../bclib/api/v2/levelgen/LevelGenEvents.java | 2 +- .../bclib/api/v2/levelgen/LevelGenUtil.java | 1 + .../features/config/PillarFeatureConfig.java | 17 +- .../client/gui/screens/WorldSetupScreen.java | 2 - .../bclib/commands/CommandRegistry.java | 9 +- .../betterx/bclib/commands/DumpDatapack.java | 259 ------------------ .../bclib/registry/PresetsRegistry.java | 2 +- .../java/org/betterx/bclib/util/MHelper.java | 5 +- .../mixin/common/WorldGenPropertiesMixin.java | 2 +- .../together/tag/v3/CommonBlockTags.java | 14 +- .../worldPreset/TogetherWorldPreset.java | 2 +- 12 files changed, 33 insertions(+), 286 deletions(-) delete mode 100644 src/main/java/org/betterx/bclib/commands/DumpDatapack.java diff --git a/src/main/java/org/betterx/bclib/api/v2/generator/map/square/SquareBiomeMap.java b/src/main/java/org/betterx/bclib/api/v2/generator/map/square/SquareBiomeMap.java index a9191ede..1ce1d9e0 100644 --- a/src/main/java/org/betterx/bclib/api/v2/generator/map/square/SquareBiomeMap.java +++ b/src/main/java/org/betterx/bclib/api/v2/generator/map/square/SquareBiomeMap.java @@ -8,7 +8,7 @@ import org.betterx.bclib.noise.OpenSimplexNoise; import org.betterx.bclib.util.MHelper; import net.minecraft.world.level.ChunkPos; -import net.minecraft.world.level.levelgen.LegacyRandom; +import net.minecraft.world.level.levelgen.LegacyRandomSource; import net.minecraft.world.level.levelgen.WorldgenRandom; import com.google.common.collect.Maps; @@ -29,7 +29,7 @@ public class SquareBiomeMap implements BiomeMap { private TriConsumer processor; public SquareBiomeMap(long seed, int size, BiomePicker picker) { - random = new WorldgenRandom(new LegacyRandom(seed)); + random = new WorldgenRandom(new LegacyRandomSource(seed)); noiseX = new OpenSimplexNoise(random.nextLong()); noiseZ = new OpenSimplexNoise(random.nextLong()); this.sizeXZ = size; diff --git a/src/main/java/org/betterx/bclib/api/v2/levelgen/LevelGenEvents.java b/src/main/java/org/betterx/bclib/api/v2/levelgen/LevelGenEvents.java index d464ca5e..2eaad106 100644 --- a/src/main/java/org/betterx/bclib/api/v2/levelgen/LevelGenEvents.java +++ b/src/main/java/org/betterx/bclib/api/v2/levelgen/LevelGenEvents.java @@ -10,6 +10,7 @@ import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI; import org.betterx.bclib.registry.PresetsRegistry; import org.betterx.worlds.together.world.event.WorldEvents; import org.betterx.worlds.together.worldPreset.TogetherWorldPreset; +import org.betterx.worlds.together.worldPreset.WorldPreset; import net.minecraft.core.Holder; import net.minecraft.core.RegistryAccess; @@ -17,7 +18,6 @@ import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.chunk.ChunkGenerator; import net.minecraft.world.level.dimension.LevelStem; import net.minecraft.world.level.levelgen.WorldGenSettings; -import net.minecraft.world.level.levelgen.presets.WorldPreset; import net.minecraft.world.level.storage.LevelStorageSource; import java.util.Map; diff --git a/src/main/java/org/betterx/bclib/api/v2/levelgen/LevelGenUtil.java b/src/main/java/org/betterx/bclib/api/v2/levelgen/LevelGenUtil.java index f7d5e69d..30244772 100644 --- a/src/main/java/org/betterx/bclib/api/v2/levelgen/LevelGenUtil.java +++ b/src/main/java/org/betterx/bclib/api/v2/levelgen/LevelGenUtil.java @@ -11,6 +11,7 @@ import org.betterx.worlds.together.levelgen.WorldGenUtil; import org.betterx.worlds.together.util.ModUtil; import org.betterx.worlds.together.world.WorldConfig; import org.betterx.worlds.together.worldPreset.TogetherWorldPreset; +import org.betterx.worlds.together.worldPreset.WorldPreset; import com.mojang.serialization.Lifecycle; import net.minecraft.core.Holder; diff --git a/src/main/java/org/betterx/bclib/api/v3/levelgen/features/config/PillarFeatureConfig.java b/src/main/java/org/betterx/bclib/api/v3/levelgen/features/config/PillarFeatureConfig.java index a7019472..4e0bd4a9 100644 --- a/src/main/java/org/betterx/bclib/api/v3/levelgen/features/config/PillarFeatureConfig.java +++ b/src/main/java/org/betterx/bclib/api/v3/levelgen/features/config/PillarFeatureConfig.java @@ -14,7 +14,10 @@ import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate; import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration; import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider; +import java.util.Arrays; +import java.util.Map; import java.util.Random; +import java.util.stream.Collectors; public class PillarFeatureConfig implements FeatureConfiguration { @FunctionalInterface @@ -65,9 +68,13 @@ public class PillarFeatureConfig implements FeatureConfiguration { ); - public static final StringRepresentable.EnumCodec CODEC = StringRepresentable - .fromEnum(KnownTransformers::values); - + public static final Codec CODEC = StringRepresentable + .fromEnum(KnownTransformers::values, KnownTransformers::byName); + private static final Map BY_NAME = Arrays.stream(KnownTransformers.values()) + .collect(Collectors.toMap( + KnownTransformers::name, + a -> a + )); public final String name; public final StateTransform stateTransform; @@ -92,6 +99,10 @@ public class PillarFeatureConfig implements FeatureConfiguration { public String getSerializedName() { return this.name; } + + public static KnownTransformers byName(String string) { + return BY_NAME.get(string); + } } public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance diff --git a/src/main/java/org/betterx/bclib/client/gui/screens/WorldSetupScreen.java b/src/main/java/org/betterx/bclib/client/gui/screens/WorldSetupScreen.java index 29a988f2..4338b053 100644 --- a/src/main/java/org/betterx/bclib/client/gui/screens/WorldSetupScreen.java +++ b/src/main/java/org/betterx/bclib/client/gui/screens/WorldSetupScreen.java @@ -18,10 +18,8 @@ import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.chunk.ChunkGenerator; -import net.minecraft.world.level.dimension.BuiltinDimensionTypes; import net.minecraft.world.level.dimension.DimensionType; import net.minecraft.world.level.dimension.LevelStem; -import net.minecraft.world.level.levelgen.presets.WorldPresets; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; diff --git a/src/main/java/org/betterx/bclib/commands/CommandRegistry.java b/src/main/java/org/betterx/bclib/commands/CommandRegistry.java index 1ccd2962..ca79745e 100644 --- a/src/main/java/org/betterx/bclib/commands/CommandRegistry.java +++ b/src/main/java/org/betterx/bclib/commands/CommandRegistry.java @@ -7,7 +7,6 @@ import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.core.BlockPos; @@ -29,12 +28,12 @@ import java.util.Map; public class CommandRegistry { public static void register() { - CommandRegistrationCallback.EVENT.register(CommandRegistry::register); + CommandRegistrationCallback.EVENT.register((a, b, c) -> register(a, c)); } + private static void register( CommandDispatcher dispatcher, - CommandBuildContext commandBuildContext, Commands.CommandSelection commandSelection ) { dispatcher.register( @@ -44,10 +43,6 @@ public class CommandRegistry { .requires(source -> source.hasPermission(Commands.LEVEL_OWNERS)) .executes(ctx -> requestGC(ctx)) ) - .then(Commands.literal("dump_datapack") - .requires(source -> source.hasPermission(Commands.LEVEL_OWNERS)) - .executes(ctx -> DumpDatapack.dumpDatapack(ctx)) - ) .then(Commands.literal("debug_ore") .requires(source -> source.hasPermission(Commands.LEVEL_OWNERS)) .executes(ctx -> revealOre(ctx)) diff --git a/src/main/java/org/betterx/bclib/commands/DumpDatapack.java b/src/main/java/org/betterx/bclib/commands/DumpDatapack.java deleted file mode 100644 index bd8187f1..00000000 --- a/src/main/java/org/betterx/bclib/commands/DumpDatapack.java +++ /dev/null @@ -1,259 +0,0 @@ -package org.betterx.bclib.commands; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.mojang.brigadier.Command; -import com.mojang.brigadier.context.CommandContext; -import com.mojang.serialization.Codec; -import com.mojang.serialization.JsonOps; -import com.mojang.serialization.codecs.KeyDispatchCodec; -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.core.Holder; -import net.minecraft.core.RegistryAccess; -import net.minecraft.resources.RegistryOps; -import net.minecraft.tags.TagEntry; -import net.minecraft.tags.TagFile; -import net.minecraft.util.KeyDispatchDataCodec; -import net.minecraft.world.effect.MobEffect; -import net.minecraft.world.level.biome.BiomeSource; -import net.minecraft.world.level.dimension.LevelStem; -import net.minecraft.world.level.gameevent.GameEvent; -import net.minecraft.world.level.levelgen.WorldGenSettings; -import net.minecraft.world.level.levelgen.structure.Structure; -import net.minecraft.world.level.levelgen.structure.templatesystem.PosRuleTestType; -import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTestType; -import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorList; -import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorType; -import net.minecraft.world.level.material.Fluid; -import org.betterx.bclib.BCLib; -import org.betterx.bclib.blocks.BaseStairsBlock; - -import java.awt.Taskbar.Feature; -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; - -public class DumpDatapack { - static int dumpDatapack(CommandContext ctx) { - dumpDatapack(ctx.getSource().getLevel().registryAccess()); - return Command.SINGLE_SUCCESS; - } - - public static void dumpDatapack(RegistryAccess registryAccess) { - final RegistryOps registryOps = RegistryOps.create(JsonOps.INSTANCE, registryAccess); - GsonBuilder gsonBuilder = new GsonBuilder(); - gsonBuilder = gsonBuilder.setPrettyPrinting(); - Gson gson = gsonBuilder.create(); - registryAccess.registries().forEach(r -> dumpDatapack(r, registryOps, gson)); - } - - private static void dumpDatapack( - RegistryAccess.RegistryEntry registry, - RegistryOps registryOps, - Gson gson - ) { - File base = new File(System.getProperty("user.dir"), "bclib_datapack_dump"); - BCLib.LOGGER.info(registry.key().toString()); - // Tag Output - registry.value() - .getTagNames() - .map(tagKey -> registry.value().getTag(tagKey)) - .filter(tag -> tag.isPresent()) - .map(tag -> tag.get()) - .forEach(tag -> { - File f1 = new File(base, tag.key().location().getNamespace()); - f1 = new File(f1, "tags"); - f1 = new File(f1, registry.key().location().getPath()); - f1.mkdirs(); - f1 = new File(f1, tag.key().location().getPath() + ".json"); - - TagFile tf = new TagFile( - tag.stream() - .map(holder -> holder.unwrapKey()) - .filter(k -> k.isPresent()) - .map(k -> TagEntry.element(k.get().location())) - .toList(), - true - ); - var o = TagFile.CODEC - .encodeStart(registryOps, tf) - .result() - .orElse(new JsonObject()); - String content = gson.toJson(o); - try { - Files.writeString(f1.toPath(), content, StandardCharsets.UTF_8); - } catch (IOException e) { - BCLib.LOGGER.error(" ->> Unable to WRITE: " + e.getMessage()); - } - }); - - registry - .value() - .entrySet() - .stream() - .map(e -> e.getKey()).map(key -> registry.value().getHolder(key).get()) - .forEach(holder -> { - File f1 = new File(base, holder.unwrapKey().get().location().getNamespace()); - f1 = new File(f1, registry.key().location().getPath()); - f1.mkdirs(); - f1 = new File(f1, holder.unwrapKey().get().location().getPath() + ".json"); - f1.getParentFile().mkdirs(); - - Codec[] codec = {null}; - - BCLib.LOGGER.info(" - " + f1); - Object obj = holder; - - while (obj instanceof Holder) { - obj = ((Holder) obj).value(); - } - - if (obj instanceof BiomeSource || obj instanceof Feature) { - System.out.print(""); - } - - if (obj instanceof Structure s) { - codec[0] = s.type().codec(); - } else if (obj instanceof StructureProcessorList s) { - codec[0] = StructureProcessorType.LIST_OBJECT_CODEC; - } else if (obj instanceof GameEvent) { - return; - } else if (obj instanceof Fluid) { - return; - } else if (obj instanceof MobEffect) { - return; - } else if (obj instanceof BaseStairsBlock) { - return; - } else if (obj instanceof RuleTestType) { - codec[0] = registry.value().byNameCodec(); - } else if (obj instanceof PosRuleTestType) { - codec[0] = registry.value().byNameCodec(); - }else if (obj instanceof WorldGenSettings) { - codec[0] = registry.value().byNameCodec(); - }else if (obj instanceof LevelStem) { - codec[0] = registry.value().byNameCodec(); - } - - if (codec[0] == null) { - for (Method m : obj.getClass().getMethods()) { - if (!Modifier.isStatic(m.getModifiers())) { - m.setAccessible(true); - if (m.getParameterTypes().length == 0) { - if (Codec.class.isAssignableFrom(m.getReturnType())) { - try { - codec[0] = (Codec) m.invoke(obj); - BCLib.LOGGER.info(" Got Codec from " + m); - break; - } catch (Exception e) { - BCLib.LOGGER.error(" !!! Unable to get Codec from " + m); - } - } else if (KeyDispatchCodec.class.isAssignableFrom(m.getReturnType())) { - try { - codec[0] = ((KeyDispatchCodec) m.invoke(obj)).codec(); - BCLib.LOGGER.info(" Got Codec from " + m); - break; - } catch (Exception e) { - BCLib.LOGGER.error(" !!! Unable to get Codec from " + m); - } - } else if (KeyDispatchDataCodec.class.isAssignableFrom(m.getReturnType())) { - try { - codec[0] = ((KeyDispatchDataCodec) m.invoke(obj)).codec(); - BCLib.LOGGER.info(" Got Codec from " + m); - break; - } catch (Exception e) { - BCLib.LOGGER.error(" !!! Unable to get Codec from " + m); - } - } - } - } - } - } - - if (codec[0] == null) { - //Try to find DIRECT_CODEC field - for (Field f : obj.getClass().getFields()) { - if (Modifier.isStatic(f.getModifiers())) { - if ("DIRECT_CODEC".equals(f.getName())) { - f.setAccessible(true); - try { - codec[0] = (Codec) f.get(null); - BCLib.LOGGER.info(" Got Codec from " + f); - } catch (Exception e) { - BCLib.LOGGER.error(" !!! Unable to get Codec from " + f); - } - } - } - } - } - - //Try to find CODEC field - if (codec[0] == null) { - for (Field f : obj.getClass().getFields()) { - if (Modifier.isStatic(f.getModifiers())) { - if ("CODEC".equals(f.getName())) { - try { - f.setAccessible(true); - codec[0] = (Codec) f.get(null); - BCLib.LOGGER.info(" Got Codec from " + f); - } catch (Exception e) { - BCLib.LOGGER.error(" !!! Unable to get Codec from " + f); - } - } - } - } - } - - //Try to find any Codec field - if (codec[0] == null) { - for (Field f : obj.getClass().getFields()) { - if (Modifier.isStatic(f.getModifiers())) { - if (Codec.class.isAssignableFrom(f.getType())) { - f.setAccessible(true); - try { - codec[0] = (Codec) f.get(null); - BCLib.LOGGER.info(" Got Codec from " + f); - } catch (Exception e) { - BCLib.LOGGER.error(" !!! Unable to get Codec from " + f); - } - } - } - } - } - - if (codec[0]==null){ - codec[0] = registry.value().byNameCodec(); - } - - if (codec[0] == null) { - codec[0] = registry.value().byNameCodec(); - } - - - if (codec[0] != null) { - try { - var o = codec[0] - .encodeStart(registryOps, obj) - .result() - .orElse(new JsonObject()); - - String content = gson.toJson(o); - try { - Files.writeString(f1.toPath(), content, StandardCharsets.UTF_8); - } catch (IOException e) { - BCLib.LOGGER.error(" ->> Unable to WRITE: " + e.getMessage()); - } - } catch (Exception e) { - BCLib.LOGGER.error(" ->> Unable to encode: " + e.getMessage()); - } - } else { - BCLib.LOGGER.error(" !!! Could not determine Codec: " + obj.getClass()); - } - }); - } -} diff --git a/src/main/java/org/betterx/bclib/registry/PresetsRegistry.java b/src/main/java/org/betterx/bclib/registry/PresetsRegistry.java index 5fb40084..b556b4c0 100644 --- a/src/main/java/org/betterx/bclib/registry/PresetsRegistry.java +++ b/src/main/java/org/betterx/bclib/registry/PresetsRegistry.java @@ -6,11 +6,11 @@ import org.betterx.bclib.api.v2.generator.config.BCLNetherBiomeSourceConfig; import org.betterx.bclib.api.v2.levelgen.LevelGenUtil; import org.betterx.worlds.together.levelgen.WorldGenUtil; import org.betterx.worlds.together.worldPreset.TogetherWorldPreset; +import org.betterx.worlds.together.worldPreset.WorldPreset; import org.betterx.worlds.together.worldPreset.WorldPresets; import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.dimension.LevelStem; -import net.minecraft.world.level.levelgen.presets.WorldPreset; import java.util.Map; diff --git a/src/main/java/org/betterx/bclib/util/MHelper.java b/src/main/java/org/betterx/bclib/util/MHelper.java index 3f842432..cd8e658b 100644 --- a/src/main/java/org/betterx/bclib/util/MHelper.java +++ b/src/main/java/org/betterx/bclib/util/MHelper.java @@ -2,7 +2,8 @@ package org.betterx.bclib.util; import com.mojang.math.Vector3f; import net.minecraft.core.Vec3i; -import net.minecraft.world.level.levelgen.LegacyRandom; +import net.minecraft.world.level.levelgen.LegacyRandomSource; +import net.minecraft.world.level.levelgen.RandomSource; import java.util.Random; @@ -12,7 +13,7 @@ public class MHelper { public static final float PHI = (float) (Math.PI * (3 - Math.sqrt(5))); public static final float PI2 = (float) (Math.PI * 2); public static final Random RANDOM = new Random(); - public static final Random RANDOM_SOURCE = new LegacyRandom(RANDOM.nextLong()); + public static final RandomSource RANDOM_SOURCE = new LegacyRandomSource(RANDOM.nextLong()); public static int randRange(int min, int max, Random random) { return min + random.nextInt(max - min + 1); diff --git a/src/main/java/org/betterx/worlds/together/mixin/common/WorldGenPropertiesMixin.java b/src/main/java/org/betterx/worlds/together/mixin/common/WorldGenPropertiesMixin.java index 39ca9761..872345c2 100644 --- a/src/main/java/org/betterx/worlds/together/mixin/common/WorldGenPropertiesMixin.java +++ b/src/main/java/org/betterx/worlds/together/mixin/common/WorldGenPropertiesMixin.java @@ -16,7 +16,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(DedicatedServerProperties.WorldGenProperties.class) public class WorldGenPropertiesMixin { @Inject(method = "", at = @At(value = "TAIL")) - private static void bcl_create(String string, JsonObject jsonObject, boolean bl, String string2, CallbackInfo ci) { + private static void bcl_create(String seed, JsonObject jsonObject, boolean bl, String string2, CallbackInfo ci) { return seed; } diff --git a/src/main/java/org/betterx/worlds/together/tag/v3/CommonBlockTags.java b/src/main/java/org/betterx/worlds/together/tag/v3/CommonBlockTags.java index 9f2ee4aa..9a6064aa 100644 --- a/src/main/java/org/betterx/worlds/together/tag/v3/CommonBlockTags.java +++ b/src/main/java/org/betterx/worlds/together/tag/v3/CommonBlockTags.java @@ -35,8 +35,10 @@ public class CommonBlockTags { public static final TagKey TERRAIN = TagManager.BLOCKS.makeCommonTag("terrain"); public static final TagKey NETHER_TERRAIN = TagManager.BLOCKS.makeCommonTag("nether_terrain"); + public static final TagKey NETHER_CARVER_REPLACEABLES = TagManager.BLOCKS.makeCommonTag( + "nether_carver_replaceables"); + static void prepareTags() { - TagManager.BLOCKS.add(SCULK_LIKE, Blocks.SCULK); TagManager.BLOCKS.addOtherTags(DRAGON_IMMUNE, BlockTags.DRAGON_IMMUNE); TagManager.BLOCKS.add(END_STONES, Blocks.END_STONE); @@ -67,8 +69,7 @@ public class CommonBlockTags { Blocks.SAND, Blocks.RED_SAND, Blocks.GLOWSTONE, - Blocks.BONE_BLOCK, - Blocks.SCULK + Blocks.BONE_BLOCK ); TagManager.BLOCKS.addOtherTags( TERRAIN, @@ -102,14 +103,13 @@ public class CommonBlockTags { TagManager.BLOCKS.add(CommonBlockTags.CHEST, Blocks.CHEST); TagManager.BLOCKS.add( - BlockTags.NETHER_CARVER_REPLACEABLES, + NETHER_CARVER_REPLACEABLES, Blocks.BASALT, Blocks.RED_SAND, - Blocks.MAGMA_BLOCK, - Blocks.SCULK + Blocks.MAGMA_BLOCK ); TagManager.BLOCKS.addOtherTags( - BlockTags.NETHER_CARVER_REPLACEABLES, + NETHER_CARVER_REPLACEABLES, CommonBlockTags.NETHER_STONES, CommonBlockTags.NETHERRACK ); diff --git a/src/main/java/org/betterx/worlds/together/worldPreset/TogetherWorldPreset.java b/src/main/java/org/betterx/worlds/together/worldPreset/TogetherWorldPreset.java index 81be5818..2d5dc3fa 100644 --- a/src/main/java/org/betterx/worlds/together/worldPreset/TogetherWorldPreset.java +++ b/src/main/java/org/betterx/worlds/together/worldPreset/TogetherWorldPreset.java @@ -122,7 +122,7 @@ public class TogetherWorldPreset extends org.betterx.worlds.together.worldPreset public static Registry getDimensions(ResourceKey key) { RegistryAccess access = WorldBootstrap.getLastRegistryAccessOrElseBuiltin(); - var preset = access.registryOrThrow(Registry.WORLD_PRESET_REGISTRY).getHolder(key); + var preset = access.registryOrThrow(WorldPresets.WORLD_PRESET_REGISTRY).getHolder(key); if (preset.isEmpty()) return null; return preset .get()