From 81286767f4914be38d8f0157b7d4a4882388049b Mon Sep 17 00:00:00 2001 From: zontreck Date: Sun, 7 Apr 2024 15:53:04 -0700 Subject: [PATCH] Migrate vectors to a dedicated API interface --- .../dev/zontreck/libzontreck/LibZontreck.java | 2 - .../dev/zontreck/libzontreck/api/Vector2.java | 126 ++++++++ .../dev/zontreck/libzontreck/api/Vector3.java | 143 +++++++++ .../libzontreck/chestgui/ChestGUI.java | 8 +- .../libzontreck/chestgui/ChestGUIButton.java | 2 - .../zontreck/libzontreck/util/BlocksUtil.java | 6 +- .../libzontreck/util/PositionUtil.java | 115 +++++++ .../libzontreck/util/ServerUtilities.java | 6 +- .../libzontreck/vectors/ChunkPos.java | 10 +- .../libzontreck/vectors/NonAbsVector3.java | 2 +- .../zontreck/libzontreck/vectors/Points.java | 10 +- .../zontreck/libzontreck/vectors/Vector2.java | 120 -------- .../libzontreck/vectors/Vector2d.java | 176 +++++++++++ .../libzontreck/vectors/Vector2i.java | 124 ++++++-- .../zontreck/libzontreck/vectors/Vector3.java | 285 ------------------ .../libzontreck/vectors/Vector3d.java | 222 ++++++++++++++ .../libzontreck/vectors/Vector3i.java | 215 +++++++++++++ .../libzontreck/vectors/WorldPosition.java | 21 +- 18 files changed, 1124 insertions(+), 469 deletions(-) create mode 100644 src/main/java/dev/zontreck/libzontreck/api/Vector2.java create mode 100644 src/main/java/dev/zontreck/libzontreck/api/Vector3.java create mode 100644 src/main/java/dev/zontreck/libzontreck/util/PositionUtil.java delete mode 100644 src/main/java/dev/zontreck/libzontreck/vectors/Vector2.java create mode 100644 src/main/java/dev/zontreck/libzontreck/vectors/Vector2d.java delete mode 100644 src/main/java/dev/zontreck/libzontreck/vectors/Vector3.java create mode 100644 src/main/java/dev/zontreck/libzontreck/vectors/Vector3d.java create mode 100644 src/main/java/dev/zontreck/libzontreck/vectors/Vector3i.java diff --git a/src/main/java/dev/zontreck/libzontreck/LibZontreck.java b/src/main/java/dev/zontreck/libzontreck/LibZontreck.java index 0a5190e..11a53a7 100644 --- a/src/main/java/dev/zontreck/libzontreck/LibZontreck.java +++ b/src/main/java/dev/zontreck/libzontreck/LibZontreck.java @@ -44,7 +44,6 @@ public class LibZontreck { public static final Logger LOGGER = LogUtils.getLogger(); public static final String MOD_ID = "libzontreck"; public static final Map PROFILES; - public static MinecraftServer THE_SERVER; public static VolatilePlayerStorage playerStorage; public static boolean ALIVE=true; public static final String FILESTORE = FileTreeDatastore.get(); @@ -107,7 +106,6 @@ public class LibZontreck { @SubscribeEvent public void onServerStarted(final ServerStartedEvent event) { - THE_SERVER = event.getServer(); ALIVE=true; CURRENT_SIDE = LogicalSide.SERVER; } diff --git a/src/main/java/dev/zontreck/libzontreck/api/Vector2.java b/src/main/java/dev/zontreck/libzontreck/api/Vector2.java new file mode 100644 index 0000000..670f786 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/api/Vector2.java @@ -0,0 +1,126 @@ +package dev.zontreck.libzontreck.api; + +import dev.zontreck.libzontreck.vectors.Vector2d; +import dev.zontreck.libzontreck.vectors.Vector2i; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.phys.Vec2; + +public interface Vector2 +{ + /** + * Converts the current Vector2 representation into a minecraft Vec2 + * @return Minecraft equivalent Vec2 + */ + Vec2 asMinecraftVector(); + + /** + * Parses a string in serialized format. + * @param vector2 Expects it in the same format returned by Vector2#toString + * @return New Vector2, or a null Vector2 initialized with zeros if invalid data + */ + static Vector2 parseString(String vector2){ + throw new UnsupportedOperationException("This method is not implemented by this implementation"); + } + + /** + * Copies the values to a new and detached instance + * @return New Vector2 + */ + Vector2 Clone(); + + /** + * Saves the X and Y positions to a NBT tag + * @return NBT compound tag + */ + CompoundTag serialize(); + + /** + * Loads a Vector2 from a NBT tag + * @param tag The NBT tag to load + */ + static Vector2 deserialize(CompoundTag tag) + { + throw new UnsupportedOperationException("This method is not implemented by this implementation"); + } + + /** + * Compares the two vector2 instances + * @param other The position to check + * @return True if same position + */ + boolean Same(Vector2 other); + + /** + * True if the current position is inside the two points + * @param point1 Lowest point + * @param point2 Hightest Point + * @return True if inside + */ + boolean Inside(Vector2 point1, Vector2 point2); + + /** + * Converts, if necessary, to Vector2d + * @return A vector2d instance + */ + Vector2d asVector2d(); + + /** + * Converts, if necessary, to Vector2i + * @return A vector2i instance + */ + Vector2i asVector2i(); + + /** + * Checks if the current vector is greater than the provided one + * @param other The other vector to check + * @return True if greater + */ + boolean greater(Vector2 other); + + /** + * Checks if the current vector is less than the provided one + * @param other The vector to check + * @return True if less than other + */ + boolean less(Vector2 other); + + /** + * Alias for Vector2#same + * @param other Vector to check + * @return True if same position + */ + boolean equal(Vector2 other); + + /** + * Adds the two vectors together + * @param other Vector to add + * @return New instance after adding the other vector + */ + Vector2 add(Vector2 other); + + /** + * Subtracts the other vector from this one + * @param other Vector to subtract + * @return New instance after subtracting + */ + Vector2 subtract(Vector2 other); + + /** + * Calculates the distance between the two vectors + * @param other + * @return The distance + */ + double distance(Vector2 other); + + /** + * Increments the Y axis by 1 + * @return New instance + */ + Vector2 moveUp(); + + /** + * Decrements the Y axis by 1 + * @return New instance + */ + Vector2 moveDown(); +} diff --git a/src/main/java/dev/zontreck/libzontreck/api/Vector3.java b/src/main/java/dev/zontreck/libzontreck/api/Vector3.java new file mode 100644 index 0000000..f7679f5 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/api/Vector3.java @@ -0,0 +1,143 @@ +package dev.zontreck.libzontreck.api; + +import dev.zontreck.libzontreck.vectors.Vector2d; +import dev.zontreck.libzontreck.vectors.Vector2i; +import dev.zontreck.libzontreck.vectors.Vector3d; +import dev.zontreck.libzontreck.vectors.Vector3i; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Vec3i; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.phys.Vec2; +import net.minecraft.world.phys.Vec3; + +public interface Vector3 +{ + /** + * Converts the current Vector3 representation into a minecraft Vec3 + * @return Minecraft equivalent Vec3 + */ + Vec3 asMinecraftVector(); + + /** + * Converts to a vec3i position + * @return Equivalent vec3i + */ + Vec3i asVec3i(); + + /** + * Converts to a block position + * @return Equivalent block position + */ + BlockPos asBlockPos(); + + /** + * Parses a string in serialized format. + * @param vector3 Expects it in the same format returned by Vector3#toString + * @return New Vector3, or a null Vector3 initialized with zeros if invalid data + */ + static Vector3 parseString(String vector3){ + throw new UnsupportedOperationException("This method is not implemented by this implementation"); + } + + /** + * Copies the values to a new and detached instance + * @return New Vector3 + */ + Vector3 Clone(); + + /** + * Saves the X, Y, and Z positions to a NBT tag + * @return NBT compound tag + */ + CompoundTag serialize(); + + /** + * Loads a Vector3 from a NBT tag + * @param tag The NBT tag to load + */ + static Vector3 deserialize(CompoundTag tag) + { + throw new UnsupportedOperationException("This method is not implemented by this implementation"); + } + + /** + * Compares the two vector3 instances + * @param other The position to check + * @return True if same position + */ + boolean Same(Vector3 other); + + /** + * True if the current position is inside the two points + * @param point1 Lowest point + * @param point2 Hightest Point + * @return True if inside + */ + boolean Inside(Vector3 point1, Vector3 point2); + + /** + * Converts, if necessary, to Vector3d + * @return A vector2d instance + */ + Vector3d asVector3d(); + + /** + * Converts, if necessary, to Vector3i + * @return A vector3i instance + */ + Vector3i asVector3i(); + + /** + * Checks if the current vector is greater than the provided one + * @param other The other vector to check + * @return True if greater + */ + boolean greater(Vector3 other); + + /** + * Checks if the current vector is less than the provided one + * @param other The vector to check + * @return True if less than other + */ + boolean less(Vector3 other); + + /** + * Alias for Vector3#same + * @param other Vector to check + * @return True if same position + */ + boolean equal(Vector3 other); + + /** + * Adds the two vectors together + * @param other Vector to add + * @return New instance after adding the other vector + */ + Vector3 add(Vector3 other); + + /** + * Subtracts the other vector from this one + * @param other Vector to subtract + * @return New instance after subtracting + */ + Vector3 subtract(Vector3 other); + + /** + * Calculates the distance between the two vectors + * @param other + * @return The distance + */ + double distance(Vector3 other); + + /** + * Increments the Y axis by 1 + * @return New instance + */ + Vector3 moveUp(); + + /** + * Decrements the Y axis by 1 + * @return New instance + */ + Vector3 moveDown(); +} diff --git a/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUI.java b/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUI.java index 8e41c10..ed1cd74 100644 --- a/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUI.java +++ b/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUI.java @@ -9,10 +9,8 @@ import dev.zontreck.libzontreck.networking.ModMessages; import dev.zontreck.libzontreck.networking.packets.S2CCloseChestGUI; import dev.zontreck.libzontreck.util.ChatHelpers; import dev.zontreck.libzontreck.util.ServerUtilities; -import dev.zontreck.libzontreck.vectors.Vector2; +import dev.zontreck.libzontreck.vectors.Vector2d; import dev.zontreck.libzontreck.vectors.Vector2i; -import net.minecraft.network.chat.Component; -import net.minecraft.resources.ResourceLocation; import net.minecraft.world.SimpleMenuProvider; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; @@ -24,8 +22,6 @@ import net.minecraftforge.network.NetworkHooks; import java.util.ArrayList; import java.util.List; import java.util.UUID; -import java.util.concurrent.Callable; -import java.util.function.Function; /** * Zontreck's ChestGUI Interface @@ -232,7 +228,7 @@ public class ChestGUI return this.id.equals(id); } - public void handleButtonClicked(int slot, Vector2 pos, Item item) { + public void handleButtonClicked(int slot, Vector2d pos, Item item) { for(ChestGUIButton button : buttons) { if(button.getSlotNum() == slot) diff --git a/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIButton.java b/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIButton.java index 4368831..3e7cb91 100644 --- a/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIButton.java +++ b/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIButton.java @@ -3,10 +3,8 @@ package dev.zontreck.libzontreck.chestgui; import dev.zontreck.libzontreck.lore.LoreContainer; import dev.zontreck.libzontreck.lore.LoreEntry; import dev.zontreck.libzontreck.util.ChatHelpers; -import dev.zontreck.libzontreck.vectors.Vector2; import dev.zontreck.libzontreck.vectors.Vector2i; import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.NbtUtils; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraftforge.items.ItemStackHandler; diff --git a/src/main/java/dev/zontreck/libzontreck/util/BlocksUtil.java b/src/main/java/dev/zontreck/libzontreck/util/BlocksUtil.java index 4439147..92edc5e 100644 --- a/src/main/java/dev/zontreck/libzontreck/util/BlocksUtil.java +++ b/src/main/java/dev/zontreck/libzontreck/util/BlocksUtil.java @@ -1,6 +1,6 @@ package dev.zontreck.libzontreck.util; -import dev.zontreck.libzontreck.vectors.Vector3; +import dev.zontreck.libzontreck.vectors.Vector3d; import net.minecraft.server.level.ServerLevel; import java.util.ArrayList; @@ -18,9 +18,9 @@ public class BlocksUtil * @param limit The applicable limit for vein detection * @return List of positions for the vein */ - public static List VeinOf(ServerLevel level, Vector3 start, int limit) + public static List VeinOf(ServerLevel level, Vector3d start, int limit) { - List ret = new ArrayList<>(); + List ret = new ArrayList<>(); diff --git a/src/main/java/dev/zontreck/libzontreck/util/PositionUtil.java b/src/main/java/dev/zontreck/libzontreck/util/PositionUtil.java new file mode 100644 index 0000000..332deae --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/util/PositionUtil.java @@ -0,0 +1,115 @@ +package dev.zontreck.libzontreck.util; + +import dev.zontreck.libzontreck.api.Vector3; +import dev.zontreck.libzontreck.vectors.Vector3d; + +import java.util.ArrayList; +import java.util.List; + +/** + * Provides helper functions for position related things + */ +public class PositionUtil +{ + + public List makeCube(Vector3 p1, Vector3 p2) + { + List vecs = new ArrayList<>(); + Vector3 work = new Vector3d(); + Vector3d v1 = p1.asVector3d(); + Vector3d v2 = p2.asVector3d(); + + double xx = v1.x; + double yy = v1.y; + double zz = v1.z; + + int yState = 0; + int zState = 0; + int xState = 0; + + for(xx = Math.round(v1.x); (xx != Math.round(v2.x) && xState != 2);) + { + for(zz = Math.round(v1.z); (zz != Math.round(v2.z) && zState != 2);) + { + for(yy = Math.round(v1.y); (yy != Math.round(v2.y) && yState != 2);) + { + work = new Vector3d(xx, yy, zz); + + if(!vecs.contains(work)) vecs.add(work); + + if(yy > v2.y) + { + yy -= 1.0; + if(yy == Math.round(v2.y) && yState == 0) + { + yState++; + }else{ + if(yState == 1) + { + yState ++; + } + } + } else if(yy < v2.y) + { + yy += 1.0; + if(yy == Math.round(v2.y) && yState == 0){ + yState ++; + }else { + if(yState == 1)yState++; + } + } + } + + yState=0; + work = new Vector3d(xx,yy,zz); + + if(!vecs.contains(work)) vecs.add(work); + + if(zz > v2.z) + { + zz -= 1.0; + + if(zz == Math.round(v2.z) && zState == 0)zState++; + else{ + if(zState == 1)zState++; + } + }else if(zz < v2.z) + { + zz += 1.0; + + if(zz == Math.round(v2.z) && zState == 0)zState++; + else { + if(zState==1)zState++; + } + } + } + + zState=0; + work = new Vector3d(xx,yy,zz); + + if(!vecs.contains(work)) vecs.add(work); + + if(xx > v2.x) + { + xx -= 1.0; + + if(xx == Math.round(v2.x) && xState == 0) xState++; + else{ + if(xState == 1)xState++; + } + }else if(xx < v2.x) + { + xx += 1.0; + + if(xx == Math.round(v2.x) && xState==0)xState++; + else{ + if(xState==1)xState++; + } + } + } + + return vecs; + } + + +} diff --git a/src/main/java/dev/zontreck/libzontreck/util/ServerUtilities.java b/src/main/java/dev/zontreck/libzontreck/util/ServerUtilities.java index abd8d57..8f35d3b 100644 --- a/src/main/java/dev/zontreck/libzontreck/util/ServerUtilities.java +++ b/src/main/java/dev/zontreck/libzontreck/util/ServerUtilities.java @@ -1,5 +1,6 @@ package dev.zontreck.libzontreck.util; +import java.util.List; import java.util.UUID; import java.util.function.Function; import java.util.function.Supplier; @@ -13,6 +14,7 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.simple.SimpleChannel; +import net.minecraftforge.server.ServerLifecycleHooks; public class ServerUtilities { @@ -23,7 +25,7 @@ public class ServerUtilities */ public static ServerPlayer getPlayerByID(String id) { - return LibZontreck.THE_SERVER.getPlayerList().getPlayer(UUID.fromString(id)); + return ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayer(UUID.fromString(id)); } /** @@ -79,7 +81,7 @@ public class ServerUtilities public static boolean playerIsOffline(UUID ID) throws InvalidSideException { if(isClient())throw new InvalidSideException("This can only be called on the server"); - if(LibZontreck.THE_SERVER.getPlayerList().getPlayer(ID) == null) return true; + if(ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayer(ID) == null) return true; else return false; } diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/ChunkPos.java b/src/main/java/dev/zontreck/libzontreck/vectors/ChunkPos.java index 9cb8fc6..e88fba8 100644 --- a/src/main/java/dev/zontreck/libzontreck/vectors/ChunkPos.java +++ b/src/main/java/dev/zontreck/libzontreck/vectors/ChunkPos.java @@ -5,10 +5,10 @@ import net.minecraft.server.level.ServerLevel; public class ChunkPos { public Points points; - public Vector2 centerPoints; + public Vector2d centerPoints; public String dim; - public ChunkPos(Vector3 point1, Vector3 point2, ServerLevel lvl) + public ChunkPos(Vector3d point1, Vector3d point2, ServerLevel lvl) { points = new Points(point1, point2, lvl); dim = WorldPosition.getDim(lvl); @@ -17,12 +17,12 @@ public class ChunkPos { public ChunkPos(CompoundTag tag) { points = new Points(tag.getCompound("points")); - centerPoints = new Vector2(tag.getCompound("center")); + centerPoints = Vector2d.deserialize(tag.getCompound("center")); } - public boolean isWithin(Vector3 point) + public boolean isWithin(Vector3d point) { - return point.inside(points.Min, points.Max); + return point.Inside(points.Min, points.Max); } public static ChunkPos getChunkPos(WorldPosition pos) diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/NonAbsVector3.java b/src/main/java/dev/zontreck/libzontreck/vectors/NonAbsVector3.java index d484e1b..bce839f 100644 --- a/src/main/java/dev/zontreck/libzontreck/vectors/NonAbsVector3.java +++ b/src/main/java/dev/zontreck/libzontreck/vectors/NonAbsVector3.java @@ -11,7 +11,7 @@ public class NonAbsVector3 public long y; public long z; - public NonAbsVector3(Vector3 origin) + public NonAbsVector3(Vector3d origin) { x = Math.round(origin.x); y = Math.round(origin.y); diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/Points.java b/src/main/java/dev/zontreck/libzontreck/vectors/Points.java index fdb0cf9..93fba5f 100644 --- a/src/main/java/dev/zontreck/libzontreck/vectors/Points.java +++ b/src/main/java/dev/zontreck/libzontreck/vectors/Points.java @@ -7,8 +7,8 @@ import net.minecraft.server.level.ServerLevel; * Two points within the same dimension */ public class Points { - public Vector3 Min = Vector3.ZERO; - public Vector3 Max = Vector3.ZERO; + public Vector3d Min = Vector3d.ZERO; + public Vector3d Max = Vector3d.ZERO; public String dimension = ""; /** @@ -17,7 +17,7 @@ public class Points { * @param max * @param lvl */ - public Points(Vector3 min, Vector3 max, ServerLevel lvl) + public Points(Vector3d min, Vector3d max, ServerLevel lvl) { dimension = WorldPosition.getDimSafe(lvl); if(min.less(max)) @@ -48,8 +48,8 @@ public class Points { public void deserialize(CompoundTag tag) { - Min = new Vector3(tag.getCompound("min")); - Max = new Vector3(tag.getCompound("max")); + Min = Vector3d.deserialize(tag.getCompound("min")); + Max = Vector3d.deserialize(tag.getCompound("max")); dimension = tag.getString("dim"); } } diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/Vector2.java b/src/main/java/dev/zontreck/libzontreck/vectors/Vector2.java deleted file mode 100644 index 100ae27..0000000 --- a/src/main/java/dev/zontreck/libzontreck/vectors/Vector2.java +++ /dev/null @@ -1,120 +0,0 @@ -package dev.zontreck.libzontreck.vectors; - -import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.phys.Vec2; - -public class Vector2 -{ - public static final Vector2 ZERO = new Vector2(0, 0); - - public float x; - public float y; - - public Vec2 asMinecraftVector(){ - return new Vec2(x, y); - } - - public Vector2() - { - - } - - public Vector2(float x, float y) - { - this.x=x; - this.y=y; - } - - public Vector2(Vec2 pos) - { - x=pos.x; - y=pos.y; - } - - public Vector2(String pos) throws InvalidDeserialization - { - // This will be serialized most likely from the ToString method - // Parse - if(pos.startsWith("<")) - { - pos=pos.substring(1, pos.length()-1); // Rip off the ending bracket too - String[] positions = pos.split(", "); - if(positions.length!=2) - { - positions = pos.split(","); - } - - if(positions.length!=2) - { - throw new InvalidDeserialization("Positions must be in the same format provided by ToString() (ex. <1,1> or <1, 1>"); - } - - this.x = Float.parseFloat(positions[0]); - this.y = Float.parseFloat(positions[1]); - // We are done now - } - } - - public Vector2 Clone() - { - Vector2 n = new Vector2(x, y); - return n; - } - - @Override - public String toString() - { - return "<"+String.valueOf(x)+", "+String.valueOf(y) + ">"; - } - - - public CompoundTag serialize() - { - CompoundTag tag = new CompoundTag(); - tag.putFloat("x", x); - tag.putFloat("y", y); - - return tag; - } - - public Vector2(CompoundTag tag) { - this.deserialize(tag); - } - public void deserialize(CompoundTag tag) - { - x=tag.getFloat("x"); - y=tag.getFloat("y"); - } - - public boolean same(Vector2 other) - { - if(x == other.x && y==other.y)return true; - else return false; - } - - public boolean inside(Vector2 point1, Vector2 point2) - { - if(point1.x <= x && point2.x >= x){ - if(point1.y <= y && point2.y >= y) - { - return true; - } - } - - return false; - } - - public boolean greater(Vector2 other) - { - return ((x>other.x) && (y>other.y)); - } - public boolean less(Vector2 other) - { - return ((x>other.x) && (y>other.y)); - } - public boolean equal(Vector2 other) - { - return same(other); - } -} diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/Vector2d.java b/src/main/java/dev/zontreck/libzontreck/vectors/Vector2d.java new file mode 100644 index 0000000..17b50bd --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/vectors/Vector2d.java @@ -0,0 +1,176 @@ +package dev.zontreck.libzontreck.vectors; + +import dev.zontreck.libzontreck.api.Vector2; +import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.phys.Vec2; + +public class Vector2d implements Vector2 +{ + public static final Vector2d ZERO = new Vector2d(0, 0); + + public float x; + public float y; + + @Override + public Vec2 asMinecraftVector(){ + return new Vec2(x, y); + } + + public Vector2d() + { + + } + + public Vector2d(float x, float y) + { + this.x=x; + this.y=y; + } + + public Vector2d(Vec2 pos) + { + x=pos.x; + y=pos.y; + } + + public static Vector2d parseString(String vector2) + { + Vector2d vec = new Vector2d(); + // This will be serialized most likely from the ToString method + // Parse + if(vector2.startsWith("<")) + { + vector2=vector2.substring(1, vector2.length()-1); // Rip off the ending bracket too + String[] positions = vector2.split(", "); + if(positions.length!=2) + { + positions = vector2.split(","); + } + + if(positions.length!=2) + { + return ZERO; + } + + vec.x = Float.parseFloat(positions[0]); + vec.y = Float.parseFloat(positions[1]); + // We are done now + } + + return vec; + } + + @Override + public Vector2d Clone() + { + Vector2d n = new Vector2d(x, y); + return n; + } + + @Override + public String toString() + { + return "<"+String.valueOf(x)+", "+String.valueOf(y) + ">"; + } + + @Override + public CompoundTag serialize() + { + CompoundTag tag = new CompoundTag(); + tag.putFloat("x", x); + tag.putFloat("y", y); + + return tag; + } + + public static Vector2d deserialize(CompoundTag tag) + { + Vector2d vec = new Vector2d(); + vec.x=tag.getFloat("x"); + vec.y=tag.getFloat("y"); + + return vec; + } + + @Override + public boolean Same(Vector2 other) + { + Vector2d ov = other.asVector2d(); + if(x == ov.x && y == ov.y) return true; + return false; + } + + @Override + public boolean Inside(Vector2 point1, Vector2 point2) + { + Vector2d p1 = point1.asVector2d(); + Vector2d p2 = point2.asVector2d(); + + if(p1.x <= x && p2.x >= x){ + if(p1.y <= y && p2.y >= y) + { + return true; + } + } + + return false; + } + + @Override + public Vector2d asVector2d() + { + return this; + } + + @Override + public Vector2i asVector2i() { + return new Vector2i(Math.round(x), Math.round(y)); + } + + @Override + public boolean greater(Vector2 other) + { + Vector2d vec = other.asVector2d(); + return ((x > vec.x) && (y > vec.y)); + } + + @Override + public boolean less(Vector2 other) + { + Vector2d vec = other.asVector2d(); + return ((x > vec.x) && (y > vec.y)); + } + public boolean equal(Vector2 other) + { + return Same(other); + } + + @Override + public Vector2d add(Vector2 other) { + Vector2d vec = other.asVector2d(); + return new Vector2d(x + vec.x, y + vec.y); + } + + @Override + public Vector2d subtract(Vector2 other) { + Vector2d vec = other.asVector2d(); + return new Vector2d(x - vec.x, y - vec.y); + } + + @Override + public double distance(Vector2 other) { + Vector2d vec = subtract(other); + return Math.sqrt((vec.x * vec.x + vec.y * vec.y)); + } + + @Override + public Vector2d moveUp() { + return add(new Vector2d(0,1)); + } + + @Override + public Vector2d moveDown() { + return subtract(new Vector2d(0,1)); + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/Vector2i.java b/src/main/java/dev/zontreck/libzontreck/vectors/Vector2i.java index bce3c94..e782c7b 100644 --- a/src/main/java/dev/zontreck/libzontreck/vectors/Vector2i.java +++ b/src/main/java/dev/zontreck/libzontreck/vectors/Vector2i.java @@ -1,16 +1,18 @@ package dev.zontreck.libzontreck.vectors; +import dev.zontreck.libzontreck.api.Vector2; import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.phys.Vec2; -public class Vector2i +public class Vector2i implements Vector2 { public static final Vector2i ZERO = new Vector2i(0, 0); public int x; public int y; + @Override public Vec2 asMinecraftVector(){ return new Vec2(x, y); } @@ -32,30 +34,34 @@ public class Vector2i y=(int)Math.floor(pos.y); } - public Vector2i(String pos) throws InvalidDeserialization + public static Vector2i parseString(String vector2) { + Vector2i vec = new Vector2i(); // This will be serialized most likely from the ToString method // Parse - if(pos.startsWith("<")) + if(vector2.startsWith("<")) { - pos=pos.substring(1, pos.length()-1); // Rip off the ending bracket too - String[] positions = pos.split(", "); + vector2=vector2.substring(1, vector2.length()-1); // Rip off the ending bracket too + String[] positions = vector2.split(", "); if(positions.length!=2) { - positions = pos.split(","); + positions = vector2.split(","); } if(positions.length!=2) { - throw new InvalidDeserialization("Positions must be in the same format provided by ToString() (ex. <1,1> or <1, 1>"); + return ZERO; } - this.x = Integer.parseInt(positions[0]); - this.y = Integer.parseInt(positions[1]); + vec.x = Integer.parseInt(positions[0]); + vec.y = Integer.parseInt(positions[1]); // We are done now } + + return vec; } + @Override public Vector2i Clone() { Vector2i n = new Vector2i(x, y); @@ -69,6 +75,7 @@ public class Vector2i } + @Override public CompoundTag serialize() { CompoundTag tag = new CompoundTag(); @@ -78,25 +85,33 @@ public class Vector2i return tag; } - public Vector2i(CompoundTag tag) { - this.deserialize(tag); - } - public void deserialize(CompoundTag tag) + public static Vector2i deserialize(CompoundTag tag) { - x=tag.getInt("x"); - y=tag.getInt("y"); + Vector2i vec = new Vector2i(); + + vec.x=tag.getInt("x"); + vec.y=tag.getInt("y"); + + return vec; } - public boolean same(Vector2i other) + @Override + public boolean Same(Vector2 other) { - if(x == other.x && y==other.y)return true; + Vector2i v2i = other.asVector2i(); + + if(x == v2i.x && y==v2i.y)return true; else return false; } - public boolean inside(Vector2i point1, Vector2i point2) + @Override + public boolean Inside(Vector2 point1, Vector2 point2) { - if(point1.x <= x && point2.x >= x){ - if(point1.y <= y && point2.y >= y) + Vector2i v1i = point1.asVector2i(); + Vector2i v2i = point2.asVector2i(); + + if(v1i.x <= x && v2i.x >= x){ + if(v1i.y <= y && v2i.y >= y) { return true; } @@ -105,16 +120,69 @@ public class Vector2i return false; } - public boolean greater(Vector2i other) - { - return ((x>other.x) && (y>other.y)); + @Override + public Vector2d asVector2d() { + return new Vector2d(x,y); } - public boolean less(Vector2i other) - { - return ((x>other.x) && (y>other.y)); + + @Override + public Vector2i asVector2i() { + return this; } - public boolean equal(Vector2i other) + + @Override + public boolean greater(Vector2 other) { - return same(other); + Vector2i vec = other.asVector2i(); + return ((x > vec.x) && (y > vec.y)); + } + + @Override + public boolean less(Vector2 other) + { + Vector2i vec = other.asVector2i(); + return ((x > vec.x) && (y > vec.y)); + } + + @Override + public boolean equal(Vector2 other) + { + return Same(other); + } + + @Override + public Vector2i add(Vector2 other) { + Vector2i vec = other.asVector2i(); + x += vec.x; + y += vec.y; + + return this; + } + + @Override + public Vector2i subtract(Vector2 other) { + Vector2i vec = other.asVector2i(); + + x -= vec.x; + y -= vec.y; + + return this; + } + + @Override + public double distance(Vector2 other) { + Vector2i vec = subtract(other); + return Math.sqrt((vec.x * vec.x + vec.y * vec.y)); + } + + + @Override + public Vector2i moveUp() { + return add(new Vector2i(0,1)); + } + + @Override + public Vector2i moveDown() { + return subtract(new Vector2i(0,1)); } } diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/Vector3.java b/src/main/java/dev/zontreck/libzontreck/vectors/Vector3.java deleted file mode 100644 index c1cf9ee..0000000 --- a/src/main/java/dev/zontreck/libzontreck/vectors/Vector3.java +++ /dev/null @@ -1,285 +0,0 @@ -package dev.zontreck.libzontreck.vectors; - -import java.util.ArrayList; -import java.util.List; - -import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; -import net.minecraft.core.BlockPos; -import net.minecraft.core.Vec3i; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.phys.Vec3; - -public class Vector3 -{ - public static final Vector3 ZERO = new Vector3(0, 0, 0); - - - public double x; - public double y; - public double z; - - public Vec3 asMinecraftVector(){ - return new Vec3(x, y, z); - } - - public Vec3i asMinecraftVec3i() - { - return new Vec3i((int) Math.round(x), (int) Math.round(y), (int) Math.round(z)); - } - - public BlockPos asBlockPos() - { - return new BlockPos(asMinecraftVec3i()); - } - - public Vector3() - { - - } - - public Vector3(double x, double y, double z) - { - this.x=x; - this.y=y; - this.z=z; - } - - public Vector3(Vec3 pos) - { - x=pos.x; - y=pos.y; - z=pos.z; - } - - public Vector3(BlockPos pos) - { - x=pos.getX(); - y=pos.getY(); - z=pos.getZ(); - } - - public Vector3(String pos) throws InvalidDeserialization - { - // This will be serialized most likely from the ToString method - // Parse - if(pos.startsWith("<")) - { - pos=pos.substring(1, pos.length()-1); // Rip off the ending bracket too - String[] positions = pos.split(", "); - if(positions.length!=3) - { - positions = pos.split(","); - } - - if(positions.length!=3) - { - throw new InvalidDeserialization("Positions must be in the same format provided by ToString() (ex. <1,1,1> or <1, 1, 1>"); - } - - this.x = Double.parseDouble(positions[0]); - this.y = Double.parseDouble(positions[1]); - this.z = Double.parseDouble(positions[2]); - // We are done now - } - } - - public List makeCube(Vector3 other) - { - List vecs = new ArrayList<>(); - Vector3 work = new Vector3(); - - double xx = x; - double yy = y; - double zz = z; - - int yState = 0; - int zState = 0; - int xState = 0; - - for(xx = Math.round(x); (xx != Math.round(other.x) && xState != 2);) - { - for(zz = Math.round(z); (zz != Math.round(other.z) && zState != 2);) - { - for(yy = Math.round(y); (yy != Math.round(other.y) && yState != 2);) - { - work = new Vector3(xx, yy, zz); - - if(!vecs.contains(work)) vecs.add(work); - - if(yy > other.y) - { - yy -= 1.0; - if(yy == Math.round(other.y) && yState == 0) - { - yState++; - }else{ - if(yState == 1) - { - yState ++; - } - } - } else if(yy < other.y) - { - yy += 1.0; - if(yy == Math.round(other.y) && yState == 0){ - yState ++; - }else { - if(yState == 1)yState++; - } - } - } - - yState=0; - work = new Vector3(xx,yy,zz); - - if(!vecs.contains(work)) vecs.add(work); - - if(zz > other.z) - { - zz -= 1.0; - - if(zz == Math.round(other.z) && zState == 0)zState++; - else{ - if(zState == 1)zState++; - } - }else if(zz < other.z) - { - zz += 1.0; - - if(zz == Math.round(other.z) && zState == 0)zState++; - else { - if(zState==1)zState++; - } - } - } - - zState=0; - work = new Vector3(xx,yy,zz); - - if(!vecs.contains(work)) vecs.add(work); - - if(xx > other.x) - { - xx -= 1.0; - - if(xx == Math.round(other.x) && xState == 0) xState++; - else{ - if(xState == 1)xState++; - } - }else if(xx < other.x) - { - xx += 1.0; - - if(xx == Math.round(other.x) && xState==0)xState++; - else{ - if(xState==1)xState++; - } - } - } - - return vecs; - } - - public Vector3 subtract(Vector3 other) - { - return new Vector3(x-other.x, y-other.y, z-other.z); - } - public Vector3 add(Vector3 other) - { - return new Vector3(x+other.x, y+other.y, z +other.z); - } - - public double distance(Vector3 other) - { - Vector3 sub = subtract(other); - return Math.sqrt((sub.x * sub.x + sub.y * sub.y + sub.z * sub.z)); - } - - public Vector3 moveUp() - { - Vector3 up = Clone(); - up.y+=1; - return up; - } - public Vector3 moveDown() - { - Vector3 up = Clone(); - up.y-=1; - return up; - } - - - public Vector3 Clone() - { - Vector3 n = new Vector3(x, y, z); - return n; - } - - @Override - public String toString() - { - return "<"+String.valueOf(x)+", "+String.valueOf(y)+", "+String.valueOf(z)+">"; - } - - public NonAbsVector3 rounded() - { - NonAbsVector3 cl = new NonAbsVector3(this); - return cl; - } - - public CompoundTag serialize() - { - CompoundTag tag = new CompoundTag(); - tag.putDouble("x", x); - tag.putDouble("y", y); - tag.putDouble("z", z); - - return tag; - } - - public Vector3(CompoundTag tag) { - this.deserialize(tag); - } - public void deserialize(CompoundTag tag) - { - x=tag.getDouble("x"); - y=tag.getDouble("y"); - z=tag.getDouble("z"); - } - - - public boolean same(Vector3 other) - { - if(x == other.x && y==other.y && z==other.z)return true; - else return false; - } - - - public boolean inside(Vector3 point1, Vector3 point2) - { - if(point1.x <= x && point2.x >= x){ - if(point1.y <= y && point2.y >= y) - { - if(point1.z <= z && point2.z >= z) - { - return true; - } - } - } - - return false; - } - - public boolean greater(Vector3 other) - { - return ((x>other.x) && (y>other.y) && (z>other.z)); - } - public boolean less(Vector3 other) - { - return ((x"; + } + + public NonAbsVector3 rounded() + { + NonAbsVector3 cl = new NonAbsVector3(this); + return cl; + } + + @Override + public CompoundTag serialize() + { + CompoundTag tag = new CompoundTag(); + tag.putDouble("x", x); + tag.putDouble("y", y); + tag.putDouble("z", z); + + return tag; + } + + public static Vector3d deserialize(CompoundTag tag) + { + Vector3d vec = new Vector3d(); + + vec.x=tag.getDouble("x"); + vec.y=tag.getDouble("y"); + vec.z=tag.getDouble("z"); + + return vec; + } + + @Override + public boolean Same(Vector3 other) + { + Vector3d vec = other.asVector3d(); + if(x == vec.x && y==vec.y && z==vec.z)return true; + else return false; + } + + + @Override + public boolean Inside(Vector3 point1, Vector3 point2) + { + Vector3d v1 = point1.asVector3d(); + Vector3d v2 = point2.asVector3d(); + + if(v1.x <= x && v2.x >= x){ + if(v1.y <= y && v2.y >= y) + { + if(v1.z <= z && v2.z >= z) + { + return true; + } + } + } + + return false; + } + + @Override + public Vector3d asVector3d() { + return this; + } + + @Override + public Vector3i asVector3i() { + return new Vector3i((int) Math.round(x), (int) Math.round(y), (int) Math.round(z)); + } + + @Override + public boolean greater(Vector3 other) + { + Vector3d vec = other.asVector3d(); + return ((x>vec.x) && (y>vec.y) && (z>vec.z)); + } + + @Override + public boolean less(Vector3 other) + { + Vector3d vec = other.asVector3d(); + return ((x or <1, 1, 1>"); + } + + vec.x = Integer.parseInt(positions[0]); + vec.y = Integer.parseInt(positions[1]); + vec.z = Integer.parseInt(positions[2]); + // We are done now + } + + return vec; + } + + @Override + public Vector3i subtract(Vector3 other) + { + Vector3i vec = other.asVector3i(); + return new Vector3i(x-vec.x, y-vec.y, z-vec.z); + } + + @Override + public Vector3i add(Vector3 other) + { + Vector3i vec = other.asVector3i(); + return new Vector3i(x+vec.x, y+vec.y, z +vec.z); + } + + @Override + public Vector3i asVector3i() { + return this; + } + + @Override + public Vector3d asVector3d() { + return new Vector3d(x, y, z); + } + + @Override + public double distance(Vector3 other) + { + Vector3i sub = subtract(other); + return Math.sqrt((sub.x * sub.x + sub.y * sub.y + sub.z * sub.z)); + } + + @Override + public Vector3i moveUp() + { + return add(new Vector3i(0,1,0)); + } + public Vector3i moveDown() + { + return subtract(new Vector3i(0,1,0)); + } + + @Override + public Vector3i Clone() + { + Vector3i n = new Vector3i(x, y, z); + return n; + } + + @Override + public String toString() + { + return "<"+String.valueOf(x)+", "+String.valueOf(y)+", "+String.valueOf(z)+">"; + } + + @Override + public CompoundTag serialize() + { + CompoundTag tag = new CompoundTag(); + tag.putInt("x", x); + tag.putInt("y", y); + tag.putInt("z", z); + + return tag; + } + + public static Vector3i deserialize(CompoundTag tag) + { + Vector3i vec = new Vector3i(); + + vec.x=tag.getInt("x"); + vec.y=tag.getInt("y"); + vec.z=tag.getInt("z"); + + return vec; + } + + @Override + public boolean Same(Vector3 other) + { + Vector3i vec = other.asVector3i(); + if(x == vec.x && y==vec.y && z==vec.z)return true; + else return false; + } + + @Override + public boolean Inside(Vector3 point1, Vector3 point2) + { + Vector3i v1 = point1.asVector3i(); + Vector3i v2 = point2.asVector3i(); + + if(v1.x <= x && v2.x >= x){ + if(v1.y <= y && v2.y >= y) + { + if(v1.z <= z && v2.z >= z) + { + return true; + } + } + } + + return false; + } + + @Override + public boolean greater(Vector3 other) + { + Vector3i v3 = other.asVector3i(); + return ((x>v3.x) && (y>v3.y) && (z>v3.z)); + } + + @Override + public boolean less(Vector3 other) + { + Vector3i vec = other.asVector3i(); + return ((x