Start migration

This commit is contained in:
Aleksey 2021-04-08 21:55:07 +03:00
parent 6630ce0cab
commit 47ed597358
491 changed files with 12045 additions and 11953 deletions

View file

@ -9,20 +9,20 @@ import java.util.Set;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.FallingBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.FallingBlock;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.Property;
import net.minecraft.tag.BlockTags;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3i;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.level.block.Rotation;
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 ru.betterend.blocks.BlueVineBlock;
import ru.betterend.blocks.basis.DoublePlantBlock;
import ru.betterend.blocks.basis.FurBlock;
@ -34,7 +34,7 @@ import ru.betterend.registry.EndTags;
public class BlocksHelper {
public static final BooleanProperty ROOTS = BooleanProperty.of("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;
@ -45,97 +45,92 @@ public class BlocksHelper {
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 Mutable POS = new Mutable();
protected static final BlockState AIR = Blocks.AIR.getDefaultState();
protected static final BlockState WATER = Blocks.WATER.getDefaultState();
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),
private static final MutableBlockPos POS = new MutableBlockPos();
protected static final BlockState AIR = Blocks.AIR.defaultBlockState();
protected static final BlockState WATER = Blocks.WATER.defaultBlockState();
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),
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) };
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(WorldAccess world, BlockPos pos, BlockState state) {
world.setBlockState(pos, state, SET_SILENT);
}
public static void setWithoutUpdate(WorldAccess world, BlockPos pos, Block block) {
world.setBlockState(pos, block.getDefaultState(), SET_SILENT);
}
public static void setWithUpdate(WorldAccess world, BlockPos pos, BlockState state) {
world.setBlockState(pos, state, SET_OBSERV);
}
public static void setWithUpdate(WorldAccess world, BlockPos pos, Block block) {
world.setBlockState(pos, block.getDefaultState(), SET_OBSERV);
public static void setWithoutUpdate(LevelAccessor world, BlockPos pos, BlockState state) {
world.setBlockAndUpdate(pos, state, SET_SILENT);
}
public static int upRay(WorldAccess world, BlockPos pos, int maxDist) {
public static void setWithoutUpdate(LevelAccessor world, BlockPos pos, Block block) {
world.setBlockAndUpdate(pos, block.defaultBlockState(), SET_SILENT);
}
public static void setWithUpdate(LevelAccessor world, BlockPos pos, BlockState state) {
world.setBlockAndUpdate(pos, state, SET_OBSERV);
}
public static void setWithUpdate(LevelAccessor world, BlockPos pos, Block block) {
world.setBlockAndUpdate(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.isAir(pos.up(j))); j++)
length++;
return length;
}
public static int downRay(WorldAccess world, BlockPos pos, int maxDist) {
public static int downRay(LevelAccessor world, BlockPos pos, int maxDist) {
int length = 0;
for (int j = 1; j < maxDist && (world.isAir(pos.down(j))); j++)
length++;
return length;
}
public static int downRayRep(WorldAccess world, BlockPos pos, int maxDist) {
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++)
{
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(WorldAccess world, BlockPos pos, int dx, int dy, int dz, int maxDist) {
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++)
{
for (int j = 1; j < maxDist && (world.getBlockState(POS)).getMaterial().isReplaceable(); j++) {
POS.move(dx, dy, dz);
}
return (int) pos.getSquaredDistance(POS);
}
public static BlockState rotateHorizontal(BlockState state, BlockRotation rotation, Property<Direction> facing) {
return state.with(facing, rotation.rotate(state.get(facing)));
public static BlockState rotateHorizontal(BlockState state, Rotation rotation, Property<Direction> facing) {
return state.with(facing, rotation.rotate(state.getValue(facing)));
}
public static BlockState mirrorHorizontal(BlockState state, BlockMirror mirror, Property<Direction> facing) {
return state.rotate(mirror.getRotation(state.get(facing)));
return state.rotate(mirror.getRotation(state.getValue(facing)));
}
public static int getLengthDown(WorldAccess world, BlockPos pos, Block block) {
public static int getLengthDown(LevelAccessor world, BlockPos pos, Block block) {
int count = 1;
while (world.getBlockState(pos.down(count)).getBlock() == block)
count++;
return count;
}
public static void cover(WorldAccess world, BlockPos center, Block ground, BlockState cover, int radius, Random random) {
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) {
@ -147,7 +142,7 @@ public class BlocksHelper {
BlockPos pos = iterator.next();
for (Vec3i offset : OFFSETS) {
if (random.nextBoolean()) {
BlockPos pos2 = pos.add(offset);
BlockPos pos2 = pos.offset(offset);
if (random.nextBoolean() && world.getBlockState(pos2).getBlock() == ground
&& !points.contains(pos2))
points2.add(pos2);
@ -164,8 +159,8 @@ public class BlocksHelper {
}
}
}
public static void fixBlocks(WorldAccess world, BlockPos start, BlockPos end) {
public static void fixBlocks(LevelAccessor world, BlockPos start, BlockPos end) {
BlockState state;
Set<BlockPos> doubleCheck = Sets.newHashSet();
for (int x = start.getX(); x <= end.getX(); x++) {
@ -175,7 +170,7 @@ public class BlocksHelper {
for (int y = start.getY(); y <= end.getY(); y++) {
POS.setY(y);
state = world.getBlockState(POS);
if (state.getBlock() instanceof FurBlock) {
doubleCheck.add(POS.toImmutable());
}
@ -207,19 +202,17 @@ public class BlocksHelper {
break;
}
}
}
else if (state.isOf(EndBlocks.SMARAGDANT_CRYSTAL)) {
} else if (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) {
POS.setY(POS.getY() - 1);
if (world.isAir(POS)) {
POS.setY(POS.getY() + 1);
while (state.isOf(EndBlocks.SMARAGDANT_CRYSTAL)) {
while (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) {
setWithoutUpdate(world, POS, AIR);
POS.setY(POS.getY() + 1);
state = world.getBlockState(POS);
}
}
}
else if (state.getBlock() instanceof StalactiteBlock) {
} else if (state.getBlock() instanceof StalactiteBlock) {
if (!state.canPlaceAt(world, POS)) {
if (world.getBlockState(POS.up()).getBlock() instanceof StalactiteBlock) {
while (state.getBlock() instanceof StalactiteBlock) {
@ -227,8 +220,7 @@ public class BlocksHelper {
POS.setY(POS.getY() + 1);
state = world.getBlockState(POS);
}
}
else {
} else {
while (state.getBlock() instanceof StalactiteBlock) {
setWithoutUpdate(world, POS, AIR);
POS.setY(POS.getY() - 1);
@ -236,15 +228,13 @@ public class BlocksHelper {
}
}
}
}
else if (state.isOf(EndBlocks.CAVE_PUMPKIN)) {
if (!world.getBlockState(POS.up()).isOf(EndBlocks.CAVE_PUMPKIN_SEED)) {
} else if (state.is(EndBlocks.CAVE_PUMPKIN)) {
if (!world.getBlockState(POS.up()).is(EndBlocks.CAVE_PUMPKIN_SEED)) {
setWithoutUpdate(world, POS, AIR);
}
}
else if (!state.canPlaceAt(world, POS)) {
} else if (!state.canPlaceAt(world, POS)) {
// Chorus
if (state.isOf(Blocks.CHORUS_PLANT)) {
if (state.is(Blocks.CHORUS_PLANT)) {
Set<BlockPos> ends = Sets.newHashSet();
Set<BlockPos> add = Sets.newHashSet();
ends.add(POS.toImmutable());
@ -253,15 +243,17 @@ public class BlocksHelper {
ends.forEach((pos) -> {
setWithoutUpdate(world, pos, AIR);
for (Direction dir : HORIZONTAL) {
BlockPos p = pos.offset(dir);
BlockPos p = pos.relative(dir);
BlockState st = world.getBlockState(p);
if ((st.isOf(Blocks.CHORUS_PLANT) || st.isOf(Blocks.CHORUS_FLOWER)) && !st.canPlaceAt(world, p)) {
if ((st.is(Blocks.CHORUS_PLANT) || st.is(Blocks.CHORUS_FLOWER))
&& !st.canPlaceAt(world, p)) {
add.add(p);
}
}
BlockPos p = pos.up();
BlockState st = world.getBlockState(p);
if ((st.isOf(Blocks.CHORUS_PLANT) || st.isOf(Blocks.CHORUS_FLOWER)) && !st.canPlaceAt(world, p)) {
if ((st.is(Blocks.CHORUS_PLANT) || st.is(Blocks.CHORUS_FLOWER))
&& !st.canPlaceAt(world, p)) {
add.add(p);
}
});
@ -286,14 +278,13 @@ public class BlocksHelper {
int ray = downRayRep(world, POS.toImmutable(), 64);
if (ray > 32) {
BlocksHelper.setWithoutUpdate(world, POS, Blocks.END_STONE.getDefaultState());
BlocksHelper.setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState());
if (world.getRandom().nextBoolean()) {
POS.setY(POS.getY() - 1);
state = world.getBlockState(POS);
BlocksHelper.setWithoutUpdate(world, POS, Blocks.END_STONE.getDefaultState());
BlocksHelper.setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState());
}
}
else {
} else {
POS.setY(y);
BlockState replacement = AIR;
for (Direction dir : HORIZONTAL) {
@ -312,7 +303,8 @@ public class BlocksHelper {
else {
// Blue Vine
if (state.getBlock() instanceof BlueVineBlock) {
while (state.isOf(EndBlocks.BLUE_VINE) || state.isOf(EndBlocks.BLUE_VINE_LANTERN) || state.isOf(EndBlocks.BLUE_VINE_FUR)) {
while (state.is(EndBlocks.BLUE_VINE) || state.is(EndBlocks.BLUE_VINE_LANTERN)
|| state.is(EndBlocks.BLUE_VINE_FUR)) {
BlocksHelper.setWithoutUpdate(world, POS, AIR);
POS.setY(POS.getY() + 1);
state = world.getBlockState(POS);
@ -333,34 +325,34 @@ public class BlocksHelper {
}
}
}
doubleCheck.forEach((pos) -> {
if (!world.getBlockState(pos).canPlaceAt(world, pos)) {
BlocksHelper.setWithoutUpdate(world, pos, AIR);
}
});
}
private static BlockState getAirOrFluid(BlockState state) {
return state.getFluidState().isEmpty() ? AIR : state.getFluidState().getBlockState();
}
public static boolean isEndNylium(Block block) {
return block.isIn(BlockTags.NYLIUM) && block.isIn(EndTags.END_GROUND);
}
public static boolean isEndNylium(BlockState state) {
return isEndNylium(state.getBlock());
}
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)];
}

View file

@ -7,68 +7,68 @@ import java.util.Random;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.block.Block;
import net.minecraft.util.Identifier;
import net.minecraft.world.level.block.Block;
import net.minecraft.resources.ResourceLocation;
import ru.betterend.registry.EndBiomes;
import ru.betterend.registry.EndBlocks;
import ru.betterend.world.biome.EndBiome;
public class BonemealUtil {
private static final Map<Identifier, Map<Block, GrassList>> GRASS_BIOMES = Maps.newHashMap();
private static final Map<ResourceLocation, Map<Block, GrassList>> GRASS_BIOMES = Maps.newHashMap();
private static final Map<Block, GrassList> GRASS_TYPES = Maps.newHashMap();
public static void init() {
addBonemealGrass(EndBlocks.END_MOSS, EndBlocks.CREEPING_MOSS);
addBonemealGrass(EndBlocks.END_MOSS, EndBlocks.UMBRELLA_MOSS);
addBonemealGrass(EndBlocks.END_MYCELIUM, EndBlocks.CREEPING_MOSS);
addBonemealGrass(EndBlocks.END_MYCELIUM, EndBlocks.UMBRELLA_MOSS);
addBonemealGrass(EndBlocks.CAVE_MOSS, EndBlocks.CAVE_GRASS);
addBonemealGrass(EndBlocks.CHORUS_NYLIUM, EndBlocks.CHORUS_GRASS);
addBonemealGrass(EndBlocks.CRYSTAL_MOSS, EndBlocks.CRYSTAL_GRASS);
addBonemealGrass(EndBlocks.SHADOW_GRASS, EndBlocks.SHADOW_PLANT);
addBonemealGrass(EndBlocks.PINK_MOSS, EndBlocks.BUSHY_GRASS);
addBonemealGrass(EndBlocks.AMBER_MOSS, EndBlocks.AMBER_GRASS);
addBonemealGrass(EndBlocks.JUNGLE_MOSS, EndBlocks.JUNGLE_GRASS);
addBonemealGrass(EndBlocks.JUNGLE_MOSS, EndBlocks.TWISTED_UMBRELLA_MOSS);
addBonemealGrass(EndBlocks.JUNGLE_MOSS, EndBlocks.SMALL_JELLYSHROOM, 0.1F);
addBonemealGrass(EndBiomes.GLOWING_GRASSLANDS, EndBlocks.END_MOSS, EndBlocks.BLOOMING_COOKSONIA);
addBonemealGrass(EndBiomes.GLOWING_GRASSLANDS, EndBlocks.END_MOSS, EndBlocks.VAIOLUSH_FERN);
addBonemealGrass(EndBiomes.GLOWING_GRASSLANDS, EndBlocks.END_MOSS, EndBlocks.FRACTURN);
addBonemealGrass(EndBiomes.GLOWING_GRASSLANDS, EndBlocks.END_MOSS, EndBlocks.SALTEAGO);
addBonemealGrass(EndBiomes.GLOWING_GRASSLANDS, EndBlocks.END_MOSS, EndBlocks.CREEPING_MOSS, 0.1F);
addBonemealGrass(EndBiomes.GLOWING_GRASSLANDS, EndBlocks.END_MOSS, EndBlocks.UMBRELLA_MOSS, 0.1F);
addBonemealGrass(EndBiomes.GLOWING_GRASSLANDS, EndBlocks.END_MOSS, EndBlocks.TWISTED_UMBRELLA_MOSS, 0.1F);
addBonemealGrass(EndBlocks.RUTISCUS, EndBlocks.ORANGO);
addBonemealGrass(EndBlocks.RUTISCUS, EndBlocks.AERIDIUM, 0.2F);
addBonemealGrass(EndBlocks.RUTISCUS, EndBlocks.LUTEBUS, 0.2F);
addBonemealGrass(EndBlocks.RUTISCUS, EndBlocks.LAMELLARIUM);
addBonemealGrass(EndBiomes.LANTERN_WOODS, EndBlocks.RUTISCUS, EndBlocks.AERIDIUM, 0.2F);
addBonemealGrass(EndBiomes.LANTERN_WOODS, EndBlocks.RUTISCUS, EndBlocks.LAMELLARIUM);
addBonemealGrass(EndBiomes.LANTERN_WOODS, EndBlocks.RUTISCUS, EndBlocks.BOLUX_MUSHROOM, 0.05F);
addBonemealGrass(EndBlocks.SANGNUM, EndBlocks.GLOBULAGUS);
addBonemealGrass(EndBlocks.SANGNUM, EndBlocks.CLAWFERN);
addBonemealGrass(EndBlocks.SANGNUM, EndBlocks.SMALL_AMARANITA_MUSHROOM, 0.1F);
addBonemealGrass(EndBlocks.MOSSY_DRAGON_BONE, EndBlocks.GLOBULAGUS);
addBonemealGrass(EndBlocks.MOSSY_DRAGON_BONE, EndBlocks.CLAWFERN);
addBonemealGrass(EndBlocks.MOSSY_DRAGON_BONE, EndBlocks.SMALL_AMARANITA_MUSHROOM, 0.1F);
addBonemealGrass(EndBlocks.MOSSY_OBSIDIAN, EndBlocks.GLOBULAGUS);
addBonemealGrass(EndBlocks.MOSSY_OBSIDIAN, EndBlocks.CLAWFERN);
addBonemealGrass(EndBlocks.MOSSY_OBSIDIAN, EndBlocks.SMALL_AMARANITA_MUSHROOM, 0.1F);
}
public static void addBonemealGrass(Block terrain, Block plant) {
addBonemealGrass(terrain, plant, 1F);
}
public static void addBonemealGrass(Block terrain, Block plant, float chance) {
GrassList list = GRASS_TYPES.get(terrain);
if (list == null) {
@ -77,11 +77,11 @@ public class BonemealUtil {
}
list.addGrass(plant, chance);
}
public static void addBonemealGrass(EndBiome biome, Block terrain, Block plant) {
addBonemealGrass(biome, terrain, plant, 1F);
}
public static void addBonemealGrass(EndBiome biome, Block terrain, Block plant, float chance) {
Map<Block, GrassList> map = GRASS_BIOMES.get(biome.getID());
if (map == null) {
@ -95,8 +95,8 @@ public class BonemealUtil {
}
list.addGrass(plant, chance);
}
public static Block getGrass(Identifier biomeID, Block terrain, Random random) {
public static Block getGrass(ResourceLocation biomeID, Block terrain, Random random) {
Map<Block, GrassList> map = GRASS_BIOMES.get(biomeID);
GrassList list = null;
if (map != null) {
@ -104,44 +104,43 @@ public class BonemealUtil {
if (list == null) {
list = GRASS_TYPES.get(terrain);
}
}
else {
} else {
list = GRASS_TYPES.get(terrain);
}
return list == null ? null : list.getGrass(random);
}
private static final class GrassInfo {
final Block grass;
float chance;
public GrassInfo(Block grass, float chance) {
this.grass = grass;
this.chance = chance;
}
public float addChance(float chance) {
this.chance += chance;
return this.chance;
}
}
private static final class GrassList {
final List<GrassInfo> list = Lists.newArrayList();
float maxChance = 0;
public void addGrass(Block grass, float chance) {
GrassInfo info = new GrassInfo(grass, chance);
maxChance = info.addChance(maxChance);
list.add(info);
}
public Block getGrass(Random random) {
if (maxChance == 0 || list.isEmpty()) {
return null;
}
float chance = random.nextFloat() * maxChance;
for (GrassInfo info: list) {
for (GrassInfo info : list) {
if (chance <= info.chance) {
return info.grass;
}

View file

@ -12,47 +12,44 @@ import net.fabricmc.api.Environment;
import net.fabricmc.fabric.impl.client.indigo.renderer.helper.ColorHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.registry.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.core.Registry;
import ru.betterend.BetterEnd;
@Environment(EnvType.CLIENT)
public class ColorUtil {
private static float[] floatBuffer = new float[4];
public static int[] toIntArray(int color) {
return new int[] {
(color >> 24) & 255,
(color >> 16) & 255,
(color >> 8) & 255,
color & 255
};
return new int[] { (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, color & 255 };
}
public static float[] toFloatArray(int color) {
floatBuffer[0] = ((color >> 16 & 255) / 255.0F);
floatBuffer[1] = ((color >> 8 & 255) / 255.0F);
floatBuffer[2] = ((color & 255) / 255.0F);
floatBuffer[3] = ((color >> 24 & 255) / 255.0F);
return floatBuffer;
}
public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals) {
float hue, saturation, brightness;
if (hsbvals == null) {
hsbvals = floatBuffer;
}
int cmax = (r > g) ? r : g;
if (b > cmax) cmax = b;
if (b > cmax)
cmax = b;
int cmin = (r < g) ? r : g;
if (b < cmin) cmin = b;
if (b < cmin)
cmin = b;
brightness = ((float) cmax) / 255.0F;
if (cmax != 0)
@ -80,14 +77,14 @@ public class ColorUtil {
hsbvals[2] = brightness;
return hsbvals;
}
public static int HSBtoRGB(float hue, float saturation, float brightness) {
int r = 0, g = 0, b = 0;
if (saturation == 0) {
r = g = b = (int) (brightness * 255.0F + 0.5F);
} else {
float h = (hue - (float)Math.floor(hue)) * 6.0F;
float f = h - (float)java.lang.Math.floor(h);
float h = (hue - (float) Math.floor(hue)) * 6.0F;
float f = h - (float) java.lang.Math.floor(h);
float p = brightness * (1.0F - saturation);
float q = brightness * (1.0F - saturation * f);
float t = brightness * (1.0F - (saturation * (1.0F - f)));
@ -126,41 +123,43 @@ public class ColorUtil {
}
return 0xFF000000 | (r << 16) | (g << 8) | (b << 0);
}
public static int parseHex(String hexColor) {
int len = hexColor.length();
if (len < 6 || len > 8 || len % 2 > 0) {
return -1;
}
int color, shift;
if(len == 6) {
color = 0xFF000000; shift = 16;
if (len == 6) {
color = 0xFF000000;
shift = 16;
} else {
color = 0; shift = 24;
color = 0;
shift = 24;
}
try {
String[] splited = hexColor.split("(?<=\\G.{2})");
for (String digit : splited) {
color |= Integer.valueOf(digit, 16) << shift;
shift -= 8;
}
} catch(NumberFormatException ex) {
} catch (NumberFormatException ex) {
BetterEnd.LOGGER.catching(ex);
return -1;
}
return color;
}
public static int toABGR(int color) {
int r = (color >> 16) & 255;
int g = (color >> 8) & 255;
int b = color & 255;
return 0xFF000000 | b << 16 | g << 8 | r;
}
public static int ABGRtoARGB(int color) {
int a = (color >> 24) & 255;
int b = (color >> 16) & 255;
@ -168,18 +167,18 @@ public class ColorUtil {
int r = color & 255;
return a << 24 | r << 16 | g << 8 | b;
}
public static int colorBrigtness(int color, float val) {
RGBtoHSB((color >> 16) & 255, (color >> 8) & 255, color & 255, floatBuffer);
floatBuffer[2] += val / 10.0F;
floatBuffer[2] = MathHelper.clamp(floatBuffer[2], 0.0F, 1.0F);
floatBuffer[2] = Mth.clamp(floatBuffer[2], 0.0F, 1.0F);
return HSBtoRGB(floatBuffer[0], floatBuffer[1], floatBuffer[2]);
}
public static int applyTint(int color, int tint) {
return colorBrigtness(ColorHelper.multiplyColor(color, tint), 1.5F);
}
public static int colorDistance(int color1, int color2) {
int r1 = (color1 >> 16) & 255;
int g1 = (color1 >> 8) & 255;
@ -189,20 +188,21 @@ public class ColorUtil {
int b2 = color2 & 255;
return MHelper.pow2(r1 - r2) + MHelper.pow2(g1 - g2) + MHelper.pow2(b1 - b2);
}
private static Map<Identifier, Integer> colorPalette = Maps.newHashMap();
private static Map<ResourceLocation, Integer> colorPalette = Maps.newHashMap();
public static int extractColor(Item item) {
Identifier id = Registry.ITEM.getId(item);
if (id.equals(Registry.ITEM.getDefaultId())) return -1;
ResourceLocation id = Registry.ITEM.getId(item);
if (id.equals(Registry.ITEM.getDefaultId()))
return -1;
if (colorPalette.containsKey(id)) {
return colorPalette.get(id);
}
Identifier texture;
ResourceLocation texture;
if (item instanceof BlockItem) {
texture = new Identifier(id.getNamespace(), "textures/block/" + id.getPath() + ".png");
texture = new ResourceLocation(id.getNamespace(), "textures/block/" + id.getPath() + ".png");
} else {
texture = new Identifier(id.getNamespace(), "textures/item/" + id.getPath() + ".png");
texture = new ResourceLocation(id.getNamespace(), "textures/item/" + id.getPath() + ".png");
}
NativeImage image = loadImage(texture, 16, 16);
List<Integer> colors = new ArrayList<>();
@ -215,27 +215,28 @@ public class ColorUtil {
}
}
image.close();
if (colors.size() == 0) return -1;
if (colors.size() == 0)
return -1;
ColorExtractor extractor = new ColorExtractor(colors);
int color = extractor.analize();
colorPalette.put(id, color);
return color;
}
public static NativeImage loadImage(Identifier image, int w, int h) {
public static NativeImage loadImage(ResourceLocation image, int w, int h) {
MinecraftClient minecraft = MinecraftClient.getInstance();
ResourceManager resourceManager = minecraft.getResourceManager();
if (resourceManager.containsResource(image)) {
try (Resource resource = resourceManager.getResource(image)) {
return NativeImage.read(resource.getInputStream());
return NativeImage.read(resource.getInputStream());
} catch (IOException e) {
BetterEnd.LOGGER.warning("Can't load texture image: {}. Will be created empty image.", image);
BetterEnd.LOGGER.warning("Cause: {}.", e.getMessage());
}
}
}
return new NativeImage(w, h, false);
}
}

View file

@ -8,7 +8,7 @@ import java.util.function.Supplier;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.minecraft.util.registry.Registry;
import net.minecraft.core.Registry;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.ConfiguredStructureFeature;
@ -23,9 +23,11 @@ public class FeaturesHelper {
biomeRegistry.forEach((biome) -> {
if (biome.getCategory() == Biome.Category.THEEND && !INJECTED.contains(biome)) {
GenerationSettingsAccessor accessor = (GenerationSettingsAccessor) biome.getGenerationSettings();
List<Supplier<ConfiguredStructureFeature<?, ?>>> structures = Lists.newArrayList(accessor.beGetStructures());
List<Supplier<ConfiguredStructureFeature<?, ?>>> structures = Lists
.newArrayList(accessor.beGetStructures());
List<List<Supplier<ConfiguredFeature<?, ?>>>> preFeatures = accessor.beGetFeatures();
List<List<Supplier<ConfiguredFeature<?, ?>>>> features = new ArrayList<List<Supplier<ConfiguredFeature<?, ?>>>>(preFeatures.size());
List<List<Supplier<ConfiguredFeature<?, ?>>>> features = new ArrayList<List<Supplier<ConfiguredFeature<?, ?>>>>(
preFeatures.size());
preFeatures.forEach((list) -> {
features.add(Lists.newArrayList(list));
});
@ -37,6 +39,6 @@ public class FeaturesHelper {
accessor.beSetStructures(structures);
INJECTED.add(biome);
}
});
});
}
}

View file

@ -5,15 +5,15 @@ import org.jetbrains.annotations.Nullable;
import com.google.gson.JsonObject;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry;
import net.minecraft.core.Registry;
import ru.betterend.BetterEnd;
public class ItemUtil {
public static String toStackString(@NotNull ItemStack stack) {
try {
if (stack == null) {
@ -26,7 +26,7 @@ public class ItemUtil {
}
return "";
}
@Nullable
public static ItemStack fromStackString(String stackString) {
if (stackString == null || stackString.equals("")) {
@ -34,15 +34,16 @@ public class ItemUtil {
}
try {
String[] parts = stackString.split(":");
if (parts.length < 2) return null;
if (parts.length < 2)
return null;
if (parts.length == 2) {
Identifier itemId = new Identifier(stackString);
ResourceLocation itemId = new ResourceLocation(stackString);
Item item = Registry.ITEM.getOrEmpty(itemId).orElseThrow(() -> {
return new IllegalStateException("Output item " + itemId + " does not exists!");
});
return new ItemStack(item);
}
Identifier itemId = new Identifier(parts[0], parts[1]);
ResourceLocation itemId = new ResourceLocation(parts[0], parts[1]);
Item item = Registry.ITEM.getOrEmpty(itemId).orElseThrow(() -> {
return new IllegalStateException("Output item " + itemId + " does not exists!");
});
@ -52,14 +53,14 @@ public class ItemUtil {
}
return null;
}
@Nullable
public static ItemStack fromJsonRecipe(JsonObject recipe) {
try {
if (!recipe.has("item")) {
throw new IllegalStateException("Invalid JsonObject. Entry 'item' does not exists!");
}
Identifier itemId = new Identifier(JsonHelper.getString(recipe, "item"));
ResourceLocation itemId = new ResourceLocation(JsonHelper.getString(recipe, "item"));
Item item = Registry.ITEM.getOrEmpty(itemId).orElseThrow(() -> {
return new IllegalStateException("Output item " + itemId + " does not exists!");
});

View file

@ -2,15 +2,15 @@ package ru.betterend.util;
import net.fabricmc.fabric.api.loot.v1.FabricLootPoolBuilder;
import net.fabricmc.fabric.api.loot.v1.event.LootTableLoadingCallback;
import net.minecraft.item.Items;
import net.minecraft.world.item.Items;
import net.minecraft.loot.UniformLootTableRange;
import net.minecraft.loot.condition.RandomChanceLootCondition;
import net.minecraft.loot.entry.ItemEntry;
import net.minecraft.util.Identifier;
import net.minecraft.resources.ResourceLocation;
import ru.betterend.registry.EndItems;
public class LootTableUtil {
private static final Identifier END_CITY_TREASURE_ID = new Identifier("chests/end_city_treasure");
private static final ResourceLocation END_CITY_TREASURE_ID = new ResourceLocation("chests/end_city_treasure");
public static void init() {
LootTableLoadingCallback.EVENT.register((resourceManager, lootManager, id, supplier, setter) -> {

View file

@ -1,18 +1,18 @@
package ru.betterend.util;
import net.minecraft.block.Block;
import net.minecraft.item.ItemConvertible;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.item.ItemConvertible;
import net.minecraft.core.Registry;
public class RecipeHelper {
public static boolean exists(ItemConvertible item) {
if (item instanceof Block) {
return Registry.BLOCK.getId((Block) item) != Registry.BLOCK.getDefaultId();
return Registry.BLOCK.getKey((Block) item) != Registry.BLOCK.getDefaultId();
} else {
return Registry.ITEM.getId(item.asItem()) != Registry.ITEM.getDefaultId();
}
}
public static boolean exists(ItemConvertible... items) {
for (ItemConvertible item : items) {
if (!exists(item)) {

View file

@ -1,21 +1,21 @@
package ru.betterend.util;
import net.fabricmc.fabric.mixin.object.builder.SpawnRestrictionAccessor;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnRestriction.Location;
import net.minecraft.entity.SpawnRestriction.SpawnPredicate;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.SpawnRestriction.Location;
import net.minecraft.world.entity.SpawnRestriction.SpawnPredicate;
import net.minecraft.world.entity.mob.MobEntity;
import net.minecraft.world.Heightmap.Type;
public class SpawnHelper {
public static <T extends MobEntity> void restrictionAir(EntityType<T> entity, SpawnPredicate<T> predicate) {
SpawnRestrictionAccessor.callRegister(entity, Location.NO_RESTRICTIONS, Type.MOTION_BLOCKING, predicate);
}
public static <T extends MobEntity> void restrictionLand(EntityType<T> entity, SpawnPredicate<T> predicate) {
SpawnRestrictionAccessor.callRegister(entity, Location.ON_GROUND, Type.MOTION_BLOCKING, predicate);
}
public static <T extends MobEntity> void restrictionWater(EntityType<T> entity, SpawnPredicate<T> predicate) {
SpawnRestrictionAccessor.callRegister(entity, Location.IN_WATER, Type.MOTION_BLOCKING, predicate);
}

View file

@ -7,11 +7,11 @@ import java.util.function.Function;
import com.google.common.collect.Lists;
import net.minecraft.block.BlockState;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.util.math.MathHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.util.Mth;
import net.minecraft.world.StructureWorldAccess;
import ru.betterend.util.sdf.SDF;
import ru.betterend.util.sdf.operator.SDFUnion;
@ -24,15 +24,15 @@ public class SplineHelper {
int count = points - 1;
for (int i = 1; i < count; i++) {
float delta = (float) i / (float) count;
float x = MathHelper.lerp(delta, x1, x2);
float y = MathHelper.lerp(delta, y1, y2);
float z = MathHelper.lerp(delta, z1, z2);
float x = Mth.lerp(delta, x1, x2);
float y = Mth.lerp(delta, y1, y2);
float z = Mth.lerp(delta, z1, z2);
spline.add(new Vector3f(x, y, z));
}
spline.add(new Vector3f(x2, y2, z2));
return spline;
}
public static List<Vector3f> smoothSpline(List<Vector3f> spline, int segmentPoints) {
List<Vector3f> result = Lists.newArrayList();
Vector3f start = spline.get(0);
@ -40,7 +40,7 @@ public class SplineHelper {
Vector3f end = spline.get(i);
for (int j = 0; j < segmentPoints; j++) {
float delta = (float) j / segmentPoints;
delta = 0.5F - 0.5F * MathHelper.cos(delta * 3.14159F);
delta = 0.5F - 0.5F * Mth.cos(delta * 3.14159F);
result.add(lerp(start, end, delta));
}
start = end;
@ -48,14 +48,14 @@ public class SplineHelper {
result.add(start);
return result;
}
private static Vector3f lerp(Vector3f start, Vector3f end, float delta) {
float x = MathHelper.lerp(delta, start.getX(), end.getX());
float y = MathHelper.lerp(delta, start.getY(), end.getY());
float z = MathHelper.lerp(delta, start.getZ(), end.getZ());
float x = Mth.lerp(delta, start.getX(), end.getX());
float y = Mth.lerp(delta, start.getY(), end.getY());
float z = Mth.lerp(delta, start.getZ(), end.getZ());
return new Vector3f(x, y, z);
}
public static void offsetParts(List<Vector3f> spline, Random random, float dx, float dy, float dz) {
int count = spline.size();
for (int i = 1; i < count; i++) {
@ -66,7 +66,7 @@ public class SplineHelper {
pos.set(x, y, z);
}
}
public static void powerOffset(List<Vector3f> spline, float distance, float power) {
int count = spline.size();
float max = count + 1;
@ -77,8 +77,9 @@ public class SplineHelper {
pos.set(pos.getX(), y, pos.getZ());
}
}
public static SDF buildSDF(List<Vector3f> spline, float radius1, float radius2, Function<BlockPos, BlockState> placerFunction) {
public static SDF buildSDF(List<Vector3f> spline, float radius1, float radius2,
Function<BlockPos, BlockState> placerFunction) {
int count = spline.size();
float max = count - 2;
SDF result = null;
@ -86,18 +87,17 @@ public class SplineHelper {
for (int i = 1; i < count; i++) {
Vector3f pos = spline.get(i);
float delta = (float) (i - 1) / max;
SDF line = new SDFLine()
.setRadius(MathHelper.lerp(delta, radius1, radius2))
.setStart(start.getX(), start.getY(), start.getZ())
.setEnd(pos.getX(), pos.getY(), pos.getZ())
SDF line = new SDFLine().setRadius(Mth.lerp(delta, radius1, radius2))
.setStart(start.getX(), start.getY(), start.getZ()).setEnd(pos.getX(), pos.getY(), pos.getZ())
.setBlock(placerFunction);
result = result == null ? line : new SDFUnion().setSourceA(result).setSourceB(line);
start = pos;
}
return result;
}
public static SDF buildSDF(List<Vector3f> spline, Function<Float, Float> radiusFunction, Function<BlockPos, BlockState> placerFunction) {
public static SDF buildSDF(List<Vector3f> spline, Function<Float, Float> radiusFunction,
Function<BlockPos, BlockState> placerFunction) {
int count = spline.size();
float max = count - 2;
SDF result = null;
@ -105,18 +105,17 @@ public class SplineHelper {
for (int i = 1; i < count; i++) {
Vector3f pos = spline.get(i);
float delta = (float) (i - 1) / max;
SDF line = new SDFLine()
.setRadius(radiusFunction.apply(delta))
.setStart(start.getX(), start.getY(), start.getZ())
.setEnd(pos.getX(), pos.getY(), pos.getZ())
SDF line = new SDFLine().setRadius(radiusFunction.apply(delta))
.setStart(start.getX(), start.getY(), start.getZ()).setEnd(pos.getX(), pos.getY(), pos.getZ())
.setBlock(placerFunction);
result = result == null ? line : new SDFUnion().setSourceA(result).setSourceB(line);
start = pos;
}
return result;
}
public static boolean fillSpline(List<Vector3f> spline, StructureWorldAccess world, BlockState state, BlockPos pos, Function<BlockState, Boolean> replace) {
public static boolean fillSpline(List<Vector3f> spline, StructureWorldAccess world, BlockState state, BlockPos pos,
Function<BlockState, Boolean> replace) {
Vector3f startPos = spline.get(0);
for (int i = 1; i < spline.size(); i++) {
Vector3f endPos = spline.get(i);
@ -125,11 +124,12 @@ public class SplineHelper {
}
startPos = endPos;
}
return true;
}
public static void fillSplineForce(List<Vector3f> spline, StructureWorldAccess world, BlockState state, BlockPos pos, Function<BlockState, Boolean> replace) {
public static void fillSplineForce(List<Vector3f> spline, StructureWorldAccess world, BlockState state,
BlockPos pos, Function<BlockState, Boolean> replace) {
Vector3f startPos = spline.get(0);
for (int i = 1; i < spline.size(); i++) {
Vector3f endPos = spline.get(i);
@ -137,8 +137,9 @@ public class SplineHelper {
startPos = endPos;
}
}
public static boolean fillLine(Vector3f start, Vector3f end, StructureWorldAccess world, BlockState state, BlockPos pos, Function<BlockState, Boolean> replace) {
public static boolean fillLine(Vector3f start, Vector3f end, StructureWorldAccess world, BlockState state,
BlockPos pos, Function<BlockState, Boolean> replace) {
float dx = end.getX() - start.getX();
float dy = end.getY() - start.getY();
float dz = end.getZ() - start.getZ();
@ -151,9 +152,9 @@ public class SplineHelper {
float y = start.getY();
float z = start.getZ();
boolean down = Math.abs(dy) > 0.2;
BlockState bState;
Mutable bPos = new Mutable();
MutableBlockPos bPos = new MutableBlockPos();
for (int i = 0; i < count; i++) {
bPos.set(x + pos.getX(), y + pos.getY(), z + pos.getZ());
bState = world.getBlockState(bPos);
@ -164,8 +165,7 @@ public class SplineHelper {
if (down && bState.equals(state) || replace.apply(bState)) {
BlocksHelper.setWithoutUpdate(world, bPos, state);
}
}
else {
} else {
return false;
}
x += dx;
@ -182,13 +182,13 @@ public class SplineHelper {
BlocksHelper.setWithoutUpdate(world, bPos, state);
}
return true;
}
else {
} else {
return false;
}
}
public static void fillLineForce(Vector3f start, Vector3f end, StructureWorldAccess world, BlockState state, BlockPos pos, Function<BlockState, Boolean> replace) {
public static void fillLineForce(Vector3f start, Vector3f end, StructureWorldAccess world, BlockState state,
BlockPos pos, Function<BlockState, Boolean> replace) {
float dx = end.getX() - start.getX();
float dy = end.getY() - start.getY();
float dz = end.getZ() - start.getZ();
@ -201,9 +201,9 @@ public class SplineHelper {
float y = start.getY();
float z = start.getZ();
boolean down = Math.abs(dy) > 0.2;
BlockState bState;
Mutable bPos = new Mutable();
MutableBlockPos bPos = new MutableBlockPos();
for (int i = 0; i < count; i++) {
bPos.set(x + pos.getX(), y + pos.getY(), z + pos.getZ());
bState = world.getBlockState(bPos);
@ -230,11 +230,12 @@ public class SplineHelper {
}
}
}
public static boolean canGenerate(List<Vector3f> spline, float scale, BlockPos start, StructureWorldAccess world, Function<BlockState, Boolean> canReplace) {
public static boolean canGenerate(List<Vector3f> spline, float scale, BlockPos start, StructureWorldAccess world,
Function<BlockState, Boolean> canReplace) {
int count = spline.size();
Vector3f vec = spline.get(0);
Mutable mut = new Mutable();
MutableBlockPos mut = new MutableBlockPos();
float x1 = start.getX() + vec.getX() * scale;
float y1 = start.getY() + vec.getY() * scale;
float z1 = start.getZ() + vec.getZ() * scale;
@ -243,29 +244,31 @@ public class SplineHelper {
float x2 = start.getX() + vec.getX() * scale;
float y2 = start.getY() + vec.getY() * scale;
float z2 = start.getZ() + vec.getZ() * scale;
for (float py = y1; py < y2; py += 3) {
if (py - start.getY() < 10) continue;
if (py - start.getY() < 10)
continue;
float lerp = (py - y1) / (y2 - y1);
float x = MathHelper.lerp(lerp, x1, x2);
float z = MathHelper.lerp(lerp, z1, z2);
float x = Mth.lerp(lerp, x1, x2);
float z = Mth.lerp(lerp, z1, z2);
mut.set(x, py, z);
if (!canReplace.apply(world.getBlockState(mut))) {
return false;
}
}
x1 = x2;
y1 = y2;
z1 = z2;
}
return true;
}
public static boolean canGenerate(List<Vector3f> spline, BlockPos start, StructureWorldAccess world, Function<BlockState, Boolean> canReplace) {
public static boolean canGenerate(List<Vector3f> spline, BlockPos start, StructureWorldAccess world,
Function<BlockState, Boolean> canReplace) {
int count = spline.size();
Vector3f vec = spline.get(0);
Mutable mut = new Mutable();
MutableBlockPos mut = new MutableBlockPos();
float x1 = start.getX() + vec.getX();
float y1 = start.getY() + vec.getY();
float z1 = start.getZ() + vec.getZ();
@ -274,25 +277,26 @@ public class SplineHelper {
float x2 = start.getX() + vec.getX();
float y2 = start.getY() + vec.getY();
float z2 = start.getZ() + vec.getZ();
for (float py = y1; py < y2; py += 3) {
if (py - start.getY() < 10) continue;
if (py - start.getY() < 10)
continue;
float lerp = (py - y1) / (y2 - y1);
float x = MathHelper.lerp(lerp, x1, x2);
float z = MathHelper.lerp(lerp, z1, z2);
float x = Mth.lerp(lerp, x1, x2);
float z = Mth.lerp(lerp, z1, z2);
mut.set(x, py, z);
if (!canReplace.apply(world.getBlockState(mut))) {
return false;
}
}
x1 = x2;
y1 = y2;
z1 = z2;
}
return true;
}
public static Vector3f getPos(List<Vector3f> spline, float index) {
int i = (int) index;
int last = spline.size() - 1;
@ -302,14 +306,14 @@ public class SplineHelper {
float delta = index - i;
Vector3f p1 = spline.get(i);
Vector3f p2 = spline.get(i + 1);
float x = MathHelper.lerp(delta, p1.getX(), p2.getX());
float y = MathHelper.lerp(delta, p1.getY(), p2.getY());
float z = MathHelper.lerp(delta, p1.getZ(), p2.getZ());
float x = Mth.lerp(delta, p1.getX(), p2.getX());
float y = Mth.lerp(delta, p1.getY(), p2.getY());
float z = Mth.lerp(delta, p1.getZ(), p2.getZ());
return new Vector3f(x, y, z);
}
public static void rotateSpline(List<Vector3f> spline, float angle) {
for (Vector3f v: spline) {
for (Vector3f v : spline) {
float sin = (float) Math.sin(angle);
float cos = (float) Math.cos(angle);
float x = v.getX() * cos + v.getZ() * sin;
@ -317,27 +321,27 @@ public class SplineHelper {
v.set(x, v.getY(), z);
}
}
public static List<Vector3f> copySpline(List<Vector3f> spline) {
List<Vector3f> result = new ArrayList<Vector3f>(spline.size());
for (Vector3f v: spline) {
for (Vector3f v : spline) {
result.add(new Vector3f(v.getX(), v.getY(), v.getZ()));
}
return result;
}
public static void scale(List<Vector3f> spline, float scale) {
scale(spline, scale, scale, scale);
}
public static void scale(List<Vector3f> spline, float x, float y, float z) {
for (Vector3f v: spline) {
for (Vector3f v : spline) {
v.set(v.getX() * x, v.getY() * y, v.getZ() * z);
}
}
public static void offset(List<Vector3f> spline, Vector3f offset) {
for (Vector3f v: spline) {
for (Vector3f v : spline) {
v.set(offset.getX() + v.getX(), offset.getY() + v.getY(), offset.getZ() + v.getZ());
}
}

View file

@ -11,39 +11,38 @@ import java.util.zip.ZipFile;
import com.google.common.collect.Sets;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.Material;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.material.Material;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.structure.Structure;
import net.minecraft.structure.StructurePlacementData;
import net.minecraft.tag.BlockTags;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.Identifier;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.util.math.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.StructureWorldAccess;
import ru.betterend.registry.EndBlocks;
import ru.betterend.registry.EndTags;
public class StructureHelper {
private static final Direction[] DIR = BlocksHelper.makeHorizontal();
public static Structure readStructure(Identifier resource) {
public static Structure readStructure(ResourceLocation resource) {
String ns = resource.getNamespace();
String nm = resource.getPath();
return readStructure("/data/" + ns + "/structures/" + nm + ".nbt");
}
public static Structure readStructure(File datapack, String path) {
if (datapack.isDirectory()) {
return readStructure(datapack.toString() + "/" + path);
}
else if (datapack.isFile() && datapack.getName().endsWith(".zip")) {
} else if (datapack.isFile() && datapack.getName().endsWith(".zip")) {
try {
ZipFile zipFile = new ZipFile(datapack);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
@ -58,25 +57,23 @@ public class StructureHelper {
System.out.format("\t %s - %d - %d\n", type, compressedSize, normalSize);
}
zipFile.close();
}
catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static Structure readStructure(String path) {
try {
InputStream inputstream = StructureHelper.class.getResourceAsStream(path);
return readStructureFromStream(inputstream);
}
catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static Structure readStructureFromStream(InputStream stream) throws IOException {
CompoundTag nbttagcompound = NbtIo.readCompressed(stream);
@ -85,22 +82,25 @@ public class StructureHelper {
return template;
}
public static BlockPos offsetPos(BlockPos pos, Structure structure, BlockRotation rotation, BlockMirror mirror) {
public static BlockPos offsetPos(BlockPos pos, Structure structure, Rotation rotation, BlockMirror mirror) {
BlockPos offset = Structure.transformAround(structure.getSize(), mirror, rotation, BlockPos.ORIGIN);
return pos.add(-offset.getX() * 0.5, 0, -offset.getZ() * 0.5);
return pos.offset(-offset.getX() * 0.5, 0, -offset.getZ() * 0.5);
}
public static void placeCenteredBottom(StructureWorldAccess world, BlockPos pos, Structure structure, BlockRotation rotation, BlockMirror mirror, Random random) {
public static void placeCenteredBottom(StructureWorldAccess world, BlockPos pos, Structure structure,
Rotation rotation, BlockMirror mirror, Random random) {
placeCenteredBottom(world, pos, structure, rotation, mirror, makeBox(pos), random);
}
public static void placeCenteredBottom(StructureWorldAccess world, BlockPos pos, Structure structure, BlockRotation rotation, BlockMirror mirror, BlockBox bounds, Random random) {
public static void placeCenteredBottom(StructureWorldAccess world, BlockPos pos, Structure structure,
Rotation rotation, BlockMirror mirror, BlockBox bounds, Random random) {
BlockPos offset = offsetPos(pos, structure, rotation, mirror);
StructurePlacementData placementData = new StructurePlacementData().setRotation(rotation).setMirror(mirror).setBoundingBox(bounds);
StructurePlacementData placementData = new StructurePlacementData().setRotation(rotation).setMirror(mirror)
.setBoundingBox(bounds);
structure.place(world, offset, placementData, random);
}
private static BlockBox makeBox(BlockPos pos) {
int sx = ((pos.getX() >> 4) << 4) - 16;
int sz = ((pos.getZ() >> 4) << 4) - 16;
@ -108,28 +108,29 @@ public class StructureHelper {
int ez = sz + 47;
return BlockBox.create(sx, 0, sz, ex, 255, ez);
}
public static BlockBox getStructureBounds(BlockPos pos, Structure structure, BlockRotation rotation, BlockMirror mirror) {
public static BlockBox getStructureBounds(BlockPos pos, Structure structure, Rotation rotation,
BlockMirror mirror) {
BlockPos max = structure.getSize();
BlockPos min = Structure.transformAround(structure.getSize(), mirror, rotation, BlockPos.ORIGIN);
max = max.subtract(min);
return new BlockBox(min.add(pos), max.add(pos));
}
public static BlockBox intersectBoxes(BlockBox box1, BlockBox box2) {
int x1 = MHelper.max(box1.minX, box2.minX);
int y1 = MHelper.max(box1.minY, box2.minY);
int z1 = MHelper.max(box1.minZ, box2.minZ);
int x2 = MHelper.min(box1.maxX, box2.maxX);
int y2 = MHelper.min(box1.maxY, box2.maxY);
int z2 = MHelper.min(box1.maxZ, box2.maxZ);
return BlockBox.create(x1, y1, z1, x2, y2, z2);
}
public static void erode(StructureWorldAccess world, BlockBox bounds, int iterations, Random random) {
Mutable mut = new Mutable();
MutableBlockPos mut = new MutableBlockPos();
boolean canDestruct = true;
for (int i = 0; i < iterations; i++) {
for (int x = bounds.minX; x <= bounds.maxX; x++) {
@ -139,7 +140,8 @@ public class StructureHelper {
for (int y = bounds.maxY; y >= bounds.minY; y--) {
mut.setY(y);
BlockState state = world.getBlockState(mut);
if (canDestruct && state.isOf(EndBlocks.FLAVOLITE_RUNED_ETERNAL) && random.nextInt(8) == 0 && world.isAir(mut.down(2))) {
if (canDestruct && state.is(EndBlocks.FLAVOLITE_RUNED_ETERNAL) && random.nextInt(8) == 0
&& world.isAir(mut.down(2))) {
int r = MHelper.randRange(1, 4, random);
int cx = mut.getX();
int cy = mut.getY();
@ -162,7 +164,8 @@ public class StructureHelper {
int dz = pz - cz;
dz *= dz;
mut.setZ(pz);
if (dx + dy + dz <= r && world.getBlockState(mut).isOf(EndBlocks.FLAVOLITE_RUNED_ETERNAL)) {
if (dx + dy + dz <= r
&& world.getBlockState(mut).is(EndBlocks.FLAVOLITE_RUNED_ETERNAL)) {
BlocksHelper.setWithoutUpdate(world, mut, Blocks.AIR);
}
}
@ -173,14 +176,13 @@ public class StructureHelper {
mut.setZ(cz);
canDestruct = false;
continue;
}
else if (ignore(state)) {
} else if (ignore(state)) {
continue;
}
if (!state.isAir() && random.nextBoolean()) {
shuffle(random);
for (Direction dir: DIR) {
if (world.isAir(mut.offset(dir)) && world.isAir(mut.down().offset(dir))) {
for (Direction dir : DIR) {
if (world.isAir(mut.offset(dir)) && world.isAir(mut.below().offset(dir))) {
BlocksHelper.setWithoutUpdate(world, mut, Blocks.AIR);
mut.move(dir).move(Direction.DOWN);
for (int py = mut.getY(); y >= bounds.minY - 10; y--) {
@ -194,8 +196,8 @@ public class StructureHelper {
}
}
break;
}
else if (random.nextInt(8) == 0 && !world.getBlockState(mut.up()).isOf(EndBlocks.ETERNAL_PEDESTAL)) {
} else if (random.nextInt(8) == 0
&& !world.getBlockState(mut.up()).is(EndBlocks.ETERNAL_PEDESTAL)) {
BlocksHelper.setWithoutUpdate(world, mut, Blocks.AIR);
}
}
@ -209,7 +211,7 @@ public class StructureHelper {
for (int y = bounds.maxY; y >= bounds.minY; y--) {
mut.setY(y);
BlockState state = world.getBlockState(mut);
if (!ignore(state) && world.isAir(mut.down())) {
if (!ignore(state) && world.isAir(mut.below())) {
BlocksHelper.setWithoutUpdate(world, mut, Blocks.AIR);
for (int py = mut.getY(); py >= bounds.minY - 10; py--) {
mut.setY(py - 1);
@ -226,8 +228,8 @@ public class StructureHelper {
}
public static void erodeIntense(StructureWorldAccess world, BlockBox bounds, Random random) {
Mutable mut = new Mutable();
Mutable mut2 = new Mutable();
MutableBlockPos mut = new MutableBlockPos();
MutableBlockPos mut2 = new MutableBlockPos();
int minY = bounds.minY - 10;
for (int x = bounds.minX; x <= bounds.maxX; x++) {
mut.setX(x);
@ -251,8 +253,7 @@ public class StructureHelper {
BlocksHelper.setWithoutUpdate(world, mut2, state);
}
}
}
else if (random.nextInt(8) == 0) {
} else if (random.nextInt(8) == 0) {
BlocksHelper.setWithoutUpdate(world, mut, Blocks.AIR);
}
}
@ -262,23 +263,23 @@ public class StructureHelper {
drop(world, bounds);
}
private static boolean isTerrainNear(StructureWorldAccess world, BlockPos pos) {
for (Direction dir: BlocksHelper.DIRECTIONS) {
if (world.getBlockState(pos.offset(dir)).isIn(EndTags.GEN_TERRAIN)) {
for (Direction dir : BlocksHelper.DIRECTIONS) {
if (world.getBlockState(pos.relative(dir)).isIn(EndTags.GEN_TERRAIN)) {
return true;
}
}
return false;
}
private static void drop(StructureWorldAccess world, BlockBox bounds) {
Mutable mut = new Mutable();
MutableBlockPos mut = new MutableBlockPos();
Set<BlockPos> blocks = Sets.newHashSet();
Set<BlockPos> edge = Sets.newHashSet();
Set<BlockPos> add = Sets.newHashSet();
for (int x = bounds.minX; x <= bounds.maxX; x++) {
mut.setX(x);
for (int z = bounds.minZ; z <= bounds.maxZ; z++) {
@ -292,14 +293,14 @@ public class StructureHelper {
}
}
}
if (edge.isEmpty()) {
return;
}
while (!edge.isEmpty()) {
for (BlockPos center: edge) {
for (Direction dir: BlocksHelper.DIRECTIONS) {
for (BlockPos center : edge) {
for (Direction dir : BlocksHelper.DIRECTIONS) {
BlockState state = world.getBlockState(center);
if (state.isFullCube(world, center)) {
mut.set(center).move(dir);
@ -312,13 +313,13 @@ public class StructureHelper {
}
}
}
blocks.addAll(edge);
edge.clear();
edge.addAll(add);
add.clear();
}
int minY = bounds.minY - 10;
for (int x = bounds.minX; x <= bounds.maxX; x++) {
mut.setX(x);
@ -343,17 +344,12 @@ public class StructureHelper {
}
private static boolean ignore(BlockState state) {
return state.getMaterial().isReplaceable()
|| !state.getFluidState().isEmpty()
|| state.isIn(EndTags.END_GROUND)
|| state.isOf(EndBlocks.ETERNAL_PEDESTAL)
|| state.isOf(EndBlocks.FLAVOLITE_RUNED_ETERNAL)
|| state.isIn(BlockTags.LOGS)
|| state.isIn(BlockTags.LEAVES)
|| state.getMaterial().equals(Material.PLANT)
|| state.getMaterial().equals(Material.LEAVES);
return state.getMaterial().isReplaceable() || !state.getFluidState().isEmpty() || state.isIn(EndTags.END_GROUND)
|| state.is(EndBlocks.ETERNAL_PEDESTAL) || state.is(EndBlocks.FLAVOLITE_RUNED_ETERNAL)
|| state.isIn(BlockTags.LOGS) || state.isIn(BlockTags.LEAVES)
|| state.getMaterial().equals(Material.PLANT) || state.getMaterial().equals(Material.LEAVES);
}
private static void shuffle(Random random) {
for (int i = 0; i < 4; i++) {
int j = random.nextInt(4);
@ -362,9 +358,9 @@ public class StructureHelper {
DIR[j] = d;
}
}
public static void cover(StructureWorldAccess world, BlockBox bounds, Random random) {
Mutable mut = new Mutable();
MutableBlockPos mut = new MutableBlockPos();
for (int x = bounds.minX; x <= bounds.maxX; x++) {
mut.setX(x);
for (int z = bounds.minZ; z <= bounds.maxZ; z++) {

View file

@ -6,70 +6,70 @@ import java.util.Set;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemConvertible;
import net.minecraft.tags.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
public class TagHelper {
private static final Map<Identifier, Set<Identifier>> TAGS_BLOCK = Maps.newHashMap();
private static final Map<Identifier, Set<Identifier>> TAGS_ITEM = Maps.newHashMap();
private static final Map<ResourceLocation, Set<ResourceLocation>> TAGS_BLOCK = Maps.newHashMap();
private static final Map<ResourceLocation, Set<ResourceLocation>> TAGS_ITEM = Maps.newHashMap();
public static void addTag(Tag.Identified<Block> tag, Block... blocks) {
Identifier tagID = tag.getId();
Set<Identifier> set = TAGS_BLOCK.get(tagID);
ResourceLocation tagID = tag.getId();
Set<ResourceLocation> set = TAGS_BLOCK.get(tagID);
if (set == null) {
set = Sets.newHashSet();
TAGS_BLOCK.put(tagID, set);
}
for (Block block: blocks) {
Identifier id = Registry.BLOCK.getId(block);
for (Block block : blocks) {
ResourceLocation id = Registry.BLOCK.getKey(block);
if (id != Registry.BLOCK.getDefaultId()) {
set.add(id);
}
}
}
public static void addTag(Tag.Identified<Item> tag, ItemConvertible... items) {
Identifier tagID = tag.getId();
Set<Identifier> set = TAGS_ITEM.get(tagID);
ResourceLocation tagID = tag.getId();
Set<ResourceLocation> set = TAGS_ITEM.get(tagID);
if (set == null) {
set = Sets.newHashSet();
TAGS_ITEM.put(tagID, set);
}
for (ItemConvertible item: items) {
Identifier id = Registry.ITEM.getId(item.asItem());
for (ItemConvertible item : items) {
ResourceLocation id = Registry.ITEM.getId(item.asItem());
if (id != Registry.ITEM.getDefaultId()) {
set.add(id);
}
}
}
@SafeVarargs
public static void addTags(ItemConvertible item, Tag.Identified<Item>... tags) {
for (Tag.Identified<Item> tag: tags) {
for (Tag.Identified<Item> tag : tags) {
addTag(tag, item);
}
}
@SafeVarargs
public static void addTags(Block block, Tag.Identified<Block>... tags) {
for (Tag.Identified<Block> tag: tags) {
for (Tag.Identified<Block> tag : tags) {
addTag(tag, block);
}
}
public static Tag.Builder apply(Tag.Builder builder, Set<Identifier> ids) {
public static Tag.Builder apply(Tag.Builder builder, Set<ResourceLocation> ids) {
ids.forEach((value) -> {
builder.add(value, "Better End Code");
});
return builder;
}
public static void apply(String entry, Map<Identifier, Tag.Builder> tagsMap) {
Map<Identifier, Set<Identifier>> endTags = null;
public static void apply(String entry, Map<ResourceLocation, Tag.Builder> tagsMap) {
Map<ResourceLocation, Set<ResourceLocation>> endTags = null;
if (entry.equals("block")) {
endTags = TAGS_BLOCK;
} else if (entry.equals("item")) {

View file

@ -9,8 +9,8 @@ import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import ru.betterend.BetterEnd;
import ru.betterend.registry.EndBiomes;
import ru.betterend.registry.EndItems;
@ -19,15 +19,15 @@ public class TranslationHelper {
public static void printMissingNames() {
List<String> missingNamesEn = Lists.newArrayList();
List<String> missingNamesRu = Lists.newArrayList();
Gson gson = new Gson();
InputStream streamEn = BetterEnd.class.getResourceAsStream("/assets/betterend/lang/en_us.json");
InputStream streamRu = BetterEnd.class.getResourceAsStream("/assets/betterend/lang/ru_ru.json");
JsonObject translationEn = gson.fromJson(new InputStreamReader(streamEn), JsonObject.class);
JsonObject translationRu = gson.fromJson(new InputStreamReader(streamRu), JsonObject.class);
Registry.BLOCK.forEach((block) -> {
if (Registry.BLOCK.getId(block).getNamespace().equals(BetterEnd.MOD_ID)) {
if (Registry.BLOCK.getKey(block).getNamespace().equals(BetterEnd.MOD_ID)) {
String name = block.getName().getString();
if (!translationEn.has(name)) {
missingNamesEn.add(name);
@ -37,7 +37,7 @@ public class TranslationHelper {
}
}
});
EndItems.getModItems().forEach((item) -> {
String name = item.getName().getString();
if (!translationEn.has(name)) {
@ -47,7 +47,7 @@ public class TranslationHelper {
missingNamesRu.add(name);
}
});
EndBiomes.getModBiomes().forEach((endBiome) -> {
if (endBiome.getID().getNamespace().equals(BetterEnd.MOD_ID)) {
String name = "biome." + BetterEnd.MOD_ID + "." + endBiome.getID().getPath();
@ -59,9 +59,9 @@ public class TranslationHelper {
}
}
});
Registry.ENTITY_TYPE.forEach((entity) -> {
Identifier id = Registry.ENTITY_TYPE.getId(entity);
ResourceLocation id = Registry.ENTITY_TYPE.getId(entity);
if (id.getNamespace().equals(BetterEnd.MOD_ID)) {
String name = "entity." + BetterEnd.MOD_ID + "." + id.getPath();
if (!translationEn.has(name)) {
@ -72,12 +72,12 @@ public class TranslationHelper {
}
}
});
if (!missingNamesEn.isEmpty() || !missingNamesRu.isEmpty()) {
System.out.println("========================================");
System.out.println(" MISSING NAMES LIST");
if (!missingNamesEn.isEmpty()) {
Collections.sort(missingNamesEn);
System.out.println("========================================");
@ -87,7 +87,7 @@ public class TranslationHelper {
System.out.println(" \"" + name + "\": \"" + fastTranslateEn(name) + "\",");
});
}
if (!missingNamesRu.isEmpty()) {
Collections.sort(missingNamesRu);
System.out.println("========================================");
@ -97,11 +97,11 @@ public class TranslationHelper {
System.out.println(" \"" + name + "\": \"\",");
});
}
System.out.println("========================================");
}
}
public static String fastTranslateEn(String text) {
String[] words = text.substring(text.lastIndexOf('.') + 1).split("_");
StringBuilder builder = new StringBuilder();

View file

@ -10,34 +10,32 @@ import ru.betterend.BetterEnd;
public class WorldDataUtil {
private static CompoundTag root;
private static File saveFile;
public static void load(File file) {
saveFile = file;
if (file.exists()) {
try {
root = NbtIo.readCompressed(file);
}
catch (IOException e) {
BetterEnd.LOGGER.error("World data loading failed", e);
} catch (IOException e) {
BetterEnd.LOGGER.error("Level data loading failed", e);
root = new CompoundTag();
}
return;
}
root = new CompoundTag();
}
public static CompoundTag getRootTag() {
return root;
}
public static CompoundTag getCompoundTag(String path) {
String[] parts = path.split("\\.");
CompoundTag tag = root;
for (String part: parts) {
for (String part : parts) {
if (tag.contains(part)) {
tag = tag.getCompound(part);
}
else {
} else {
CompoundTag t = new CompoundTag();
tag.put(part, t);
tag = t;
@ -45,13 +43,12 @@ public class WorldDataUtil {
}
return tag;
}
public static void saveFile() {
try {
NbtIo.writeCompressed(root, saveFile);
}
catch (IOException e) {
BetterEnd.LOGGER.error("World data saving failed", e);
} catch (IOException e) {
BetterEnd.LOGGER.error("Level data saving failed", e);
}
}
}

View file

@ -2,33 +2,33 @@ package ru.betterend.util.sdf;
import java.util.Map;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
public class PosInfo implements Comparable<PosInfo> {
private static final BlockState AIR = Blocks.AIR.getDefaultState();
private static final BlockState AIR = Blocks.AIR.defaultBlockState();
private final Map<BlockPos, PosInfo> blocks;
private final Map<BlockPos, PosInfo> add;
private final BlockPos pos;
private BlockState state;
public static PosInfo create(Map<BlockPos, PosInfo> blocks, Map<BlockPos, PosInfo> add, BlockPos pos) {
return new PosInfo(blocks, add, pos);
}
private PosInfo(Map<BlockPos, PosInfo> blocks, Map<BlockPos, PosInfo> add, BlockPos pos) {
this.blocks = blocks;
this.add = add;
this.pos = pos;
blocks.put(pos, this);
}
public BlockState getState() {
return state;
}
public BlockState getState(BlockPos pos) {
PosInfo info = blocks.get(pos);
if (info == null) {
@ -37,48 +37,48 @@ public class PosInfo implements Comparable<PosInfo> {
}
return info.getState();
}
public void setState(BlockState state) {
this.state = state;
}
public void setState(BlockPos pos, BlockState state) {
PosInfo info = blocks.get(pos);
if (info != null) {
info.setState(state);
}
}
public BlockState getState(Direction dir) {
PosInfo info = blocks.get(pos.offset(dir));
PosInfo info = blocks.get(pos.relative(dir));
if (info == null) {
info = add.get(pos.offset(dir));
info = add.get(pos.relative(dir));
return info == null ? AIR : info.getState();
}
return info.getState();
}
public BlockState getState(Direction dir, int distance) {
PosInfo info = blocks.get(pos.offset(dir, distance));
PosInfo info = blocks.get(pos.relative(dir, distance));
if (info == null) {
return AIR;
}
return info.getState();
}
public BlockState getStateUp() {
return getState(Direction.UP);
}
public BlockState getStateDown() {
return getState(Direction.DOWN);
}
@Override
public int hashCode() {
return pos.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof PosInfo)) {
@ -95,7 +95,7 @@ public class PosInfo implements Comparable<PosInfo> {
public BlockPos getPos() {
return pos;
}
public void setBlockPos(BlockPos pos, BlockState state) {
PosInfo info = new PosInfo(blocks, add, pos);
info.state = state;

View file

@ -11,11 +11,11 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Direction;
import net.minecraft.core.Direction;
import net.minecraft.world.ServerWorldAccess;
import ru.betterend.util.BlocksHelper;
import ru.betterend.world.structures.StructureWorld;
@ -27,19 +27,19 @@ public abstract class SDF {
};
public abstract float getDistance(float x, float y, float z);
public abstract BlockState getBlockState(BlockPos pos);
public SDF addPostProcess(Function<PosInfo, BlockState> postProcess) {
this.postProcesses.add(postProcess);
return this;
}
public SDF setReplaceFunction(Function<BlockState, Boolean> canReplace) {
this.canReplace = canReplace;
return this;
}
public void fillRecursive(ServerWorldAccess world, BlockPos start) {
Map<BlockPos, PosInfo> mapWorld = Maps.newHashMap();
Map<BlockPos, PosInfo> addInfo = Maps.newHashMap();
@ -48,15 +48,15 @@ public abstract class SDF {
Set<BlockPos> add = Sets.newHashSet();
ends.add(new BlockPos(0, 0, 0));
boolean run = true;
Mutable bPos = new Mutable();
MutableBlockPos bPos = new MutableBlockPos();
while (run) {
for (BlockPos center: ends) {
for (Direction dir: Direction.values()) {
for (BlockPos center : ends) {
for (Direction dir : Direction.values()) {
bPos.set(center).move(dir);
BlockPos wpos = bPos.add(start);
if (!blocks.contains(bPos) && canReplace.apply(world.getBlockState(wpos))) {
if (this.getDistance(bPos.getX(), bPos.getY(), bPos.getZ()) < 0) {
BlockState state = getBlockState(wpos);
@ -66,15 +66,15 @@ public abstract class SDF {
}
}
}
blocks.addAll(ends);
ends.clear();
ends.addAll(add);
add.clear();
run &= !ends.isEmpty();
}
List<PosInfo> infos = new ArrayList<PosInfo>(mapWorld.values());
if (infos.size() > 0) {
Collections.sort(infos);
@ -102,12 +102,12 @@ public abstract class SDF {
});
}
}
public void fillArea(ServerWorldAccess world, BlockPos center, Box box) {
Map<BlockPos, PosInfo> mapWorld = Maps.newHashMap();
Map<BlockPos, PosInfo> addInfo = Maps.newHashMap();
Mutable mut = new Mutable();
MutableBlockPos mut = new MutableBlockPos();
for (int y = (int) box.minY; y <= box.maxY; y++) {
mut.setY(y);
for (int x = (int) box.minX; x <= box.maxX; x++) {
@ -123,7 +123,7 @@ public abstract class SDF {
}
}
}
List<PosInfo> infos = new ArrayList<PosInfo>(mapWorld.values());
if (infos.size() > 0) {
Collections.sort(infos);
@ -151,7 +151,7 @@ public abstract class SDF {
});
}
}
public void fillRecursiveIgnore(ServerWorldAccess world, BlockPos start, Function<BlockState, Boolean> ignore) {
Map<BlockPos, PosInfo> mapWorld = Maps.newHashMap();
Map<BlockPos, PosInfo> addInfo = Maps.newHashMap();
@ -160,12 +160,12 @@ public abstract class SDF {
Set<BlockPos> add = Sets.newHashSet();
ends.add(new BlockPos(0, 0, 0));
boolean run = true;
Mutable bPos = new Mutable();
MutableBlockPos bPos = new MutableBlockPos();
while (run) {
for (BlockPos center: ends) {
for (Direction dir: Direction.values()) {
for (BlockPos center : ends) {
for (Direction dir : Direction.values()) {
bPos.set(center).move(dir);
BlockPos wpos = bPos.add(start);
BlockState state = world.getBlockState(wpos);
@ -178,15 +178,15 @@ public abstract class SDF {
}
}
}
blocks.addAll(ends);
ends.clear();
ends.addAll(add);
add.clear();
run &= !ends.isEmpty();
}
List<PosInfo> infos = new ArrayList<PosInfo>(mapWorld.values());
if (infos.size() > 0) {
Collections.sort(infos);
@ -214,7 +214,7 @@ public abstract class SDF {
});
}
}
public void fillRecursive(StructureWorld world, BlockPos start) {
Map<BlockPos, PosInfo> mapWorld = Maps.newHashMap();
Map<BlockPos, PosInfo> addInfo = Maps.newHashMap();
@ -223,15 +223,15 @@ public abstract class SDF {
Set<BlockPos> add = Sets.newHashSet();
ends.add(new BlockPos(0, 0, 0));
boolean run = true;
Mutable bPos = new Mutable();
MutableBlockPos bPos = new MutableBlockPos();
while (run) {
for (BlockPos center: ends) {
for (Direction dir: Direction.values()) {
for (BlockPos center : ends) {
for (Direction dir : Direction.values()) {
bPos.set(center).move(dir);
BlockPos wpos = bPos.add(start);
if (!blocks.contains(bPos)) {
if (this.getDistance(bPos.getX(), bPos.getY(), bPos.getZ()) < 0) {
BlockState state = getBlockState(wpos);
@ -241,15 +241,15 @@ public abstract class SDF {
}
}
}
blocks.addAll(ends);
ends.clear();
ends.addAll(add);
add.clear();
run &= !ends.isEmpty();
}
List<PosInfo> infos = new ArrayList<PosInfo>(mapWorld.values());
Collections.sort(infos);
postProcesses.forEach((postProcess) -> {
@ -260,7 +260,7 @@ public abstract class SDF {
infos.forEach((info) -> {
world.setBlock(info.getPos(), info.getState());
});
infos.clear();
infos.addAll(addInfo.values());
Collections.sort(infos);
@ -273,19 +273,19 @@ public abstract class SDF {
world.setBlock(info.getPos(), info.getState());
});
}
public Set<BlockPos> getPositions(ServerWorldAccess world, BlockPos start) {
Set<BlockPos> blocks = Sets.newHashSet();
Set<BlockPos> ends = Sets.newHashSet();
Set<BlockPos> add = Sets.newHashSet();
ends.add(new BlockPos(0, 0, 0));
boolean run = true;
Mutable bPos = new Mutable();
MutableBlockPos bPos = new MutableBlockPos();
while (run) {
for (BlockPos center: ends) {
for (Direction dir: Direction.values()) {
for (BlockPos center : ends) {
for (Direction dir : Direction.values()) {
bPos.set(center).move(dir);
BlockPos wpos = bPos.add(start);
BlockState state = world.getBlockState(wpos);
@ -296,15 +296,15 @@ public abstract class SDF {
}
}
}
ends.forEach((end) -> blocks.add(end.add(start)));
ends.clear();
ends.addAll(add);
add.clear();
run &= !ends.isEmpty();
}
return blocks;
}
}

View file

@ -1,28 +1,28 @@
package ru.betterend.util.sdf.operator;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import ru.betterend.util.sdf.SDF;
public abstract class SDFBinary extends SDF {
protected SDF sourceA;
protected SDF sourceB;
protected boolean firstValue;
public SDFBinary setSourceA(SDF sourceA) {
this.sourceA = sourceA;
return this;
}
public SDFBinary setSourceB(SDF sourceB) {
this.sourceB = sourceB;
return this;
}
protected void selectValue(float a, float b) {
firstValue = a < b;
}
@Override
public BlockState getBlockState(BlockPos pos) {
if (firstValue) {

View file

@ -1,7 +1,7 @@
package ru.betterend.util.sdf.operator;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.Mth;
public class SDFHeightmap extends SDFDisplacement {
private float intensity = 1F;
@ -11,18 +11,18 @@ public class SDFHeightmap extends SDFDisplacement {
private float scale;
private float cos = 1;
private float sin = 0;
public SDFHeightmap() {
setFunction((pos) -> {
if (map == null) {
return 0F;
}
float px = MathHelper.clamp(pos.getX() * scale + offsetX, 0, map.getWidth() - 2);
float pz = MathHelper.clamp(pos.getZ() * scale + offsetZ, 0, map.getHeight() - 2);
float px = Mth.clamp(pos.getX() * scale + offsetX, 0, map.getWidth() - 2);
float pz = Mth.clamp(pos.getZ() * scale + offsetZ, 0, map.getHeight() - 2);
float dx = (px * cos - pz * sin);
float dz = (pz * cos + px * sin);
int x1 = MathHelper.floor(dx);
int z1 = MathHelper.floor(dz);
int x1 = Mth.floor(dx);
int z1 = Mth.floor(dz);
int x2 = x1 + 1;
int z2 = z1 + 1;
dx = dx - x1;
@ -31,12 +31,12 @@ public class SDFHeightmap extends SDFDisplacement {
float b = (map.getPixelColor(x2, z1) & 255) / 255F;
float c = (map.getPixelColor(x1, z2) & 255) / 255F;
float d = (map.getPixelColor(x2, z2) & 255) / 255F;
a = MathHelper.lerp(dx, a, b);
b = MathHelper.lerp(dx, c, d);
return -MathHelper.lerp(dz, a, b) * intensity;
a = Mth.lerp(dx, a, b);
b = Mth.lerp(dx, c, d);
return -Mth.lerp(dz, a, b) * intensity;
});
}
public SDFHeightmap setMap(NativeImage map) {
this.map = map;
offsetX = map.getWidth() * 0.5F;
@ -44,18 +44,18 @@ public class SDFHeightmap extends SDFDisplacement {
scale = map.getWidth();
return this;
}
public SDFHeightmap setAngle(float angle) {
sin = MathHelper.sin(angle);
cos = MathHelper.cos(angle);
sin = Mth.sin(angle);
cos = Mth.cos(angle);
return this;
}
public SDFHeightmap setScale(float scale) {
this.scale = map.getWidth() * scale;
return this;
}
public SDFHeightmap setIntensity(float intensity) {
this.intensity = intensity;
return this;

View file

@ -1,19 +1,19 @@
package ru.betterend.util.sdf.operator;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.Mth;
import ru.betterend.noise.OpenSimplexNoise;
import ru.betterend.util.MHelper;
public class SDFRadialNoiseMap extends SDFDisplacement {
private static final float SIN = MathHelper.sin(0.5F);
private static final float COS = MathHelper.cos(0.5F);
private static final float SIN = Mth.sin(0.5F);
private static final float COS = Mth.cos(0.5F);
private OpenSimplexNoise noise;
private float intensity = 1F;
private float radius = 1F;
private short offsetX;
private short offsetZ;
public SDFRadialNoiseMap() {
setFunction((pos) -> {
if (intensity == 0) {
@ -25,33 +25,34 @@ public class SDFRadialNoiseMap extends SDFDisplacement {
if (distance > 1) {
return 0F;
}
distance = 1 - MathHelper.sqrt(distance);
distance = 1 - Mth.sqrt(distance);
float nx = px * COS - pz * SIN;
float nz = pz * COS + px * SIN;
distance *= getNoise(nx * 0.75 + offsetX, nz * 0.75 + offsetZ);
return distance * intensity;
});
}
private float getNoise(double x, double z) {
return (float) noise.eval(x, z) + (float) noise.eval(x * 3 + 1000, z * 3) * 0.5F + (float) noise.eval(x * 9 + 1000, z * 9) * 0.2F;
return (float) noise.eval(x, z) + (float) noise.eval(x * 3 + 1000, z * 3) * 0.5F
+ (float) noise.eval(x * 9 + 1000, z * 9) * 0.2F;
}
public SDFRadialNoiseMap setSeed(long seed) {
noise = new OpenSimplexNoise(seed);
return this;
}
public SDFRadialNoiseMap setIntensity(float intensity) {
this.intensity = intensity;
return this;
}
public SDFRadialNoiseMap setRadius(float radius) {
this.radius = radius;
return this;
}
public SDFRadialNoiseMap setOffset(int x, int z) {
offsetX = (short) (x & 32767);
offsetZ = (short) (z & 32767);

View file

@ -1,6 +1,6 @@
package ru.betterend.util.sdf.operator;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.Mth;
public class SDFSmoothIntersection extends SDFBinary {
private float radius;
@ -9,13 +9,13 @@ public class SDFSmoothIntersection extends SDFBinary {
this.radius = radius;
return this;
}
@Override
public float getDistance(float x, float y, float z) {
float a = this.sourceA.getDistance(x, y, z);
float b = this.sourceB.getDistance(x, y, z);
this.selectValue(a, b);
float h = MathHelper.clamp(0.5F - 0.5F * (b - a) / radius, 0F, 1F);
return MathHelper.lerp(h, b, a) + radius * h * (1F - h);
float h = Mth.clamp(0.5F - 0.5F * (b - a) / radius, 0F, 1F);
return Mth.lerp(h, b, a) + radius * h * (1F - h);
}
}

View file

@ -1,6 +1,6 @@
package ru.betterend.util.sdf.operator;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.Mth;
public class SDFSmoothSubtraction extends SDFBinary {
private float radius;
@ -9,13 +9,13 @@ public class SDFSmoothSubtraction extends SDFBinary {
this.radius = radius;
return this;
}
@Override
public float getDistance(float x, float y, float z) {
float a = this.sourceA.getDistance(x, y, z);
float b = this.sourceB.getDistance(x, y, z);
this.selectValue(a, b);
float h = MathHelper.clamp(0.5F - 0.5F * (b + a) / radius, 0F, 1F);
return MathHelper.lerp(h, b, -a) + radius * h * (1F - h);
float h = Mth.clamp(0.5F - 0.5F * (b + a) / radius, 0F, 1F);
return Mth.lerp(h, b, -a) + radius * h * (1F - h);
}
}

View file

@ -1,6 +1,6 @@
package ru.betterend.util.sdf.operator;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.Mth;
public class SDFSmoothUnion extends SDFBinary {
private float radius;
@ -15,7 +15,7 @@ public class SDFSmoothUnion extends SDFBinary {
float a = this.sourceA.getDistance(x, y, z);
float b = this.sourceB.getDistance(x, y, z);
this.selectValue(a, b);
float h = MathHelper.clamp(0.5F + 0.5F * (b - a) / radius, 0F, 1F);
return MathHelper.lerp(h, b, a) - radius * h * (1F - h);
float h = Mth.clamp(0.5F + 0.5F * (b - a) / radius, 0F, 1F);
return Mth.lerp(h, b, a) - radius * h * (1F - h);
}
}

View file

@ -1,17 +1,17 @@
package ru.betterend.util.sdf.operator;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import ru.betterend.util.sdf.SDF;
public abstract class SDFUnary extends SDF {
protected SDF source;
public SDFUnary setSource(SDF source) {
this.source = source;
return this;
}
@Override
public BlockState getBlockState(BlockPos pos) {
return source.getBlockState(pos);

View file

@ -1,23 +1,23 @@
package ru.betterend.util.sdf.primitive;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.Mth;
import ru.betterend.util.MHelper;
public class SDFCappedCone extends SDFPrimitive {
private float radius1;
private float radius2;
private float height;
public SDFCappedCone setRadius1(float radius) {
this.radius1 = radius;
return this;
}
public SDFCappedCone setRadius2(float radius) {
this.radius2 = radius;
return this;
}
public SDFCappedCone setHeight(float height) {
this.height = height;
return this;
@ -30,7 +30,8 @@ public class SDFCappedCone extends SDFPrimitive {
float k2y = 2 * height;
float cax = qx - MHelper.min(qx, (y < 0F) ? radius1 : radius2);
float cay = Math.abs(y) - height;
float mlt = MathHelper.clamp(MHelper.dot(radius2 - qx, height - y, k2x, k2y) / MHelper.dot(k2x, k2y, k2x, k2y), 0F, 1F);
float mlt = Mth.clamp(MHelper.dot(radius2 - qx, height - y, k2x, k2y) / MHelper.dot(k2x, k2y, k2x, k2y), 0F,
1F);
float cbx = qx - radius2 + k2x * mlt;
float cby = y - height + k2y * mlt;
float s = (cbx < 0F && cay < 0F) ? -1F : 1F;

View file

@ -1,24 +1,24 @@
package ru.betterend.util.sdf.primitive;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.Mth;
import ru.betterend.util.MHelper;
public class SDFCapsule extends SDFPrimitive {
private float radius;
private float height;
public SDFCapsule setRadius(float radius) {
this.radius = radius;
return this;
}
public SDFCapsule setHeight(float height) {
this.height = height;
return this;
}
@Override
public float getDistance(float x, float y, float z) {
return MHelper.length(x, y - MathHelper.clamp(y, 0, height), z) - radius;
return MHelper.length(x, y - Mth.clamp(y, 0, height), z) - radius;
}
}

View file

@ -1,6 +1,6 @@
package ru.betterend.util.sdf.primitive;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.Mth;
import ru.betterend.util.MHelper;
public class SDFLine extends SDFPrimitive {
@ -11,19 +11,19 @@ public class SDFLine extends SDFPrimitive {
private float x2;
private float y2;
private float z2;
public SDFLine setRadius(float radius) {
this.radius = radius;
return this;
}
public SDFLine setStart(float x, float y, float z) {
this.x1 = x;
this.y1 = y;
this.z1 = z;
return this;
}
public SDFLine setEnd(float x, float y, float z) {
this.x2 = x;
this.y2 = y;
@ -43,7 +43,7 @@ public class SDFLine extends SDFPrimitive {
float dpb = MHelper.dot(pax, pay, paz, bax, bay, baz);
float dbb = MHelper.dot(bax, bay, baz, bax, bay, baz);
float h = MathHelper.clamp(dpb / dbb, 0F, 1F);
float h = Mth.clamp(dpb / dbb, 0F, 1F);
return MHelper.length(pax - bax * h, pay - bay * h, paz - baz * h) - radius;
}
}

View file

@ -1,31 +1,31 @@
package ru.betterend.util.sdf.primitive;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.Mth;
import ru.betterend.util.MHelper;
public class SDFPie extends SDFPrimitive {
private float sin;
private float cos;
private float radius;
public SDFPie setAngle(float angle) {
this.sin = (float) Math.sin(angle);
this.cos = (float) Math.cos(angle);
return this;
}
public SDFPie setRadius(float radius) {
this.radius = radius;
return this;
}
@Override
public float getDistance(float x, float y, float z) {
float px = Math.abs(x);
float l = MHelper.length(px, y, z) - radius;
float m = MHelper.dot(px, z, sin, cos);
m = MathHelper.clamp(m, 0, radius);
float l = MHelper.length(px, y, z) - radius;
float m = MHelper.dot(px, z, sin, cos);
m = Mth.clamp(m, 0, radius);
m = MHelper.length(px - sin * m, z - cos * m);
return MHelper.max(l, m * (float) Math.signum(cos * px - sin * z));
return MHelper.max(l, m * (float) Math.signum(cos * px - sin * z));
}
}

View file

@ -2,38 +2,40 @@ package ru.betterend.util.sdf.primitive;
import java.util.function.Function;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import ru.betterend.util.sdf.SDF;
public abstract class SDFPrimitive extends SDF {
protected Function<BlockPos, BlockState> placerFunction;
public SDFPrimitive setBlock(Function<BlockPos, BlockState> placerFunction) {
this.placerFunction = placerFunction;
return this;
}
public SDFPrimitive setBlock(BlockState state) {
this.placerFunction = (pos) -> {
return state;
};
return this;
}
public SDFPrimitive setBlock(Block block) {
this.placerFunction = (pos) -> {
return block.getDefaultState();
return block.defaultBlockState();
};
return this;
}
public BlockState getBlockState(BlockPos pos) {
return placerFunction.apply(pos);
}
/*public abstract CompoundTag toNBT(CompoundTag root) {
}*/
/*
* public abstract CompoundTag toNBT(CompoundTag root) {
*
* }
*/
}