Scissors/patches/removed/0021-Add-custom-classes-use...

211 lines
6.9 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Telesphoreo <me@telesphoreo.me>
Date: Sat, 11 Jun 2022 22:56:59 -0500
Subject: [PATCH] Add custom classes used by Scissors
diff --git a/src/main/java/me/totalfreedom/scissors/MathUtility.java b/src/main/java/me/totalfreedom/scissors/MathUtility.java
new file mode 100644
index 0000000000000000000000000000000000000000..754b578b575137a9c48cb20dee965a9388fedb3c
--- /dev/null
+++ b/src/main/java/me/totalfreedom/scissors/MathUtility.java
@@ -0,0 +1,29 @@
+package me.totalfreedom.scissors;
+
+public class MathUtility
+{
+ public static int clampInt(int number, int minimum, int maximum)
+ {
+ return Math.min(Math.max(number, minimum), maximum);
+ }
+
+ public static long clampLong(long number, long minimum, long maximum)
+ {
+ return Math.min(Math.max(number, minimum), maximum);
+ }
+
+ public static double clampDouble(double number, double minimum, double maximum)
+ {
+ return Math.min(Math.max(number, minimum), maximum);
+ }
+
+ public static int safeDoubleToInt(double number)
+ {
+ return (int) clampDouble(number, Integer.MIN_VALUE, Integer.MAX_VALUE);
+ }
+
+ public static int safeLongToInt(long number)
+ {
+ return (int) clampLong(number, Integer.MIN_VALUE, Integer.MAX_VALUE);
+ }
+}
diff --git a/src/main/java/me/totalfreedom/scissors/NbtUtility.java b/src/main/java/me/totalfreedom/scissors/NbtUtility.java
new file mode 100644
index 0000000000000000000000000000000000000000..b724baaef8d565e41db1af6393d0890e919a5aa8
--- /dev/null
+++ b/src/main/java/me/totalfreedom/scissors/NbtUtility.java
@@ -0,0 +1,74 @@
+package me.totalfreedom.scissors;
+
+import java.nio.charset.StandardCharsets;
+import javax.annotation.Nullable;
+import net.minecraft.nbt.CompoundTag;
+import net.minecraft.nbt.ListTag;
+import net.minecraft.nbt.Tag;
+
+public class NbtUtility
+{
+ public static final long MAXIMUM_SIZE = (256 * 1024);
+
+ public static long getTagSize(@Nullable Tag tag, int depth)
+ {
+ if (depth > 512)
+ {
+ return 0;
+ }
+ if (tag == null)
+ {
+ return 0;
+ }
+
+ long size = 0;
+
+ if (tag.getType() == CompoundTag.TYPE)
+ {
+ CompoundTag compoundTag = (CompoundTag) tag;
+ for (String key : compoundTag.getAllKeys())
+ {
+ size += getTagSize(compoundTag.get(key), depth + 1);
+ }
+ }
+ else if (tag.getType() == ListTag.TYPE)
+ {
+ ListTag listTag = (ListTag) tag;
+ for (Tag tag1 : listTag)
+ {
+ size += getTagSize(tag1, depth + 1);
+ }
+ }
+ else
+ {
+ size += tag.getAsString().getBytes(StandardCharsets.UTF_8).length;
+ }
+
+ return size;
+ }
+
+ public static long getTagSize(@Nullable CompoundTag tag)
+ {
+ return getTagSize(tag, 0);
+ }
+
+ public static boolean isTooLarge(@Nullable CompoundTag tag)
+ {
+ if (tag == null)
+ {
+ return false;
+ }
+ return getTagSize(tag) > MAXIMUM_SIZE;
+ }
+
+ public static class Item
+ {
+ public static CompoundTag removeItemData(CompoundTag tag)
+ {
+ CompoundTag cleaned = new CompoundTag();
+ cleaned.putString("id", tag.getString("id"));
+ cleaned.putByte("Count", tag.getByte("Count"));
+ return cleaned;
+ }
+ }
+}
diff --git a/src/main/java/me/totalfreedom/scissors/PositionUtility.java b/src/main/java/me/totalfreedom/scissors/PositionUtility.java
new file mode 100644
index 0000000000000000000000000000000000000000..9e33ad84e50c7e2491aa883f905323f3ad2b070c
--- /dev/null
+++ b/src/main/java/me/totalfreedom/scissors/PositionUtility.java
@@ -0,0 +1,83 @@
+package me.totalfreedom.scissors;
+
+import net.minecraft.core.BlockPos;
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.level.Level;
+import net.minecraft.world.level.border.WorldBorder;
+import net.minecraft.world.phys.Vec3;
+
+public class PositionUtility
+{
+ public static Vec3 getValidVec3(double x, double y, double z, Entity entity)
+ {
+ final Level level = entity.level;
+
+ try
+ {
+ if (level.isInWorldBounds(new BlockPos(Math.floor(MathUtility.safeDoubleToInt(x)), Math.floor(MathUtility.safeDoubleToInt(y)), Math.floor(MathUtility.safeDoubleToInt(z)))))
+ {
+ return new Vec3(x, y, z);
+ }
+ else
+ {
+ final WorldBorder worldBorder = level.getWorldBorder();
+
+ final double maxX = worldBorder.getMaxX();
+ final double maxY = level.getMaxBuildHeight();
+ final double maxZ = worldBorder.getMaxZ();
+
+ final double minX = worldBorder.getMinX();
+ final double minY = level.getMinBuildHeight();
+ final double minZ = worldBorder.getMinZ();
+
+ return new Vec3(MathUtility.clampDouble(x, minX, maxX), MathUtility.clampDouble(y, minY, maxY), MathUtility.clampDouble(z, minZ, maxZ));
+ }
+ }
+ catch (Exception e)
+ { // If we throw some sort of exception due to the position being crazy, catch it
+ return new Vec3(0, 0, 0);
+ }
+ }
+
+ public static Vec3 getValidVec3FromBlockPos(BlockPos blockPos, Entity entity)
+ {
+ final BlockPos validBlockPos = getValidBlockPos(blockPos, entity);
+
+ return new Vec3(validBlockPos.getX(), validBlockPos.getY(), validBlockPos.getZ());
+ }
+
+ public static BlockPos getValidBlockPos(BlockPos blockPos, Entity entity)
+ {
+ final Level level = entity.level;
+
+ try
+ {
+ if (level.isInWorldBounds(blockPos))
+ {
+ return blockPos;
+ }
+ else
+ {
+ final int x = blockPos.getX();
+ final int y = blockPos.getY();
+ final int z = blockPos.getZ();
+
+ final WorldBorder worldBorder = level.getWorldBorder();
+
+ final int maxX = MathUtility.safeDoubleToInt(worldBorder.getMaxX());
+ final int maxY = level.getMaxBuildHeight();
+ final int maxZ = MathUtility.safeDoubleToInt(worldBorder.getMaxZ());
+
+ final int minX = MathUtility.safeDoubleToInt(worldBorder.getMinX());
+ final int minY = level.getMinBuildHeight();
+ final int minZ = MathUtility.safeDoubleToInt(worldBorder.getMinZ());
+
+ return new BlockPos(MathUtility.clampInt(x, minX, maxX), MathUtility.clampInt(y, minY, maxY), MathUtility.clampInt(z, minZ, maxZ));
+ }
+ }
+ catch (Exception e)
+ { // If we throw some sort of exception due to the position being crazy, catch it
+ return new BlockPos(0, 0, 0);
+ }
+ }
+}