From 7e5c273f4df8e96aab2a20a4323c5c9e3270c164 Mon Sep 17 00:00:00 2001 From: Aria Date: Wed, 22 Feb 2023 01:22:00 -0700 Subject: [PATCH] Publish changes to Library API --- gradle.properties | 2 +- .../exceptions/InvalidSideException.java | 12 ++ .../libzontreck/vectors/ChunkPos.java | 12 +- .../zontreck/libzontreck/vectors/Points.java | 21 +++- .../libzontreck/vectors/WorldPosition.java | 115 +++++++++++++++--- src/main/resources/META-INF/mods.toml | 2 +- 6 files changed, 134 insertions(+), 30 deletions(-) create mode 100644 src/main/java/dev/zontreck/libzontreck/exceptions/InvalidSideException.java diff --git a/gradle.properties b/gradle.properties index b16a3a9..5548e31 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,5 +5,5 @@ org.gradle.daemon=false mc_version=1.19.2 forge_version=43.2.3 -myversion=1.0.4.4 +myversion=1.0.4.5 parchment_version=2022.11.27 \ No newline at end of file diff --git a/src/main/java/dev/zontreck/libzontreck/exceptions/InvalidSideException.java b/src/main/java/dev/zontreck/libzontreck/exceptions/InvalidSideException.java new file mode 100644 index 0000000..646ca87 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/exceptions/InvalidSideException.java @@ -0,0 +1,12 @@ +package dev.zontreck.libzontreck.exceptions; + +/** + * Thrown when requesting a world position's level on the client when in the wrong dimension. + * @see WorldPosition + */ +public class InvalidSideException extends Exception +{ + public InvalidSideException(String msg){ + super(msg); + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/ChunkPos.java b/src/main/java/dev/zontreck/libzontreck/vectors/ChunkPos.java index e504587..9c5540c 100644 --- a/src/main/java/dev/zontreck/libzontreck/vectors/ChunkPos.java +++ b/src/main/java/dev/zontreck/libzontreck/vectors/ChunkPos.java @@ -1,21 +1,21 @@ package dev.zontreck.libzontreck.vectors; import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.level.Level; public class ChunkPos { - public boolean isSubArea; public Points points; public Vector2 centerPoints; + public String dim; - public ChunkPos(Vector3 point1, Vector3 point2) + public ChunkPos(Vector3 point1, Vector3 point2, Level lvl) { - isSubArea=true; - points = new Points(point1, point2); + points = new Points(point1, point2, lvl); + dim = WorldPosition.getDim(lvl); } public ChunkPos(CompoundTag tag) { - isSubArea = tag.getBoolean("subarea"); points = new Points(tag.getCompound("points")); centerPoints = new Vector2(tag.getCompound("center")); } @@ -33,9 +33,9 @@ public class ChunkPos { public CompoundTag serialize() { CompoundTag tag = new CompoundTag(); - tag.putBoolean("subarea", isSubArea); tag.put("points", points.serialize()); tag.put("center", centerPoints.serialize()); + tag.putString("dim", dim); return tag; } diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/Points.java b/src/main/java/dev/zontreck/libzontreck/vectors/Points.java index 147d26a..1b027a3 100644 --- a/src/main/java/dev/zontreck/libzontreck/vectors/Points.java +++ b/src/main/java/dev/zontreck/libzontreck/vectors/Points.java @@ -1,14 +1,25 @@ package dev.zontreck.libzontreck.vectors; import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.level.Level; +/** + * Two points within the same dimension + */ public class Points { public Vector3 Min = Vector3.ZERO; public Vector3 Max = Vector3.ZERO; + public String dimension = ""; - - public Points(Vector3 min, Vector3 max) + /** + * Creates a new set of points + * @param min + * @param max + * @param lvl + */ + public Points(Vector3 min, Vector3 max, Level lvl) { + dimension = WorldPosition.getDimSafe(lvl); if(min.less(max)) { Min=min; @@ -19,6 +30,10 @@ public class Points { } } + /** + * Deserializes a points compound tag + * @param tag + */ public Points(CompoundTag tag){ deserialize(tag); } @@ -27,6 +42,7 @@ public class Points { CompoundTag tag = new CompoundTag(); tag.put("min", Min.serialize()); tag.put("max", Max.serialize()); + tag.putString("dim", dimension); return tag; } @@ -34,5 +50,6 @@ public class Points { { Min = new Vector3(tag.getCompound("min")); Max = new Vector3(tag.getCompound("max")); + dimension = tag.getString("dim"); } } diff --git a/src/main/java/dev/zontreck/libzontreck/vectors/WorldPosition.java b/src/main/java/dev/zontreck/libzontreck/vectors/WorldPosition.java index 213c1d7..92962b7 100644 --- a/src/main/java/dev/zontreck/libzontreck/vectors/WorldPosition.java +++ b/src/main/java/dev/zontreck/libzontreck/vectors/WorldPosition.java @@ -1,13 +1,24 @@ package dev.zontreck.libzontreck.vectors; +import com.mojang.authlib.yggdrasil.response.UserAttributesResponse.ProfanityFilterPreferences; + import dev.zontreck.libzontreck.LibZontreck; import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; +import dev.zontreck.libzontreck.exceptions.InvalidSideException; +import net.minecraft.client.Minecraft; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtUtils; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.network.FilteredText; +import net.minecraft.util.datafix.fixes.FilteredSignsFix; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; +/** + * Stores a position relative to a dimension + */ public class WorldPosition { @@ -42,29 +53,57 @@ public class WorldPosition this(new Vector3(player.position()), player.getLevel()); } + public WorldPosition(Player player) + { + this(new Vector3(player.position()), player.getLevel()); + } + + public WorldPosition(Vector3 pos, Level lvl) + { + this(pos, lvl.dimension().location().getNamespace() + ":"+lvl.dimension().location().getPath()); + } + public WorldPosition(Vector3 pos, ServerLevel lvl) { - Position=pos; - Dimension = lvl.dimension().location().getNamespace() + ":"+lvl.dimension().location().getPath(); - calcDimSafe(); + this(pos, lvl.dimension().location().getNamespace() + ":"+lvl.dimension().location().getPath()); } public void calcDimSafe() { - ServerLevel lvl = getActualDimension(); - DimSafe = lvl.dimension().location().getNamespace() + "-" + lvl.dimension().location().getPath(); + try{ + + Level lvl = getActualDimension(); + DimSafe = lvl.dimension().location().getNamespace() + "-" + lvl.dimension().location().getPath(); + }catch(InvalidSideException ex) + { + DimSafe=""; + } } - public static String getDimSafe(ServerLevel lvl) + public static String getDimSafe(Level lvl) { return lvl.dimension().location().getNamespace() + "-" + lvl.dimension().location().getPath(); } + /** + * Gives you the dimension string modid:dimension + * @param lvl + * @return dimension string + */ + public static String getDim(Level lvl) + { + return lvl.dimension().location().getNamespace() + ":" + lvl.dimension().location().getPath(); + } + @Override public String toString() { return NbtUtils.structureToSnbt(serialize()); } + /** + * Stores the position as a string instead of as a compound tag. Uses more memory overall + * @return + */ public CompoundTag serializePretty() { CompoundTag tag = new CompoundTag(); @@ -75,6 +114,10 @@ public class WorldPosition return tag; } + /** + * Serializes the compound tag version of this position + * @return + */ public CompoundTag serialize() { CompoundTag tag = new CompoundTag(); @@ -86,24 +129,41 @@ public class WorldPosition - public ServerLevel getActualDimension() + /** + * You must run this on the Server unless you know for a fact that the position you are requesting the dimension for is the same one the client is currently in. Otherwise you must handle the Exception properly. + * @return The level that this worldpos stores + * @throws InvalidSideException + */ + public Level getActualDimension() throws InvalidSideException { 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(); + Level dimL = null; + + if(Minecraft.getInstance()==null) + { - if(XL.getNamespace().equals(rl.getNamespace())){ - if(XL.getPath().equals(rl.getPath())){ - dimL = lServerLevel; + for (Level lLevel : LibZontreck.THE_SERVER.getAllLevels()) { + ResourceLocation XL = lLevel.dimension().location(); + + if(XL.getNamespace().equals(rl.getNamespace())){ + if(XL.getPath().equals(rl.getPath())){ + dimL = lLevel; + } } } + }else { + if(getDim(Minecraft.getInstance().level) == Dimension) + { + return Minecraft.getInstance().level; + }else throw new InvalidSideException("This operation must be run on the server as the client is not in the dimension you requested"); + } + if(dimL == null) { LibZontreck.LOGGER.error("DIMENSION COULD NOT BE FOUND : "+Dimension); @@ -113,20 +173,35 @@ public class WorldPosition return dimL; } - + /** + * Returns true if the other world position is identical + * @param other + * @return + */ public boolean same(WorldPosition other) { if(Position.same(other.Position) && Dimension == other.Dimension)return true; else return false; } + /** + * Gets the LibZontreck ChunkPosition + * @return ChunkPos of the chunk mentioned by this worldposition, or null in the event that we are on the client and the dimension is not the same one we are inside. + * @see WorldPosition#getActualDimension() + */ public ChunkPos getChunkPos() { - net.minecraft.world.level.ChunkPos mcChunk = getActualDimension().getChunkAt(Position.asBlockPos()).getPos(); - ChunkPos pos = new ChunkPos(new Vector3(mcChunk.getMinBlockX(),-70,mcChunk.getMinBlockZ()), new Vector3(mcChunk.getMaxBlockX(), 400, mcChunk.getMaxBlockZ())); - pos.isSubArea=false; - pos.centerPoints = new Vector2(mcChunk.getMiddleBlockX(), mcChunk.getMiddleBlockZ()); - - return pos; + try + { + Level lvl = getActualDimension(); + net.minecraft.world.level.ChunkPos mcChunk = lvl.getChunkAt(Position.asBlockPos()).getPos(); + ChunkPos pos = new ChunkPos(new Vector3(mcChunk.getMinBlockX(),-70,mcChunk.getMinBlockZ()), new Vector3(mcChunk.getMaxBlockX(), 400, mcChunk.getMaxBlockZ()), lvl); + pos.centerPoints = new Vector2(mcChunk.getMiddleBlockX(), mcChunk.getMiddleBlockZ()); + + return pos; + }catch(InvalidSideException ex) + { + return null; + } } } diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 6dc4983..e465b6e 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.4.4" #mandatory +version="1.0.4.5" #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/