Small utility functions

This commit is contained in:
Jozufozu 2022-02-06 00:55:49 -08:00
parent 1d0e0eb6bc
commit ae5853442b
3 changed files with 38 additions and 1 deletions

View file

@ -10,6 +10,7 @@ import com.jozufozu.flywheel.util.Pair;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
public interface Material<D extends InstanceData> {
@ -26,6 +27,10 @@ public interface Material<D extends InstanceData> {
return model(partial, () -> new BlockModel(partial.get(), referenceState));
}
default Instancer<D> getModel(PartialModel partial) {
return model(partial, () -> new BlockModel(partial.get(), Blocks.AIR.defaultBlockState()));
}
default Instancer<D> getModel(PartialModel partial, BlockState referenceState, Direction dir) {
return getModel(partial, referenceState, dir, ModelUtil.rotateToFace(dir));
}

View file

@ -2,6 +2,10 @@ package com.jozufozu.flywheel.core.materials;
import com.jozufozu.flywheel.api.InstanceData;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.LightLayer;
/**
* An interface that implementors of {@link InstanceData} should also implement
* if they wish to make use of Flywheel's provided light update methods.
@ -25,5 +29,10 @@ public interface FlatLit<D extends InstanceData & FlatLit<D>> {
*/
D setSkyLight(int skyLight);
default D updateLight(BlockAndTintGetter level, BlockPos pos) {
return setBlockLight(level.getBrightness(LightLayer.BLOCK, pos))
.setSkyLight(level.getBrightness(LightLayer.SKY, pos));
}
int getPackedLight();
}

View file

@ -1,5 +1,7 @@
package com.jozufozu.flywheel.util.box;
import java.util.Collection;
import com.jozufozu.flywheel.util.RenderMath;
import net.minecraft.core.BlockPos;
@ -61,7 +63,28 @@ public class GridAlignedBB implements ImmutableBox {
return new GridAlignedBB(startX, 0, startZ, startX + 16, 256, startZ + 16);
}
public void fixMinMax() {
public static ImmutableBox containingAll(Collection<BlockPos> positions) {
if (positions.isEmpty()) {
return new GridAlignedBB();
}
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
int minZ = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int maxY = Integer.MIN_VALUE;
int maxZ = Integer.MIN_VALUE;
for (BlockPos pos : positions) {
minX = Math.min(minX, pos.getX());
minY = Math.min(minY, pos.getY());
minZ = Math.min(minZ, pos.getZ());
maxX = Math.max(maxX, pos.getX());
maxY = Math.max(maxY, pos.getY());
maxZ = Math.max(maxZ, pos.getZ());
}
return new GridAlignedBB(minX, minY, minZ, maxX, maxY, maxZ);
}
public void fixMinMax() {
int minX = Math.min(this.minX, this.maxX);
int minY = Math.min(this.minY, this.maxY);
int minZ = Math.min(this.minZ, this.maxZ);