com.yungnickyoung.minecraft.yungsapi.world.structure.condition.AltitudeCondition 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.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;
}
}