All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yungnickyoung.minecraft.yungsapi.world.structure.condition.RandomChanceCondition Maven / Gradle / Ivy

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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy