From 51043006b56af3628253568c51699300930c4202 Mon Sep 17 00:00:00 2001 From: Zontreck Date: Mon, 17 Oct 2022 05:33:33 -0700 Subject: [PATCH] 1.0.1.0 Move more common code from OTEMod into this library --- gradle.properties | 2 +- .../dev/zontreck/libzontreck/LibZontreck.java | 25 ++++ .../exceptions/InvalidDeserialization.java | 12 ++ .../libzontreck/vectors/NonAbsVector3.java | 18 +++ .../zontreck/libzontreck/vectors/Vector2.java | 68 +++++++++ .../zontreck/libzontreck/vectors/Vector3.java | 140 ++++++++++++++++++ .../libzontreck/vectors/WorldPosition.java | 95 ++++++++++++ src/main/resources/META-INF/mods.toml | 2 +- 8 files changed, 360 insertions(+), 2 deletions(-) create mode 100644 src/main/java/dev/zontreck/libzontreck/exceptions/InvalidDeserialization.java create mode 100644 src/main/java/dev/zontreck/libzontreck/vectors/NonAbsVector3.java create mode 100644 src/main/java/dev/zontreck/libzontreck/vectors/Vector2.java create mode 100644 src/main/java/dev/zontreck/libzontreck/vectors/Vector3.java create mode 100644 src/main/java/dev/zontreck/libzontreck/vectors/WorldPosition.java diff --git a/gradle.properties b/gradle.properties index cd91715..d41eef5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,4 +5,4 @@ org.gradle.daemon=false mc_version=1.19.2 forge_version=43.1.16 -myversion=1.0.0.2 \ No newline at end of file +myversion=1.0.1.0 \ No newline at end of file diff --git a/src/main/java/dev/zontreck/libzontreck/LibZontreck.java b/src/main/java/dev/zontreck/libzontreck/LibZontreck.java index 0d4ebb6..dfbf84e 100644 --- a/src/main/java/dev/zontreck/libzontreck/LibZontreck.java +++ b/src/main/java/dev/zontreck/libzontreck/LibZontreck.java @@ -4,14 +4,39 @@ import org.slf4j.Logger; import com.mojang.logging.LogUtils; +import net.minecraft.server.MinecraftServer; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.server.ServerStartedEvent; +import net.minecraftforge.eventbus.api.IEventBus; +import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; @Mod(LibZontreck.MOD_ID) public class LibZontreck { public static final Logger LOGGER = LogUtils.getLogger(); public static final String MOD_ID = "libzontreck"; + public static MinecraftServer THE_SERVER; public LibZontreck(){ + + IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); + // Register the setup method for modloading + bus.addListener(this::setup); + + MinecraftForge.EVENT_BUS.register(this); + } + + private void setup(final FMLCommonSetupEvent event) + { + } + + + @SubscribeEvent + public void onServerStarted(final ServerStartedEvent event) + { + THE_SERVER = event.getServer(); } } diff --git a/src/main/java/dev/zontreck/libzontreck/exceptions/InvalidDeserialization.java b/src/main/java/dev/zontreck/libzontreck/exceptions/InvalidDeserialization.java new file mode 100644 index 0000000..7d53db3 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/exceptions/InvalidDeserialization.java @@ -0,0 +1,12 @@ +package dev.zontreck.libzontreck.exceptions; + +public class InvalidDeserialization extends Exception +{ + + public InvalidDeserialization(String error){ + super(error); + } + public InvalidDeserialization(){ + super("Incorrect information was provided to the deserializer"); + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/NonAbsVector3.java b/src/main/java/dev/zontreck/libzontreck/vectors/NonAbsVector3.java new file mode 100644 index 0000000..acb320d --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/vectors/NonAbsVector3.java @@ -0,0 +1,18 @@ +package dev.zontreck.libzontreck.vectors; + +/* +* This is a non-serializable instanced Vector that is meant to slam positions down as a integer +*/ +public class NonAbsVector3 +{ + public long x; + public long y; + public long z; + + public NonAbsVector3(Vector3 origin) + { + x = Math.round(origin.x); + y = Math.round(origin.y); + z = Math.round(origin.z); + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/Vector2.java b/src/main/java/dev/zontreck/libzontreck/vectors/Vector2.java new file mode 100644 index 0000000..daf9e5e --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/vectors/Vector2.java @@ -0,0 +1,68 @@ +package dev.zontreck.libzontreck.vectors; + +import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; +import net.minecraft.world.phys.Vec2; +import net.minecraft.world.phys.Vec3; + +public class Vector2 +{ + 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) + ">"; + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/Vector3.java b/src/main/java/dev/zontreck/libzontreck/vectors/Vector3.java new file mode 100644 index 0000000..6ec105e --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/vectors/Vector3.java @@ -0,0 +1,140 @@ +package dev.zontreck.libzontreck.vectors; + +import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; +import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.phys.Vec3; + +public class Vector3 +{ + public double x; + public double y; + public double z; + + public Vec3 asMinecraftVector(){ + return new Vec3(x, y, z); + } + + public BlockPos asBlockPos() + { + return new BlockPos(asMinecraftVector()); + } + + 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 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"); + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/WorldPosition.java b/src/main/java/dev/zontreck/libzontreck/vectors/WorldPosition.java new file mode 100644 index 0000000..6479536 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/vectors/WorldPosition.java @@ -0,0 +1,95 @@ +package dev.zontreck.libzontreck.vectors; + +import dev.zontreck.libzontreck.LibZontreck; +import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtUtils; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; + +public class WorldPosition +{ + + public Vector3 Position; + public String Dimension; + + public WorldPosition(CompoundTag tag, boolean pretty) throws InvalidDeserialization + { + if(pretty){ + + Position = new Vector3(tag.getString("Position")); + Dimension = tag.getString("Dimension"); + }else { + Position = new Vector3(tag.getCompound("pos")); + Dimension = tag.getString("Dimension"); + } + + } + + public WorldPosition(Vector3 pos, String dim) + { + Position=pos; + Dimension=dim; + } + + public WorldPosition(Vector3 pos, ServerLevel lvl) + { + Position=pos; + Dimension = lvl.dimension().location().getNamespace() + ":"+lvl.dimension().location().getPath(); + } + + @Override + public String toString() + { + return NbtUtils.structureToSnbt(serialize()); + } + + public CompoundTag serializePretty() + { + CompoundTag tag = new CompoundTag(); + + tag.putString("Position", Position.toString()); + tag.putString("Dimension", Dimension); + + return tag; + } + + public CompoundTag serialize() + { + CompoundTag tag = new CompoundTag(); + tag.put("pos", Position.serialize()); + tag.putString("Dimension", Dimension); + + return tag; + } + + + + public ServerLevel getActualDimension() + { + + String dim = Dimension; + String[] dims = dim.split(":"); + + ResourceLocation rl = new ResourceLocation(dims[0], dims[1]); + ServerLevel dimL = null; + for (ServerLevel lServerLevel : LibZontreck.THE_SERVER.getAllLevels()) { + ResourceLocation XL = lServerLevel.dimension().location(); + + if(XL.getNamespace().equals(rl.getNamespace())){ + if(XL.getPath().equals(rl.getPath())){ + dimL = lServerLevel; + } + } + } + + if(dimL == null) + { + LibZontreck.LOGGER.error("DIMENSION COULD NOT BE FOUND : "+Dimension); + return null; + } + + return dimL; + } +} diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 9c3a29c..e84b498 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -19,7 +19,7 @@ modId="libzontreck" #mandatory # The version number of the mod - there's a few well known ${} variables useable here or just hardcode it # ${file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata # see the associated build.gradle script for how to populate this completely automatically during a build -version="1.0.0.2" #mandatory +version="1.0.1.0" #mandatory # A display name for the mod displayName="LibZontreck" #mandatory # A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/