Add some more utility functions

This commit is contained in:
zontreck 2024-04-07 19:50:32 -07:00
parent ed9d9bd249
commit a4da2bb52b
10 changed files with 164 additions and 47 deletions

View file

@ -1,11 +1,11 @@
package dev.zontreck.libzontreck.api;
import dev.zontreck.libzontreck.vectors.Vector2d;
import dev.zontreck.libzontreck.vectors.Vector2f;
import dev.zontreck.libzontreck.vectors.Vector2i;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.phys.Vec2;
public interface Vector2
public interface Vector2<T>
{
/**
* Converts the current Vector2 representation into a minecraft Vec2
@ -62,7 +62,7 @@ public interface Vector2
* Converts, if necessary, to Vector2d
* @return A vector2d instance
*/
Vector2d asVector2d();
Vector2f asVector2f();
/**
* Converts, if necessary, to Vector2i
@ -123,4 +123,16 @@ public interface Vector2
* @return New instance
*/
Vector2 moveDown();
/**
* Gets the X value
* @return X
*/
T getX();
/**
* Gets the Y value
* @return Y
*/
T getY();
}

View file

@ -1,16 +1,13 @@
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
public interface Vector3<T>
{
/**
* Converts the current Vector3 representation into a minecraft Vec3
@ -140,4 +137,22 @@ public interface Vector3
* @return New instance
*/
Vector3 moveDown();
/**
* Gets the X value
* @return X
*/
T getX();
/**
* Gets the Y value
* @return Y
*/
T getY();
/**
* Gets the Z value
* @return Z
*/
T getZ();
}

View file

