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

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

The newest version!
package com.yungnickyoung.minecraft.yungsapi.world.structure.condition;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.yungnickyoung.minecraft.yungsapi.world.structure.context.StructureContext;

import java.util.Optional;

/**
 * Condition for constraining y-value.
 */
public class AltitudeCondition extends StructureCondition {
    public static final MapCodec CODEC = RecordCodecBuilder.mapCodec((builder) -> builder
            .group(
                    Codec.DOUBLE.optionalFieldOf("bottom_cutoff_y").forGetter(conditon -> conditon.bottomCutoffY),
                    Codec.DOUBLE.optionalFieldOf("top_cutoff_y").forGetter(conditon -> conditon.topCutoffY))
            .apply(builder, AltitudeCondition::new));

    /**
     * The minimum allowed y-value.
     * If any part of the piece would be below this value, then the condition fails.
     */
    private Optional bottomCutoffY;

    /**
     * The maximum allowed y-value.
     * If any part of the piece would be above this value, then the condition fails.
     */
    private Optional topCutoffY;

    public AltitudeCondition(Optional bottomCutoffY, Optional topCutoffY) {
        this.bottomCutoffY = bottomCutoffY;
        this.topCutoffY = topCutoffY;
    }

    @Override
    public StructureConditionType type() {
        return StructureConditionType.ALTITUDE;
    }

    @Override
    public boolean passes(StructureContext ctx) {
        if (bottomCutoffY.isPresent() && ctx.pieceMinY() < bottomCutoffY.get()) return false;
        if (topCutoffY.isPresent() && ctx.pieceMaxY() > topCutoffY.get()) return false;
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy