Code cleanup and refactoring before porting

This commit is contained in:
2023-05-10 21:21:16 +02:00
parent 1dec8d130c
commit 8e72212bf6
134 changed files with 975 additions and 755 deletions

View File

@@ -0,0 +1,22 @@
package com.hypherionmc.craterlib.util;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import java.util.function.ToIntFunction;
/**
* @author HypherionSA
* Helper class to create light levels from BlockState values
*/
public class BlockStateUtils {
public static ToIntFunction<BlockState> lightLevelFromLitBlockState(int litLevel) {
return state -> state.getValue(BlockStateProperties.LIT) ? litLevel : 0;
}
public static ToIntFunction<BlockState> lightLevelFromPoweredBlockState(int litLevel) {
return state -> state.getValue(BlockStateProperties.POWERED) ? litLevel : 0;
}
}

View File

@@ -0,0 +1,34 @@
package com.hypherionmc.craterlib.util;
import com.hypherionmc.craterlib.common.item.BlockItemDyable;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.item.ClampedItemPropertyFunction;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
/**
* @author HypherionSA
*/
public class ColorPropertyFunction implements ClampedItemPropertyFunction {
private final BlockItemDyable item;
public ColorPropertyFunction(BlockItemDyable item) {
this.item = item;
}
@Override
public float call(ItemStack itemStack, @Nullable ClientLevel clientLevel, @Nullable LivingEntity livingEntity, int i) {
return Mth.clamp(this.unclampedCall(itemStack, clientLevel, livingEntity, i), 0.0F, 15.0F);
}
@Override
public float unclampedCall(ItemStack itemStack, @Nullable ClientLevel clientLevel, @Nullable LivingEntity livingEntity, int i) {
DyeColor color = item.getColorFromNBT(itemStack);
return color.getId();
}
}

View File

@@ -0,0 +1,31 @@
package com.hypherionmc.craterlib.util;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
/**
* @author HypherionSA
* Utility class for interacting with fluids across modloaders
*/
public class FluidUtils {
public static int fluidColorFromDye(DyeColor color) {
return color.getMaterialColor().col | 0xFF000000;
}
public static void putFluid(CompoundTag compound, String key, Fluid fluidVariant) {
compound.putString("tankFluid", BuiltInRegistries.FLUID.getKey(fluidVariant).toString());
}
public static Fluid getFluidCompatible(CompoundTag tag) {
if (tag == null || !tag.contains("tankFluid"))
return Fluids.EMPTY;
return BuiltInRegistries.FLUID.get(new ResourceLocation(tag.getString("tankFluid")));
}
}

View File

@@ -0,0 +1,28 @@
package com.hypherionmc.craterlib.util;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
/**
* @author HypherionSA
* Utility class to handle Translation Keys and Formatting
*/
public class LangUtils {
public static Component getTooltipTitle(String key) {
return Component.literal(ChatFormatting.YELLOW + Component.translatable(key).getString());
}
public static String resolveTranslation(String key) {
return Component.translatable(key).getString();
}
public static Component getTranslation(String key) {
return Component.translatable(key);
}
public static Component makeComponent(String text) {
return Component.translatable(text);
}
}

View File

@@ -0,0 +1,41 @@
package com.hypherionmc.craterlib.util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
/**
* @author HypherionSA
* Utility class to handle various mathematical functions
*/
public class MathUtils {
public static VoxelShape rotateShape(Direction from, Direction to, VoxelShape shape) {
VoxelShape[] buffer = new VoxelShape[]{shape, Shapes.empty()};
int times = (to.ordinal() - from.ordinal() + 4) % 4;
for (int i = 0; i < times; i++) {
buffer[0].forAllBoxes((minX, minY, minZ, maxX, maxY, maxZ) -> buffer[1] = Shapes.or(buffer[1], Shapes.box(1 - maxZ, minY, minX, 1 - minZ, maxY, maxX)));
buffer[0] = buffer[1];
buffer[1] = Shapes.empty();
}
return buffer[0];
}
public static void writeBlockPosToNBT(BlockPos pos, CompoundTag tag) {
tag.putInt("block_x", pos.getX());
tag.putInt("block_y", pos.getY());
tag.putInt("block_z", pos.getZ());
}
public static BlockPos readBlockPosFromNBT(CompoundTag tag) {
int x, y, z;
x = tag.getInt("block_x");
y = tag.getInt("block_y");
z = tag.getInt("block_z");
return new BlockPos(x, y, z);
}
}

View File

@@ -0,0 +1,46 @@
package com.hypherionmc.craterlib.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author HypherionSA
* Utility class for Optifine compatibility
*/
public class OptifineUtils {
private static final boolean hasOptifine = checkOptifine();
private static boolean checkOptifine() {
try {
Class<?> ofConfigClass = Class.forName("net.optifine.Config");
return true;
} catch (ClassNotFoundException e) {
// Optifine is probably not present. Ignore the error
return false;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static boolean isRenderRegions() {
try {
Class<?> ofConfigClass = Class.forName("net.optifine.Config");
Method rrField = ofConfigClass.getMethod("isRenderRegions");
return (boolean) rrField.invoke(null);
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
IllegalAccessException e) {
// Optifine is probably not present. Ignore the error
return false;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static boolean hasOptifine() {
return hasOptifine;
}
}

View File

@@ -0,0 +1,70 @@
package com.hypherionmc.craterlib.util;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.DyeColor;
import org.joml.Vector4f;
import java.awt.*;
/**
* @author HypherionSA
* Utility class for various rendering functions
*/
public class RenderUtils {
public static Vector4f colorIntToRGBA(int color) {
float a = 1.0F;
float r = (color >> 16 & 0xFF) / 255.0F;
float g = (color >> 8 & 0xFF) / 255.0F;
float b = (color & 0xFF) / 255.0F;
return new Vector4f(r, g, b, a);
}
public static Component getFluidAmount(long amount, long capacity) {
amount = amount / 81;
capacity = capacity / 81;
String text = "" + (int) (((float) amount / capacity) * 100);
return amount > 0 ? Component.literal(ChatFormatting.AQUA + text + "%") : Component.literal(text + "%");
}
public static Component getTimeDisplayString(double value) {
long seconds = Math.round((value / 20));
long minutes = Math.round(seconds / 60);
if (seconds >= 60) {
String appendString = (minutes == 1) ? "Minute" : "Minutes";
String doSeconds = ((seconds - (minutes * 60)) > 0) ? ", " + (seconds - (minutes * 60)) + " Seconds" : "";
return Component.literal(minutes + " " + appendString + doSeconds);
} else {
return Component.literal(seconds + " Seconds");
}
}
public static class ARGB32 {
public static int alpha(int pPackedColor) {
return pPackedColor >>> 24;
}
public static int red(int pPackedColor) {
return pPackedColor >> 16 & 255;
}
public static int green(int pPackedColor) {
return pPackedColor >> 8 & 255;
}
public static int blue(int pPackedColor) {
return pPackedColor & 255;
}
}
public static int renderColorFromDye(DyeColor color) {
return color.getMaterialColor().col | 0xFF000000;
}
public static int alphaColorFromDye(DyeColor color, float alpha) {
float[] colors = color.getTextureDiffuseColors();
return new Color(colors[0], colors[1], colors[2], alpha).getRGB();
}
}