Fixed structure features and code style

This commit is contained in:
paulevsGitch 2021-07-10 16:07:44 +03:00
parent d431f2555c
commit 5a9365e2bb
153 changed files with 2304 additions and 2459 deletions

View file

@ -1,10 +1,6 @@
package ru.bclib.util;
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;
@ -18,48 +14,51 @@ import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import java.util.Map;
import java.util.Random;
public class BlocksHelper {
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();
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++) {
@ -67,7 +66,7 @@ public class BlocksHelper {
}
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++) {
@ -75,7 +74,7 @@ public class BlocksHelper {
}
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++) {
@ -83,7 +82,7 @@ public class BlocksHelper {
}
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++) {
@ -91,21 +90,23 @@ public class BlocksHelper {
}
return (int) pos.distSqr(POS);
}
/**
* Rotates {@link BlockState} horizontally. Used in block classes with {@link Direction} {@link Property} in rotate function.
* @param state - {@link BlockState} to mirror;
*
* @param state - {@link BlockState} to mirror;
* @param rotation - {@link Rotation};
* @param facing - Block {@link Direction} {@link Property}.
* @param facing - Block {@link Direction} {@link Property}.
* @return Rotated {@link BlockState}.
*/
public static BlockState rotateHorizontal(BlockState state, Rotation rotation, Property<Direction> facing) {
return state.setValue(facing, rotation.rotate(state.getValue(facing)));
}
/**
* Mirrors {@link BlockState} horizontally. Used in block classes with {@link Direction} {@link Property} in mirror function.
* @param state - {@link BlockState} to mirror;
*
* @param state - {@link BlockState} to mirror;
* @param mirror - {@link Mirror};
* @param facing - Block {@link Direction} {@link Property}.
* @return Mirrored {@link BlockState}.
@ -113,11 +114,12 @@ public class BlocksHelper {
public static BlockState mirrorHorizontal(BlockState state, Mirror mirror, Property<Direction> facing) {
return state.rotate(mirror.getRotation(state.getValue(facing)));
}
/**
* Counts the amount of same block down.
*
* @param world - {@link LevelAccessor} world;
* @param pos - {@link BlockPos} start position;
* @param pos - {@link BlockPos} start position;
* @param block - {@link Block} to count.
* @return Integer amount of blocks.
*/
@ -128,36 +130,40 @@ public class BlocksHelper {
}
return count;
}
/**
* Creates a new {@link Direction} array with clockwise order:
* NORTH, EAST, SOUTH, WEST
*
* @return Array of {@link Direction}.
*/
public static Direction[] makeHorizontal() {
return new Direction[] { Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST };
return new Direction[] {Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST};
}
/**
* Get any random horizontal {@link Direction}.
*
* @param random - {@link Random}.
* @return {@link Direction}.
*/
public static Direction randomHorizontal(Random random) {
return HORIZONTAL[random.nextInt(4)];
}
/**
* Get any random {@link Direction} including vertical and horizontal.
*
* @param random - {@link Random}.
* @return {@link Direction}.
*/
public static Direction randomDirection(Random random) {
return DIRECTIONS[random.nextInt(6)];
}
/**
* Check if block is {@link Fluid} or not.
*
* @param state - {@link BlockState} to check.
* @return {@code true} if block is fluid and {@code false} if not.
*/
@ -167,9 +173,10 @@ public class BlocksHelper {
/**
* Check if block is "invulnerable" like Bedrock.
*
* @param state - {@link BlockState} to check;
* @param world - {@link BlockGetter} world where BlockState exist;
* @param pos - {@link BlockPos} where BlockState is.
* @param pos - {@link BlockPos} where BlockState is.
* @return {@code true} if block is "invulnerable" and {@code false} if not.
*/
public static boolean isInvulnerable(BlockState state, BlockGetter world, BlockPos pos) {
@ -178,6 +185,7 @@ public class BlocksHelper {
/**
* Check if block is "invulnerable" like Bedrock. Unlike safe function will pass world and position parameters as {@code null}.
*
* @param state - {@link BlockState} to check.
* @return {@code true} if block is "invulnerable" and {@code false} if not.
*/

View file

@ -9,7 +9,7 @@ public class ColorExtractor {
private List<Center> centers = new ArrayList<>();
private List<Integer> colors;
private Integer result;
public ColorExtractor(List<Integer> colors) {
this.colors = colors;
Random rnd = new Random();
@ -19,7 +19,7 @@ public class ColorExtractor {
this.centers.add(new Center(color));
}
}
public int analize() {
boolean moved = true;
while (moved) {
@ -41,10 +41,10 @@ public class ColorExtractor {
toClear.forEach(clear -> centers.remove(clear));
}
this.centers.sort(Center.COMPARATOR);
return this.getResult();
}
public int getResult() {
if (result == null) {
double weights = 0;
@ -59,19 +59,20 @@ public class ColorExtractor {
red += center.r * weight;
green += center.g * weight;
blue += center.b * weight;
} ;
}
;
int a = (int) Math.round(alpha / weights);
int r = (int) Math.round(red / weights);
int g = (int) Math.round(green / weights);
int b = (int) Math.round(blue / weights);
this.result = a << 24 | r << 16 | g << 8 | b;
}
return this.result;
}
private void remap() {
this.centers.forEach(entry -> entry.colors.clear());
this.colors.forEach(color -> {
@ -89,7 +90,7 @@ public class ColorExtractor {
this.centers.get(id).colors.add(color);
});
}
private static class Center {
static final Comparator<Center> COMPARATOR = new Comparator<Center>() {
@Override
@ -97,24 +98,24 @@ public class ColorExtractor {
return Integer.compare(c1.getColor(), c2.getColor());
}
};
List<Integer> colors = new ArrayList<>();
double a, r, g, b;
Center(int color) {
this.a = (color >> 24) & 255;
this.r = (color >> 16) & 255;
this.g = (color >> 8) & 255;
this.b = color & 255;
}
private void update(double a, double r, double g, double b) {
this.a = a;
this.r = r;
this.g = g;
this.b = b;
}
public int getColor() {
int a = (int) Math.round(this.a);
int r = (int) Math.round(this.r);
@ -122,7 +123,7 @@ public class ColorExtractor {
int b = (int) Math.round(this.b);
return a << 24 | r << 16 | g << 8 | b;
}
public boolean move() {
double or = r;
double og = g;
@ -139,9 +140,9 @@ public class ColorExtractor {
r /= size;
g /= size;
b /= size;
this.update(a, r, g, b);
return Math.abs(r - or) > 0.1 || Math.abs(g - og) > 0.1 || Math.abs(b - ob) > 0.1;
}
}

View file

@ -1,13 +1,7 @@
package ru.bclib.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Maps;
import com.mojang.blaze3d.platform.NativeImage;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.impl.client.indigo.renderer.helper.ColorHelper;
@ -21,39 +15,39 @@ import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import ru.bclib.BCLib;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ColorUtil {
private static final float[] FLOAT_BUFFER = new float[4];
private static final int ALPHA = 255 << 24;
public static int color(int r, int g, int b) {
return ALPHA | (r << 16) | (g << 8) | b;
}
public static int color(String hex) {
int r = Integer.parseInt(hex.substring(0, 2), 16);
int g = Integer.parseInt(hex.substring(2, 4), 16);
int b = Integer.parseInt(hex.substring(4, 6), 16);
return color(r, g, b);
}
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) {
FLOAT_BUFFER[0] = ((color >> 16 & 255) / 255.0F);
FLOAT_BUFFER[1] = ((color >> 8 & 255) / 255.0F);
FLOAT_BUFFER[2] = ((color & 255) / 255.0F);
FLOAT_BUFFER[3] = ((color >> 24 & 255) / 255.0F);
return FLOAT_BUFFER;
}
public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals) {
float hue, saturation, brightness;
if (hsbvals == null) {
@ -63,34 +57,27 @@ public class ColorUtil {
if (b > cmax) cmax = b;
int cmin = (r < g) ? r : g;
if (b < cmin) cmin = b;
brightness = ((float) cmax) / 255.0F;
if (cmax != 0)
saturation = ((float) (cmax - cmin)) / ((float) cmax);
else
saturation = 0;
if (saturation == 0)
hue = 0;
if (cmax != 0) saturation = ((float) (cmax - cmin)) / ((float) cmax);
else saturation = 0;
if (saturation == 0) hue = 0;
else {
float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
if (r == cmax)
hue = bluec - greenc;
else if (g == cmax)
hue = 2.0F + redc - bluec;
else
hue = 4.0F + greenc - redc;
if (r == cmax) hue = bluec - greenc;
else if (g == cmax) hue = 2.0F + redc - bluec;
else hue = 4.0F + greenc - redc;
hue = hue / 6.0F;
if (hue < 0)
hue = hue + 1.0F;
if (hue < 0) hue = hue + 1.0F;
}
hsbvals[0] = hue;
hsbvals[1] = saturation;
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) {
@ -137,13 +124,13 @@ 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;
@ -153,7 +140,7 @@ public class ColorUtil {
color = 0;
shift = 24;
}
try {
String[] splited = hexColor.split("(?<=\\G.{2})");
for (String digit : splited) {
@ -165,17 +152,17 @@ public class ColorUtil {
BCLib.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;
@ -183,18 +170,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, FLOAT_BUFFER);
FLOAT_BUFFER[2] += val / 10.0F;
FLOAT_BUFFER[2] = Mth.clamp(FLOAT_BUFFER[2], 0.0F, 1.0F);
return HSBtoRGB(FLOAT_BUFFER[0], FLOAT_BUFFER[1], FLOAT_BUFFER[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;
@ -204,9 +191,9 @@ public class ColorUtil {
int b2 = color2 & 255;
return MHelper.sqr(r1 - r2) + MHelper.sqr(g1 - g2) + MHelper.sqr(b1 - b2);
}
private static Map<ResourceLocation, Integer> colorPalette = Maps.newHashMap();
@Environment(EnvType.CLIENT)
public static int extractColor(Item item) {
ResourceLocation id = Registry.ITEM.getKey(item);
@ -232,16 +219,16 @@ public class ColorUtil {
}
}
image.close();
if (colors.size() == 0) return -1;
ColorExtractor extractor = new ColorExtractor(colors);
int color = extractor.analize();
colorPalette.put(id, color);
return color;
}
@Environment(EnvType.CLIENT)
public static NativeImage loadImage(ResourceLocation image, int w, int h) {
Minecraft minecraft = Minecraft.getInstance();

View file

@ -1,5 +1,11 @@
package ru.bclib.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import ru.bclib.BCLib;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
@ -8,16 +14,9 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import ru.bclib.BCLib;
public class JsonFactory {
public final static Gson GSON = new GsonBuilder().setPrettyPrinting().create();
public static JsonObject getJsonObject(InputStream stream) {
try {
Reader reader = new InputStreamReader(stream);
@ -32,7 +31,7 @@ public class JsonFactory {
}
return new JsonObject();
}
public static JsonObject getJsonObject(File jsonFile) {
if (jsonFile.exists()) {
JsonElement json = loadJson(jsonFile);
@ -43,7 +42,7 @@ public class JsonFactory {
}
return new JsonObject();
}
public static JsonElement loadJson(File jsonFile) {
if (jsonFile.exists()) {
try (Reader reader = new FileReader(jsonFile)) {
@ -55,11 +54,11 @@ public class JsonFactory {
}
return null;
}
public static JsonElement loadJson(Reader reader) {
return GSON.fromJson(reader, JsonElement.class);
}
public static void storeJson(File jsonFile, JsonElement jsonObject) {
try (FileWriter writer = new FileWriter(jsonFile)) {
String json = GSON.toJson(jsonObject);
@ -70,22 +69,22 @@ public class JsonFactory {
BCLib.LOGGER.catching(ex);
}
}
public static int getInt(JsonObject object, String member, int def) {
JsonElement elem = object.get(member);
return elem == null ? def : elem.getAsInt();
}
public static float getFloat(JsonObject object, String member, float def) {
JsonElement elem = object.get(member);
return elem == null ? def : elem.getAsFloat();
}
public static boolean getBoolean(JsonObject object, String member, boolean def) {
JsonElement elem = object.get(member);
return elem == null ? def : elem.getAsBoolean();
}
public static String getString(JsonObject object, String member, String def) {
JsonElement elem = object.get(member);
return elem == null ? def : elem.getAsString();

View file

@ -6,56 +6,56 @@ import org.apache.logging.log4j.LogManager;
public final class Logger {
private static final org.apache.logging.log4j.Logger LOGGER = LogManager.getLogger();
private final String modPref;
public Logger(String modID) {
this.modPref = "[" + modID + "] ";
}
public void log(Level level, String message) {
LOGGER.log(level, modPref + message);
}
public void log(Level level, String message, Object... params) {
LOGGER.log(level, modPref + message, params);
}
public void debug(Object message) {
this.log(Level.DEBUG, message.toString());
}
public void debug(Object message, Object... params) {
this.log(Level.DEBUG, message.toString(), params);
}
public void catching(Throwable ex) {
this.error(ex.getLocalizedMessage());
LOGGER.catching(ex);
}
public void info(String message) {
this.log(Level.INFO, message);
}
public void info(String message, Object... params) {
this.log(Level.INFO, message, params);
}
public void warning(String message, Object... params) {
this.log(Level.WARN, message, params);
}
public void warning(String message, Object obj, Exception ex) {
LOGGER.warn(modPref + message, obj, ex);
}
public void error(String message) {
this.log(Level.ERROR, message);
}
public void error(String message, Object obj, Exception ex) {
LOGGER.error(modPref + message, obj, ex);
}
public void error(String message, Exception ex) {
LOGGER.error(modPref + message, ex);
}

View file

@ -1,18 +1,17 @@
package ru.bclib.util;
import java.util.Random;
import com.mojang.math.Vector3f;
import net.minecraft.core.Vec3i;
import java.util.Random;
public class MHelper {
private static final Vec3i[] RANDOM_OFFSETS = new Vec3i[3 * 3 * 3 - 1];
private static final float RAD_TO_DEG = 57.295779513082320876798154814105F;
public static final float PHI = (float) (Math.PI * (3 - Math.sqrt(5)));
public static final float PI2 = (float) (Math.PI * 2);
public static final Random RANDOM = new Random();
public static int randRange(int min, int max, Random random) {
return min + random.nextInt(max - min + 1);
}
@ -20,37 +19,37 @@ public class MHelper {
public static double randRange(double min, double max, Random random) {
return min + random.nextDouble() * (max - min);
}
public static float randRange(float min, float max, Random random) {
return min + random.nextFloat() * (max - min);
}
public static byte setBit(byte source, int pos, boolean value) {
return value ? setBitTrue(source, pos) : setBitFalse(source, pos);
}
public static byte setBitTrue(byte source, int pos) {
source |= 1 << pos;
return source;
}
public static byte setBitFalse(byte source, int pos) {
source &= ~(1 << pos);
return source;
}
public static boolean getBit(byte source, int pos) {
return ((source >> pos) & 1) == 1;
}
public static float wrap(float x, float side) {
return x - floor(x / side) * side;
}
public static int floor(double x) {
return x < 0 ? (int) (x - 1) : (int) x;
}
public static int min(int a, int b) {
return a < b ? a : b;
}
@ -134,7 +133,7 @@ public class MHelper {
h = (h ^ (h >> 13)) * 1274126177;
return h ^ (h >> 16);
}
public static int getSeed(int seed, int x, int y, int z) {
int h = seed + x * 374761393 + y * 668265263 + z;
h = (h ^ (h >> 13)) * 1274126177;
@ -149,7 +148,7 @@ public class MHelper {
array[i2] = element;
}
}
public static int sqr(int i) {
return i * i;
}
@ -170,8 +169,7 @@ public class MHelper {
return value / RAD_TO_DEG;
}
public static Vector3f cross(Vector3f vec1, Vector3f vec2)
{
public static Vector3f cross(Vector3f vec1, Vector3f vec2) {
float cx = vec1.y() * vec2.z() - vec1.z() * vec2.y();
float cy = vec1.z() * vec2.x() - vec1.x() * vec2.z();
float cz = vec1.x() * vec2.y() - vec1.y() * vec2.x();

View file

@ -1,13 +1,7 @@
package ru.bclib.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
import com.google.common.collect.Lists;
import com.mojang.math.Vector3f;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.util.Mth;
@ -17,6 +11,11 @@ import ru.bclib.sdf.SDF;
import ru.bclib.sdf.operator.SDFUnion;
import ru.bclib.sdf.primitive.SDFLine;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
public class SplineHelper {
public static List<Vector3f> makeSpline(float x1, float y1, float z1, float x2, float y2, float z2, int points) {
List<Vector3f> spline = Lists.newArrayList();
@ -86,11 +85,7 @@ 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(Mth.lerp(delta, radius1, radius2))
.setStart(start.x(), start.y(), start.z())
.setEnd(pos.x(), pos.y(), pos.z())
.setBlock(placerFunction);
SDF line = new SDFLine().setRadius(Mth.lerp(delta, radius1, radius2)).setStart(start.x(), start.y(), start.z()).setEnd(pos.x(), pos.y(), pos.z()).setBlock(placerFunction);
result = result == null ? line : new SDFUnion().setSourceA(result).setSourceB(line);
start = pos;
}
@ -105,11 +100,7 @@ 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.x(), start.y(), start.z())
.setEnd(pos.x(), pos.y(), pos.z())
.setBlock(placerFunction);
SDF line = new SDFLine().setRadius(radiusFunction.apply(delta)).setStart(start.x(), start.y(), start.z()).setEnd(pos.x(), pos.y(), pos.z()).setBlock(placerFunction);
result = result == null ? line : new SDFUnion().setSourceA(result).setSourceB(line);
start = pos;
}
@ -309,7 +300,7 @@ public class SplineHelper {
}
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.x() * cos + v.z() * sin;
@ -320,7 +311,7 @@ public class SplineHelper {
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.x(), v.y(), v.z()));
}
return result;
@ -331,13 +322,13 @@ public class SplineHelper {
}
public static void scale(List<Vector3f> spline, float x, float y, float z) {
for (Vector3f v: spline) {
for (Vector3f v : spline) {
v.set(v.x() * x, v.y() * y, v.z() * z);
}
}
public static void offset(List<Vector3f> spline, Vector3f offset) {
for (Vector3f v: spline) {
for (Vector3f v : spline) {
v.set(offset.x() + v.x(), offset.y() + v.y(), offset.z() + v.z());
}
}

View file

@ -1,16 +1,6 @@
package ru.bclib.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Random;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import com.google.common.collect.Sets;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.core.Direction;
@ -31,6 +21,15 @@ import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.Vec3;
import ru.bclib.api.TagAPI;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Random;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class StructureHelper {
private static final Direction[] DIR = BlocksHelper.makeHorizontal();
@ -54,7 +53,7 @@ public class StructureHelper {
long compressedSize = entry.getCompressedSize();
long normalSize = entry.getSize();
String type = entry.isDirectory() ? "DIR" : "FILE";
System.out.println(name);
System.out.format("\t %s - %d - %d\n", type, compressedSize, normalSize);
}
@ -80,10 +79,10 @@ public class StructureHelper {
private static StructureTemplate readStructureFromStream(InputStream stream) throws IOException {
CompoundTag nbttagcompound = NbtIo.readCompressed(stream);
StructureTemplate template = new StructureTemplate();
template.load(nbttagcompound);
return template;
}
@ -181,7 +180,7 @@ public class StructureHelper {
}
if (!state.isAir() && random.nextBoolean()) {
MHelper.shuffle(DIR, random);
for (Direction dir: DIR) {
for (Direction dir : DIR) {
if (world.isEmptyBlock(mut.relative(dir)) && world.isEmptyBlock(mut.below().relative(dir))) {
BlocksHelper.setWithoutUpdate(world, mut, Blocks.AIR);
mut.move(dir).move(Direction.DOWN);
@ -226,7 +225,7 @@ public class StructureHelper {
}
}
}
public static void erodeIntense(WorldGenLevel world, BoundingBox bounds, Random random) {
MutableBlockPos mut = new MutableBlockPos();
MutableBlockPos mut2 = new MutableBlockPos();
@ -261,12 +260,12 @@ public class StructureHelper {
}
}
}
drop(world, bounds);
}
private static boolean isTerrainNear(WorldGenLevel world, BlockPos pos) {
for (Direction dir: BlocksHelper.DIRECTIONS) {
for (Direction dir : BlocksHelper.DIRECTIONS) {
if (world.getBlockState(pos.relative(dir)).is(TagAPI.GEN_TERRAIN)) {
return true;
}
@ -300,8 +299,8 @@ public class StructureHelper {
}
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.isCollisionShapeFullBlock(world, center)) {
mut.set(center).move(dir);
@ -343,16 +342,9 @@ public class StructureHelper {
}
}
}
private static boolean ignore(BlockState state, WorldGenLevel world, BlockPos pos) {
return state.getMaterial().isReplaceable() ||
!state.getFluidState().isEmpty() ||
state.is(TagAPI.END_GROUND) ||
state.is(BlockTags.LOGS) ||
state.is(BlockTags.LEAVES) ||
state.getMaterial().equals(Material.PLANT) ||
state.getMaterial().equals(Material.LEAVES) ||
BlocksHelper.isInvulnerable(state, world, pos);
return state.getMaterial().isReplaceable() || !state.getFluidState().isEmpty() || state.is(TagAPI.END_GROUND) || state.is(BlockTags.LOGS) || state.is(BlockTags.LEAVES) || state.getMaterial().equals(Material.PLANT) || state.getMaterial().equals(Material.LEAVES) || BlocksHelper.isInvulnerable(state, world, pos);
}
public static void cover(WorldGenLevel world, BoundingBox bounds, Random random) {

View file

@ -1,11 +1,7 @@
package ru.bclib.util;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.Tag;
@ -13,6 +9,9 @@ import net.minecraft.world.item.Item;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
import java.util.Map;
import java.util.Set;
public class TagHelper {
private static final Map<ResourceLocation, Set<ResourceLocation>> TAGS_BLOCK = Maps.newConcurrentMap();
private static final Map<ResourceLocation, Set<ResourceLocation>> TAGS_ITEM = Maps.newConcurrentMap();
@ -20,7 +19,7 @@ public class TagHelper {
public static void addTag(Tag.Named<Block> tag, Block... blocks) {
ResourceLocation tagID = tag.getName();
Set<ResourceLocation> set = TAGS_BLOCK.computeIfAbsent(tagID, k -> Sets.newHashSet());
for (Block block: blocks) {
for (Block block : blocks) {
ResourceLocation id = Registry.BLOCK.getKey(block);
if (id != Registry.BLOCK.getDefaultKey()) {
set.add(id);
@ -31,7 +30,7 @@ public class TagHelper {
public static void addTag(Tag.Named<Item> tag, ItemLike... items) {
ResourceLocation tagID = tag.getName();
Set<ResourceLocation> set = TAGS_ITEM.computeIfAbsent(tagID, k -> Sets.newHashSet());
for (ItemLike item: items) {
for (ItemLike item : items) {
ResourceLocation id = Registry.ITEM.getKey(item.asItem());
if (id != Registry.ITEM.getDefaultKey()) {
set.add(id);
@ -41,14 +40,14 @@ public class TagHelper {
@SafeVarargs
public static void addTags(ItemLike item, Tag.Named<Item>... tags) {
for (Tag.Named<Item> tag: tags) {
for (Tag.Named<Item> tag : tags) {
addTag(tag, item);
}
}
@SafeVarargs
public static void addTags(Block block, Tag.Named<Block>... tags) {
for (Tag.Named<Block> tag: tags) {
for (Tag.Named<Block> tag : tags) {
addTag(tag, block);
}
}
@ -62,7 +61,8 @@ public class TagHelper {
Map<ResourceLocation, Set<ResourceLocation>> endTags = null;
if ("tags/blocks".equals(directory)) {
endTags = TAGS_BLOCK;
} else if ("tags/items".equals(directory)) {
}
else if ("tags/items".equals(directory)) {
endTags = TAGS_ITEM;
}
if (endTags != null) {

View file

@ -1,18 +1,17 @@
package ru.bclib.util;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.resources.ResourceLocation;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.resources.ResourceLocation;
public class TranslationHelper {
public static void printMissingNames(String modID) {
List<String> missingNamesEn = Lists.newArrayList();

View file

@ -14,6 +14,7 @@ public class WeighTree<T> {
/**
* Get eandom value from tree.
*
* @param random - {@link Random}.
* @return {@link T} value.
*/
@ -53,7 +54,7 @@ public class WeighTree<T> {
this.min = min;
this.max = max;
}
@Override
T get(float value) {
return value < separator ? min.get(value) : max.get(value);
@ -71,7 +72,7 @@ public class WeighTree<T> {
Leaf(T value) {
this.biome = value;
}
@Override
T get(float value) {
return biome;

View file

@ -12,6 +12,7 @@ public class WeightedList<T> {
/**
* Adds value with specified weight to the list
*
* @param value
* @param weight
*/
@ -23,6 +24,7 @@ public class WeightedList<T> {
/**
* Get random value.
*
* @param random - {@link Random}.
* @return {@link T} value.
*/
@ -41,6 +43,7 @@ public class WeightedList<T> {
/**
* Get value by index.
*
* @param index - {@code int} index.
* @return {@link T} value.
*/
@ -50,33 +53,37 @@ public class WeightedList<T> {
/**
* Get value weight. Weight is summed with all previous values weights.
*
* @param index - {@code int} index.
* @return {@code float} weight.
*/
public float getWeight(int index) {
return weights.get(index);
}
/**
* Chech if the list is empty.
*
* @return {@code true} if list is empty and {@code false} if not.
*/
public boolean isEmpty() {
return maxWeight == 0;
}
/**
* Get the list size.
*
* @return {@code int} list size.
*/
public int size() {
return values.size();
}
/**
* Makes a sublist of this list with same weights. Used only in {@link WeighTree}
*
* @param start - {@code int} start index (inclusive).
* @param end - {@code int} end index (exclusive).
* @param end - {@code int} end index (exclusive).
* @return {@link WeightedList}.
*/
protected WeightedList<T> subList(int start, int end) {
@ -87,26 +94,29 @@ public class WeightedList<T> {
}
return list;
}
/**
* Check if list contains certain value.
*
* @param value - {@link T} value.
* @return {@code true} if value is in list and {@code false} if not.
*/
public boolean contains(T value) {
return values.contains(value);
}
/**
* Applies {@link Consumer} to all values in list.
*
* @param function - {@link Consumer}.
*/
public void forEach(Consumer<T> function) {
values.forEach(function);
}
/**
* Get the maximum weight of the tree.
*
* @return {@code float} maximum weight.
*/
public float getMaxWeight() {