diff --git a/src/main/java/net/frozenblock/lib/worldgen/feature/api/FrozenLibFeatureUtils.java b/src/main/java/net/frozenblock/lib/worldgen/feature/api/FrozenLibFeatureUtils.java index 25b466b6..985e7750 100644 --- a/src/main/java/net/frozenblock/lib/worldgen/feature/api/FrozenLibFeatureUtils.java +++ b/src/main/java/net/frozenblock/lib/worldgen/feature/api/FrozenLibFeatureUtils.java @@ -17,7 +17,6 @@ package net.frozenblock.lib.worldgen.feature.api; -import java.util.Iterator; import lombok.experimental.UtilityClass; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -48,20 +47,19 @@ public static boolean isAirOrWaterNearby(WorldGenLevel level, @NotNull BlockPos pos.offset(searchDistance, searchDistance, searchDistance) ); for (BlockPos blockPos : poses) { - if (BlockPredicate.ONLY_IN_AIR_OR_WATER_PREDICATE.test(level, blockPos)) { - return true; - } + if (BlockPredicate.ONLY_IN_AIR_OR_WATER_PREDICATE.test(level, blockPos)) return true; } return false; } - public static boolean isWaterNearby(WorldGenLevel level, @NotNull BlockPos blockPos, int searchArea) { - Iterator poses = BlockPos.betweenClosed(blockPos.offset(-searchArea, -searchArea, -searchArea), blockPos.offset(searchArea, searchArea, searchArea)).iterator(); - BlockPos blockPos2; - do { - if (!poses.hasNext()) return false; - blockPos2 = poses.next(); - } while (!level.getBlockState(blockPos2).is(Blocks.WATER)); - return true; + public static boolean isWaterNearby(WorldGenLevel level, @NotNull BlockPos pos, int searchDistance) { + Iterable poses = BlockPos.betweenClosed( + pos.offset(-searchDistance, -searchDistance, -searchDistance), + pos.offset(searchDistance, searchDistance, searchDistance) + ); + for (BlockPos blockPos : poses) { + if (level.getBlockState(blockPos).is(Blocks.WATER)) return true; + } + return false; } } diff --git a/src/main/java/org/quiltmc/qsl/frozenblock/misc/datafixerupper/api/SimpleFixes.java b/src/main/java/org/quiltmc/qsl/frozenblock/misc/datafixerupper/api/SimpleFixes.java index 8a3e2bf2..c919adac 100644 --- a/src/main/java/org/quiltmc/qsl/frozenblock/misc/datafixerupper/api/SimpleFixes.java +++ b/src/main/java/org/quiltmc/qsl/frozenblock/misc/datafixerupper/api/SimpleFixes.java @@ -22,10 +22,13 @@ import com.mojang.datafixers.DataFix; import com.mojang.datafixers.DataFixerBuilder; import com.mojang.datafixers.schemas.Schema; +import java.util.List; import java.util.Map; import java.util.Objects; import static java.util.Objects.requireNonNull; import net.frozenblock.lib.datafix.api.BlockStateRenameFix; +import net.frozenblock.lib.math.api.AdvancedMath; +import net.minecraft.Util; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.datafix.DataFixers; import net.minecraft.util.datafix.fixes.BlockRenameFix; @@ -56,9 +59,13 @@ private SimpleFixes() { * @param schema the schema this fixer should be a part of * @see BlockRenameFix */ - public static void addBlockRenameFix(@NotNull DataFixerBuilder builder, @NotNull String name, - @NotNull ResourceLocation oldId, @NotNull ResourceLocation newId, - @NotNull Schema schema) { + public static void addBlockRenameFix( + @NotNull DataFixerBuilder builder, + @NotNull String name, + @NotNull ResourceLocation oldId, + @NotNull ResourceLocation newId, + @NotNull Schema schema + ) { requireNonNull(builder, "DataFixerBuilder cannot be null"); requireNonNull(name, "Fix name cannot be null"); requireNonNull(oldId, "Old identifier cannot be null"); @@ -70,6 +77,38 @@ public static void addBlockRenameFix(@NotNull DataFixerBuilder builder, @NotNull Objects.equals(NamespacedSchema.ensureNamespaced(inputName), oldIdStr) ? newIdStr : inputName)); } + /** + * Adds a block rename fix to the builder, choosing a random new id from a list. + * + * @param builder the builder + * @param name the fix's name + * @param oldId the block's old identifier + * @param newIds the new ids to randomly pick from + * @param schema the schema this fixer should be a part of + * @see BlockRenameFix + */ + public static void addRandomBlockRenameFix( + @NotNull DataFixerBuilder builder, + @NotNull String name, + @NotNull ResourceLocation oldId, + List newIds, + @NotNull Schema schema + ) { + Objects.requireNonNull(builder, "DataFixerBuilder cannot be null"); + Objects.requireNonNull(name, "Fix name cannot be null"); + Objects.requireNonNull(oldId, "Old identifier cannot be null"); + Objects.requireNonNull(newIds, "New identifiers cannot be null"); + Objects.requireNonNull(schema, "Schema cannot be null"); + String oldIdStr = oldId.toString(); + builder.addFixer( + BlockRenameFix.create( + schema, + name, + (inputName) -> Objects.equals(NamespacedSchema.ensureNamespaced(inputName), oldIdStr) ? Util.getRandom(newIds, AdvancedMath.random()).toString() : inputName + ) + ); + } + /** * Adds an entity rename fix to the builder, in case an entity's identifier is changed. * @@ -80,9 +119,13 @@ public static void addBlockRenameFix(@NotNull DataFixerBuilder builder, @NotNull * @param schema the schema this fix should be a part of * @see SimplestEntityRenameFix */ - public static void addEntityRenameFix(@NotNull DataFixerBuilder builder, @NotNull String name, - @NotNull ResourceLocation oldId, @NotNull ResourceLocation newId, - @NotNull Schema schema) { + public static void addEntityRenameFix( + @NotNull DataFixerBuilder builder, + @NotNull String name, + @NotNull ResourceLocation oldId, + @NotNull ResourceLocation newId, + @NotNull Schema schema + ) { requireNonNull(builder, "DataFixerBuilder cannot be null"); requireNonNull(name, "Fix name cannot be null"); requireNonNull(oldId, "Old identifier cannot be null"); @@ -108,9 +151,13 @@ protected String rename(String inputName) { * @param schema the schema this fix should be a part of * @see ItemRenameFix */ - public static void addItemRenameFix(@NotNull DataFixerBuilder builder, @NotNull String name, - @NotNull ResourceLocation oldId, @NotNull ResourceLocation newId, - @NotNull Schema schema) { + public static void addItemRenameFix( + @NotNull DataFixerBuilder builder, + @NotNull String name, + @NotNull ResourceLocation oldId, + @NotNull ResourceLocation newId, + @NotNull Schema schema + ) { requireNonNull(builder, "DataFixerBuilder cannot be null"); requireNonNull(name, "Fix name cannot be null"); requireNonNull(oldId, "Old identifier cannot be null"); @@ -122,6 +169,38 @@ public static void addItemRenameFix(@NotNull DataFixerBuilder builder, @NotNull Objects.equals(NamespacedSchema.ensureNamespaced(inputName), oldIdStr) ? newIdStr : inputName)); } + /** + * Adds an item rename fix to the builder, choosing a random new id from a list. + * + * @param builder the builder + * @param name the fix's name + * @param oldId the item's old identifier + * @param newIds the new ids to randomly pick from + * @param schema the schema this fix should be a part of + * @see ItemRenameFix + */ + private static void addRandomItemRenameFix( + @NotNull DataFixerBuilder builder, + @NotNull String name, + @NotNull ResourceLocation oldId, + List newIds, + @NotNull Schema schema + ) { + Objects.requireNonNull(builder, "DataFixerBuilder cannot be null"); + Objects.requireNonNull(name, "Fix name cannot be null"); + Objects.requireNonNull(oldId, "Old identifier cannot be null"); + Objects.requireNonNull(newIds, "New identifiers cannot be null"); + Objects.requireNonNull(schema, "Schema cannot be null"); + String oldIdStr = oldId.toString(); + builder.addFixer( + ItemRenameFix.create( + schema, + name, + (inputName) -> Objects.equals(NamespacedSchema.ensureNamespaced(inputName), oldIdStr) ? Util.getRandom(newIds, AdvancedMath.random()).toString() : inputName + ) + ); + } + /** * Adds a blockstate rename fix to the builder, in case a blockstate's name is changed. * @@ -134,10 +213,15 @@ public static void addItemRenameFix(@NotNull DataFixerBuilder builder, @NotNull * @param schema the schema this fixer should be a part of * @see BlockStateRenameFix */ - public static void addBlockStateRenameFix(@NotNull DataFixerBuilder builder, @NotNull String name, - @NotNull ResourceLocation blockId, @NotNull String oldState, - @NotNull String defaultValue, @NotNull String newState, - @NotNull Schema schema) { + public static void addBlockStateRenameFix( + @NotNull DataFixerBuilder builder, + @NotNull String name, + @NotNull ResourceLocation blockId, + @NotNull String oldState, + @NotNull String defaultValue, + @NotNull String newState, + @NotNull Schema schema + ) { requireNonNull(builder, "DataFixerBuilder cannot be null"); requireNonNull(name, "Fix name cannot be null"); requireNonNull(blockId, "Block Id cannot be null"); @@ -159,9 +243,13 @@ public static void addBlockStateRenameFix(@NotNull DataFixerBuilder builder, @No * @param schema the schema this fixer should be a part of * @see NamespacedTypeRenameFix */ - public static void addBiomeRenameFix(@NotNull DataFixerBuilder builder, @NotNull String name, - @NotNull Map changes, - @NotNull Schema schema) { + public static void addBiomeRenameFix( + @NotNull DataFixerBuilder builder, + @NotNull String name, + @NotNull Map changes, + @NotNull Schema schema + ) { requireNonNull(builder, "DataFixerBuilder cannot be null"); requireNonNull(name, "Fix name cannot be null"); requireNonNull(changes, "Changes cannot be null");