SDF, Noise, Blocks Helper, Spline Helper, Translation Helper

This commit is contained in:
paulevsGitch 2021-05-22 23:54:39 +03:00
parent 6a5584deae
commit 017d663af6
37 changed files with 4315 additions and 0 deletions

View file

@ -0,0 +1,171 @@
package ru.bclib.util;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import com.google.common.collect.Maps;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.Property;
public class BlocksHelper {
public static final BooleanProperty ROOTS = BooleanProperty.create("roots");
private static final Map<Block, Integer> COLOR_BY_BLOCK = Maps.newHashMap();
public static final int FLAG_UPDATE_BLOCK = 1;
public static final int FLAG_SEND_CLIENT_CHANGES = 2;
public static final int FLAG_NO_RERENDER = 4;
public static final int FORSE_RERENDER = 8;
public static final int FLAG_IGNORE_OBSERVERS = 16;
public static final int SET_SILENT = FLAG_UPDATE_BLOCK | FLAG_IGNORE_OBSERVERS | FLAG_SEND_CLIENT_CHANGES;
public static final int SET_OBSERV = FLAG_UPDATE_BLOCK | FLAG_SEND_CLIENT_CHANGES;
public static final Direction[] HORIZONTAL = makeHorizontal();
public static final Direction[] DIRECTIONS = Direction.values();
private static final MutableBlockPos POS = new MutableBlockPos();
protected static final BlockState AIR = Blocks.AIR.defaultBlockState();
protected static final BlockState WATER = Blocks.WATER.defaultBlockState();
private static final Vec3i[] OFFSETS = new Vec3i[] {
new Vec3i(-1, -1, -1), new Vec3i(-1, -1, 0), new Vec3i(-1, -1, 1),
new Vec3i(-1, 0, -1), new Vec3i(-1, 0, 0), new Vec3i(-1, 0, 1),
new Vec3i(-1, 1, -1), new Vec3i(-1, 1, 0), new Vec3i(-1, 1, 1),
new Vec3i(0, -1, -1), new Vec3i(0, -1, 0), new Vec3i(0, -1, 1),
new Vec3i(0, 0, -1), new Vec3i(0, 0, 0), new Vec3i(0, 0, 1),
new Vec3i(0, 1, -1), new Vec3i(0, 1, 0), new Vec3i(0, 1, 1),
new Vec3i(1, -1, -1), new Vec3i(1, -1, 0), new Vec3i(1, -1, 1),
new Vec3i(1, 0, -1), new Vec3i(1, 0, 0), new Vec3i(1, 0, 1),
new Vec3i(1, 1, -1), new Vec3i(1, 1, 0), new Vec3i(1, 1, 1)
};
public static void addBlockColor(Block block, int color) {
COLOR_BY_BLOCK.put(block, color);
}
public static int getBlockColor(Block block) {
return COLOR_BY_BLOCK.getOrDefault(block, 0xFF000000);
}
public static void setWithoutUpdate(LevelAccessor world, BlockPos pos, BlockState state) {
world.setBlock(pos, state, SET_SILENT);
}
public static void setWithoutUpdate(LevelAccessor world, BlockPos pos, Block block) {
world.setBlock(pos, block.defaultBlockState(), SET_SILENT);
}
public static void setWithUpdate(LevelAccessor world, BlockPos pos, BlockState state) {
world.setBlock(pos, state, SET_OBSERV);
}
public static void setWithUpdate(LevelAccessor world, BlockPos pos, Block block) {
world.setBlock(pos, block.defaultBlockState(), SET_OBSERV);
}
public static int upRay(LevelAccessor world, BlockPos pos, int maxDist) {
int length = 0;
for (int j = 1; j < maxDist && (world.isEmptyBlock(pos.above(j))); j++) {
length++;
}
return length;
}
public static int downRay(LevelAccessor world, BlockPos pos, int maxDist) {
int length = 0;
for (int j = 1; j < maxDist && (world.isEmptyBlock(pos.below(j))); j++) {
length++;
}
return length;
}
public static int downRayRep(LevelAccessor world, BlockPos pos, int maxDist) {
POS.set(pos);
for (int j = 1; j < maxDist && (world.getBlockState(POS)).getMaterial().isReplaceable(); j++) {
POS.setY(POS.getY() - 1);
}
return pos.getY() - POS.getY();
}
public static int raycastSqr(LevelAccessor world, BlockPos pos, int dx, int dy, int dz, int maxDist) {
POS.set(pos);
for (int j = 1; j < maxDist && (world.getBlockState(POS)).getMaterial().isReplaceable(); j++) {
POS.move(dx, dy, dz);
}
return (int) pos.distSqr(POS);
}
public static BlockState rotateHorizontal(BlockState state, Rotation rotation, Property<Direction> facing) {
return state.setValue(facing, rotation.rotate(state.getValue(facing)));
}
public static BlockState mirrorHorizontal(BlockState state, Mirror mirror, Property<Direction> facing) {
return state.rotate(mirror.getRotation(state.getValue(facing)));
}
public static int getLengthDown(LevelAccessor world, BlockPos pos, Block block) {
int count = 1;
while (world.getBlockState(pos.below(count)).getBlock() == block) {
count++;
}
return count;
}
public static void cover(LevelAccessor world, BlockPos center, Block ground, BlockState cover, int radius, Random random) {
HashSet<BlockPos> points = new HashSet<BlockPos>();
HashSet<BlockPos> points2 = new HashSet<BlockPos>();
if (world.getBlockState(center).getBlock() == ground) {
points.add(center);
points2.add(center);
for (int i = 0; i < radius; i++) {
Iterator<BlockPos> iterator = points.iterator();
while (iterator.hasNext()) {
BlockPos pos = iterator.next();
for (Vec3i offset : OFFSETS) {
if (random.nextBoolean()) {
BlockPos pos2 = pos.offset(offset);
if (random.nextBoolean() && world.getBlockState(pos2).getBlock() == ground
&& !points.contains(pos2))
points2.add(pos2);
}
}
}
points.addAll(points2);
points2.clear();
}
Iterator<BlockPos> iterator = points.iterator();
while (iterator.hasNext()) {
BlockPos pos = iterator.next();
BlocksHelper.setWithoutUpdate(world, pos, cover);
}
}
}
public static Direction[] makeHorizontal() {
return new Direction[] { Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST };
}
public static Direction randomHorizontal(Random random) {
return HORIZONTAL[random.nextInt(4)];
}
public static Direction randomDirection(Random random) {
return DIRECTIONS[random.nextInt(6)];
}
public static boolean isFluid(BlockState blockState) {
return !blockState.getFluidState().isEmpty();
}
}