@ -9,7 +9,7 @@ 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.Vector2d;
import dev.zontreck.libzontreck.vectors.Vector2f;
import dev.zontreck.libzontreck.vectors.Vector2i;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.item.Item;
@ -228,7 +228,7 @@ public class ChestGUI
return this.id.equals(id);
}
public void handleButtonClicked(int slot, Vector2d pos, Item item) {
public void handleButtonClicked(int slot, Vector2f pos, Item item) {
for(ChestGUIButton button : buttons)
{
if(button.getSlotNum() == slot)

View file

@ -2,8 +2,10 @@ package dev.zontreck.libzontreck.util;
import dev.zontreck.libzontreck.api.Vector3;
import dev.zontreck.libzontreck.vectors.Vector3d;
import dev.zontreck.libzontreck.vectors.Vector3i;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
@ -112,4 +114,43 @@ public class PositionUtil
}
public List<Vector3> sortAscending(List<Vector3> vecs)
{
List<Vector3> copy = new ArrayList<>(vecs);
List<Vector3> sorted = new ArrayList<>();
while(copy.size()>0)
{
Vector3 lowest = findFirstLowestPosition(copy);
copy.remove(lowest);
sorted.add(lowest);
}
return sorted;
}
public static Vector3 findFirstLowestPosition(List<Vector3> vecs)
{
Vector3i lowest = new Vector3i(0, 500, 0);
List<Vector3> copy = new ArrayList<>(vecs);
Iterator<Vector3> it = copy.iterator();
while(it.hasNext())
{
Vector3i entry = it.next().asVector3i();
if(entry.y < lowest.y)
{
lowest = entry;
it.remove();
}else it.remove();
}
return lowest;
}
}

View file

@ -5,7 +5,7 @@ import net.minecraft.server.level.ServerLevel;
public class ChunkPos {
public Points points;
public Vector2d centerPoints;
public Vector2f centerPoints;
public String dim;
public ChunkPos(Vector3d point1, Vector3d point2, ServerLevel lvl)
@ -17,7 +17,7 @@ public class ChunkPos {
public ChunkPos(CompoundTag tag)
{
points = new Points(tag.getCompound("points"));
centerPoints = Vector2d.deserialize(tag.getCompound("center"));
centerPoints = Vector2f.deserialize(tag.getCompound("center"));
}
public boolean isWithin(Vector3d point)

View file

@ -1,13 +1,12 @@
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 class Vector2f implements Vector2<Float>
{
public static final Vector2d ZERO = new Vector2d(0, 0);
public static final Vector2f ZERO = new Vector2f(0, 0);
public float x;
public float y;
@ -17,26 +16,26 @@ public class Vector2d implements Vector2
return new Vec2(x, y);
}
public Vector2d()
public Vector2f()
{
}
public Vector2d(float x, float y)
public Vector2f(float x, float y)
{
this.x=x;
this.y=y;
}
public Vector2d(Vec2 pos)
public Vector2f(Vec2 pos)
{
x=pos.x;
y=pos.y;
}
public static Vector2d parseString(String vector2)
public static Vector2f parseString(String vector2)
{
Vector2d vec = new Vector2d();
Vector2f vec = new Vector2f();
// This will be serialized most likely from the ToString method
// Parse
if(vector2.startsWith("<"))
@ -62,9 +61,9 @@ public class Vector2d implements Vector2
}
@Override
public Vector2d Clone()
public Vector2f Clone()
{
Vector2d n = new Vector2d(x, y);
Vector2f n = new Vector2f(x, y);
return n;
}
@ -84,9 +83,9 @@ public class Vector2d implements Vector2
return tag;
}
public static Vector2d deserialize(CompoundTag tag)
public static Vector2f deserialize(CompoundTag tag)
{
Vector2d vec = new Vector2d();
Vector2f vec = new Vector2f();
vec.x=tag.getFloat("x");
vec.y=tag.getFloat("y");
@ -96,7 +95,7 @@ public class Vector2d implements Vector2
@Override
public boolean Same(Vector2 other)
{
Vector2d ov = other.asVector2d();
Vector2f ov = other.asVector2f();
if(x == ov.x && y == ov.y) return true;
return false;
}
@ -104,8 +103,8 @@ public class Vector2d implements Vector2
@Override
public boolean Inside(Vector2 point1, Vector2 point2)
{
Vector2d p1 = point1.asVector2d();
Vector2d p2 = point2.asVector2d();
Vector2f p1 = point1.asVector2f();
Vector2f p2 = point2.asVector2f();
if(p1.x <= x && p2.x >= x){
if(p1.y <= y && p2.y >= y)
@ -118,7 +117,7 @@ public class Vector2d implements Vector2
}
@Override
public Vector2d asVector2d()
public Vector2f asVector2f()
{
return this;
}
@ -131,14 +130,14 @@ public class Vector2d implements Vector2
@Override
public boolean greater(Vector2 other)
{
Vector2d vec = other.asVector2d();
Vector2f vec = other.asVector2f();
return ((x > vec.x) && (y > vec.y));
}
@Override
public boolean less(Vector2 other)
{
Vector2d vec = other.asVector2d();
Vector2f vec = other.asVector2f();
return ((x > vec.x) && (y > vec.y));
}
public boolean equal(Vector2 other)
@ -147,30 +146,40 @@ public class Vector2d implements Vector2
}
@Override
public Vector2d add(Vector2 other) {
Vector2d vec = other.asVector2d();
return new Vector2d(x + vec.x, y + vec.y);
public Vector2f add(Vector2 other) {
Vector2f vec = other.asVector2f();
return new Vector2f(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);
public Vector2f subtract(Vector2 other) {
Vector2f vec = other.asVector2f();
return new Vector2f(x - vec.x, y - vec.y);
}
@Override
public double distance(Vector2 other) {
Vector2d vec = subtract(other);
Vector2f vec = subtract(other);
return Math.sqrt((vec.x * vec.x + vec.y * vec.y));
}
@Override
public Vector2d moveUp() {
return add(new Vector2d(0,1));
public Vector2f moveUp() {
return add(new Vector2f(0,1));
}
@Override
public Vector2d moveDown() {
return subtract(new Vector2d(0,1));
public Vector2f moveDown() {
return subtract(new Vector2f(0,1));
}
@Override
public Float getX() {
return x;
}
@Override
public Float getY() {
return y;
}
}

View file

@ -1,11 +1,10 @@
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 implements Vector2
public class Vector2i implements Vector2<Integer>
{
public static final Vector2i ZERO = new Vector2i(0, 0);
@ -121,8 +120,8 @@ public class Vector2i implements Vector2
}
@Override
public Vector2d asVector2d() {
return new Vector2d(x,y);
public Vector2f asVector2f() {
return new Vector2f(x,y);
}
@Override
@ -185,4 +184,15 @@ public class Vector2i implements Vector2
public Vector2i moveDown() {
return subtract(new Vector2i(0,1));
}
@Override
public Integer getX() {
return x;
}
@Override
public Integer getY() {
return y;
}
}

View file

@ -10,7 +10,7 @@ import net.minecraft.core.Vec3i;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.phys.Vec3;
public class Vector3d implements Vector3
public class Vector3d implements Vector3<Double>
{
public static final Vector3d ZERO = new Vector3d(0, 0, 0);
@ -219,4 +219,19 @@ public class Vector3d implements Vector3
{
return Same(other);
}
@Override
public Double getX() {
return x;
}
@Override
public Double getY() {
return y;
}
@Override
public Double getZ() {
return z;
}
}

View file

@ -10,7 +10,7 @@ import net.minecraft.world.phys.Vec3;
import java.util.ArrayList;
import java.util.List;
public class Vector3i implements Vector3
public class Vector3i implements Vector3<Integer>
{
public static final Vector3i ZERO = new Vector3i(0, 0, 0);
@ -212,4 +212,19 @@ public class Vector3i implements Vector3
{
return Same(other);
}
@Override
public Integer getX() {
return x;
}
@Override
public Integer getY() {
return y;
}
@Override
public Integer getZ() {
return z;
}
}

View file

@ -121,7 +121,7 @@ public class WorldPosition {
public ChunkPos getChunkPos() {
net.minecraft.world.level.ChunkPos mcChunk = getActualDimension().getChunkAt(Position.asBlockPos()).getPos();
ChunkPos pos = new ChunkPos(new Vector3d(mcChunk.getMinBlockX(), -70, mcChunk.getMinBlockZ()), new Vector3d(mcChunk.getMaxBlockX(), 400, mcChunk.getMaxBlockZ()), getActualDimension());
pos.centerPoints = new Vector2d(mcChunk.getMiddleBlockX(), mcChunk.getMiddleBlockZ());
pos.centerPoints = new Vector2f(mcChunk.getMiddleBlockX(), mcChunk.getMiddleBlockZ());
return pos;
}