Bugfix: Rollback clientlevel support

This commit is contained in:
Aria 2023-02-22 20:19:26 -07:00
parent 116e2d92e1
commit e711ba6a2e
5 changed files with 27 additions and 87 deletions

View file

@ -5,5 +5,5 @@ org.gradle.daemon=false
mc_version=1.18.2 mc_version=1.18.2
forge_version=40.2.1 forge_version=40.2.1
myversion=1.0.4.10 myversion=1.0.4.11
parchment_version=2022.11.06 parchment_version=2022.11.06

View file

@ -1,6 +1,7 @@
package dev.zontreck.libzontreck.vectors; package dev.zontreck.libzontreck.vectors;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
public class ChunkPos { public class ChunkPos {
@ -8,7 +9,7 @@ public class ChunkPos {
public Vector2 centerPoints; public Vector2 centerPoints;
public String dim; public String dim;
public ChunkPos(Vector3 point1, Vector3 point2, Level lvl) public ChunkPos(Vector3 point1, Vector3 point2, ServerLevel lvl)
{ {
points = new Points(point1, point2, lvl); points = new Points(point1, point2, lvl);
dim = WorldPosition.getDim(lvl); dim = WorldPosition.getDim(lvl);

View file

@ -1,7 +1,7 @@
package dev.zontreck.libzontreck.vectors; package dev.zontreck.libzontreck.vectors;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.Level; import net.minecraft.server.level.ServerLevel;
/** /**
* Two points within the same dimension * Two points within the same dimension
@ -17,7 +17,7 @@ public class Points {
* @param max * @param max
* @param lvl * @param lvl
*/ */
public Points(Vector3 min, Vector3 max, Level lvl) public Points(Vector3 min, Vector3 max, ServerLevel lvl)
{ {
dimension = WorldPosition.getDimSafe(lvl); dimension = WorldPosition.getDimSafe(lvl);
if(min.less(max)) if(min.less(max))

View file

@ -2,19 +2,12 @@ package dev.zontreck.libzontreck.vectors;
import dev.zontreck.libzontreck.LibZontreck; import dev.zontreck.libzontreck.LibZontreck;
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; 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.CompoundTag;
import net.minecraft.nbt.NbtUtils; import net.minecraft.nbt.NbtUtils;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
/**
* Stores a position relative to a dimension
*/
public class WorldPosition public class WorldPosition
{ {
@ -49,33 +42,19 @@ public class WorldPosition
this(new Vector3(player.position()), player.getLevel()); 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) public WorldPosition(Vector3 pos, ServerLevel lvl)
{ {
this(pos, lvl.dimension().location().getNamespace() + ":"+lvl.dimension().location().getPath()); Position=pos;
Dimension = lvl.dimension().location().getNamespace() + ":"+lvl.dimension().location().getPath();
calcDimSafe();
} }
public void calcDimSafe() public void calcDimSafe()
{ {
try{ ServerLevel lvl = getActualDimension();
Level lvl = getActualDimension();
DimSafe = lvl.dimension().location().getNamespace() + "-" + lvl.dimension().location().getPath(); 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(); return lvl.dimension().location().getNamespace() + "-" + lvl.dimension().location().getPath();
} }
@ -85,21 +64,18 @@ public class WorldPosition
* @param lvl * @param lvl
* @return dimension string * @return dimension string
*/ */
public static String getDim(Level lvl) public static String getDim(ServerLevel lvl)
{ {
return lvl.dimension().location().getNamespace() + ":" + lvl.dimension().location().getPath(); return lvl.dimension().location().getNamespace() + ":" + lvl.dimension().location().getPath();
} }
@Override @Override
public String toString() public String toString()
{ {
return NbtUtils.structureToSnbt(serialize()); return NbtUtils.structureToSnbt(serialize());
} }
/**
* Stores the position as a string instead of as a compound tag. Uses more memory overall
* @return
*/
public CompoundTag serializePretty() public CompoundTag serializePretty()
{ {
CompoundTag tag = new CompoundTag(); CompoundTag tag = new CompoundTag();
@ -110,10 +86,6 @@ public class WorldPosition
return tag; return tag;
} }
/**
* Serializes the compound tag version of this position
* @return
*/
public CompoundTag serialize() public CompoundTag serialize()
{ {
CompoundTag tag = new CompoundTag(); CompoundTag tag = new CompoundTag();
@ -125,40 +97,23 @@ 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 dim = Dimension;
String[] dims = dim.split(":"); String[] dims = dim.split(":");
ResourceLocation rl = new ResourceLocation(dims[0], dims[1]); ResourceLocation rl = new ResourceLocation(dims[0], dims[1]);
Level dimL = null; ServerLevel dimL = null;
for (ServerLevel lServerLevel : LibZontreck.THE_SERVER.getAllLevels()) {
if(Minecraft.getInstance()==null) ResourceLocation XL = lServerLevel.dimension().location();
{
for (Level lLevel : LibZontreck.THE_SERVER.getAllLevels()) {
ResourceLocation XL = lLevel.dimension().location();
if(XL.getNamespace().equals(rl.getNamespace())){ if(XL.getNamespace().equals(rl.getNamespace())){
if(XL.getPath().equals(rl.getPath())){ if(XL.getPath().equals(rl.getPath())){
dimL = lLevel; dimL = lServerLevel;
} }
} }
} }
}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) if(dimL == null)
{ {
@ -169,35 +124,19 @@ public class WorldPosition
return dimL; return dimL;
} }
/**
* Returns true if the other world position is identical
* @param other
* @return
*/
public boolean same(WorldPosition other) public boolean same(WorldPosition other)
{ {
if(Position.same(other.Position) && Dimension == other.Dimension)return true; if(Position.same(other.Position) && Dimension == other.Dimension)return true;
else return false; 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() public ChunkPos getChunkPos()
{ {
try 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()), getActualDimension());
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()); pos.centerPoints = new Vector2(mcChunk.getMiddleBlockX(), mcChunk.getMiddleBlockZ());
return pos; return pos;
}catch(InvalidSideException ex)
{
return null;
}
} }
} }

View file

@ -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 # 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 # ${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 # see the associated build.gradle script for how to populate this completely automatically during a build
version="1.0.4.10" #mandatory version="1.0.4.11" #mandatory
# A display name for the mod # A display name for the mod
displayName="LibZontreck" #mandatory 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/ # A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/