com.yungnickyoung.minecraft.yungsapi.world.structure.condition.RandomChanceCondition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of YungsApi-1.21-Fabric Show documentation
Show all versions of YungsApi-1.21-Fabric Show documentation
A common API for YUNG's Minecraft mods
The newest version!
package com.yungnickyoung.minecraft.yungsapi.world.structure.condition;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.yungnickyoung.minecraft.yungsapi.YungsApiCommon;
import com.yungnickyoung.minecraft.yungsapi.world.structure.context.StructureContext;
import net.minecraft.class_5699;
import net.minecraft.class_5819;
/**
* Condition that randomly passes with the specified chance.
*/
public class RandomChanceCondition extends StructureCondition {
public static final MapCodec CODEC = RecordCodecBuilder.mapCodec((builder) -> builder
.group(
class_5699.field_34387.fieldOf("chance").forGetter(condition -> condition.chance))
.apply(builder, RandomChanceCondition::new));
public final float chance;
public RandomChanceCondition(float chance) {
this.chance = chance;
}
@Override
public StructureConditionType> type() {
return StructureConditionType.RANDOM_CHANCE;
}
@Override
public boolean passes(StructureContext ctx) {
// Extract args from context
class_5819 random = ctx.random();
// Abort if missing any args
if (random == null) {
YungsApiCommon.LOGGER.error("Missing required field 'random' for random_chance condition!");
return false;
}
return random.method_43057() < this.chance;
}
}