Skip to content

Commit

Permalink
make variant selection actually random
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Dec 28, 2024
1 parent f0be11f commit d65d23e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/main/java/net/frozenblock/wilderwild/entity/Firefly.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ public static boolean isValidHomePos(@NotNull Level level, @NotNull BlockPos pos

@Nullable
@Override
public SpawnGroupData finalizeSpawn(@NotNull ServerLevelAccessor level, @NotNull DifficultyInstance difficulty, @NotNull MobSpawnType spawnType, @Nullable SpawnGroupData spawnData) {
public SpawnGroupData finalizeSpawn(
@NotNull ServerLevelAccessor level, @NotNull DifficultyInstance difficulty, @NotNull MobSpawnType spawnType, @Nullable SpawnGroupData spawnData
) {
this.natural = isFireflySpawnTypeNatural(spawnType);
this.hasHome = this.hasHome || !this.natural;
FireflyAi.rememberHome(this, this.blockPosition());
Expand All @@ -159,7 +161,7 @@ public SpawnGroupData finalizeSpawn(@NotNull ServerLevelAccessor level, @NotNull
if (spawnData instanceof FireflySpawnGroupData fireflySpawnGroupData) {
this.setColor(fireflySpawnGroupData.type.value());
} else {
Holder<FireflyColor> fireflyColorHolder = FireflyColors.getSpawnVariant(this.registryAccess(), holder);
Holder<FireflyColor> fireflyColorHolder = FireflyColors.getSpawnVariant(this.registryAccess(), holder, level.getRandom());
spawnData = new FireflySpawnGroupData(fireflyColorHolder);
this.setColor(fireflyColorHolder.value());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public SpawnGroupData finalizeSpawn(
jellyfishGroupData = jellyGroupData;
} else {
Holder<Biome> holder = level.getBiome(this.blockPosition());
Holder<JellyfishVariant> jellyfishVariantHolder = JellyfishVariants.getSpawnVariant(this.registryAccess(), holder);
Holder<JellyfishVariant> jellyfishVariantHolder = JellyfishVariants.getSpawnVariant(this.registryAccess(), holder, level.getRandom());
spawnData = jellyfishGroupData = new JellyfishGroupData(true, jellyfishVariantHolder);
this.setVariant(jellyfishVariantHolder.value());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import com.google.common.collect.ImmutableList;
import net.frozenblock.wilderwild.WWConstants;
import net.frozenblock.wilderwild.registry.WilderWildRegistries;
import net.minecraft.Util;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.data.worldgen.BootstrapContext;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.biome.Biome;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -85,14 +87,17 @@ private static void register(
bootstrapContext.register(resourceKey, new FireflyColor(textureLocation, HolderSet.empty(), modelPredicate));
}

public static Holder<FireflyColor> getSpawnVariant(@NotNull RegistryAccess registryAccess, Holder<Biome> holder) {
public static Holder<FireflyColor> getSpawnVariant(@NotNull RegistryAccess registryAccess, Holder<Biome> holder, RandomSource random) {
Registry<FireflyColor> registry = registryAccess.registryOrThrow(WilderWildRegistries.FIREFLY_COLOR);
return registry.holders()
List<Holder.Reference<FireflyColor>> colors = registry.holders()
.filter(reference -> (reference.value()).biomes().contains(holder))
.findAny()
.or(() -> registry.getHolder(DEFAULT))
.or(registry::getAny)
.orElseThrow();
.toList();

if (!colors.isEmpty()) {
return Util.getRandom(colors, random);
} else {
return registry.getHolder(DEFAULT).orElse(registry.getAny().orElseThrow());
}
}

@Contract(pure = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.frozenblock.wilderwild.registry.WilderWildRegistries;
import net.frozenblock.wilderwild.tag.WWBiomeTags;
import net.frozenblock.wilderwild.tag.WWItemTags;
import net.minecraft.Util;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
Expand All @@ -30,9 +31,11 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.biome.Biome;
import org.jetbrains.annotations.NotNull;
import java.util.List;

public class JellyfishVariants {
public static final ResourceKey<JellyfishVariant> BLUE = createKey("blue");
Expand Down Expand Up @@ -68,14 +71,17 @@ private static void register(
);
}

public static Holder<JellyfishVariant> getSpawnVariant(@NotNull RegistryAccess registryAccess, Holder<Biome> holder) {
public static Holder<JellyfishVariant> getSpawnVariant(@NotNull RegistryAccess registryAccess, Holder<Biome> holder, RandomSource random) {
Registry<JellyfishVariant> registry = registryAccess.registryOrThrow(WilderWildRegistries.JELLYFISH_VARIANT);
return registry.holders()
List<Holder.Reference<JellyfishVariant>> variants = registry.holders()
.filter(reference -> (reference.value()).biomes().contains(holder))
.findAny()
.or(() -> registry.getHolder(DEFAULT))
.or(registry::getAny)
.orElseThrow();
.toList();

if (!variants.isEmpty()) {
return Util.getRandom(variants, random);
} else {
return registry.getHolder(DEFAULT).orElse(registry.getAny().orElseThrow());
}
}

public static void bootstrap(BootstrapContext<JellyfishVariant> bootstrapContext) {
Expand Down

0 comments on commit d65d23e

Please sign in to comment.