From 4101ff70be0a49bbc641638317f0432b48311083 Mon Sep 17 00:00:00 2001 From: Zontreck Date: Mon, 4 Mar 2024 18:06:23 -0700 Subject: [PATCH] Fix a client crash --- gradle.properties | 2 +- .../zontreck/otemod/events/ClientEvents.java | 2 +- .../packets/EnergyRequestC2SPacket.java | 50 +++++++++++++++---- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/gradle.properties b/gradle.properties index 612f7e3..6ba184f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -48,7 +48,7 @@ mod_name=Thresholds # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=GPLv3 # The mod version. See https://semver.org/ -mod_version=1201.4.030424.1722 +mod_version=1201.4.030424.1750 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/dev/zontreck/otemod/events/ClientEvents.java b/src/main/java/dev/zontreck/otemod/events/ClientEvents.java index b4a381f..ec6258a 100644 --- a/src/main/java/dev/zontreck/otemod/events/ClientEvents.java +++ b/src/main/java/dev/zontreck/otemod/events/ClientEvents.java @@ -60,7 +60,7 @@ public class ClientEvents { lvl = entity.getLevel(); ResourceLocation location = lvl.dimension().location(); - ModMessages.sendToServer(new EnergyRequestC2SPacket(new WorldPosition(new Vector3(pos.getX(), pos.getY(), pos.getZ()), location.getNamespace() + ":" + location.getPath()), Minecraft.getInstance().player)); + ModMessages.sendToServer(new EnergyRequestC2SPacket(pos, lvl, Minecraft.getInstance().player)); } } } diff --git a/src/main/java/dev/zontreck/otemod/networking/packets/EnergyRequestC2SPacket.java b/src/main/java/dev/zontreck/otemod/networking/packets/EnergyRequestC2SPacket.java index 60440b8..ec8498d 100644 --- a/src/main/java/dev/zontreck/otemod/networking/packets/EnergyRequestC2SPacket.java +++ b/src/main/java/dev/zontreck/otemod/networking/packets/EnergyRequestC2SPacket.java @@ -1,5 +1,6 @@ package dev.zontreck.otemod.networking.packets; +import dev.zontreck.libzontreck.LibZontreck; import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; import dev.zontreck.libzontreck.util.ServerUtilities; import dev.zontreck.libzontreck.vectors.WorldPosition; @@ -7,38 +8,42 @@ import dev.zontreck.otemod.implementation.energy.IThresholdsEnergy; import dev.zontreck.otemod.networking.ModMessages; import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerLevel; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.server.ServerLifecycleHooks; +import java.util.Iterator; import java.util.UUID; import java.util.function.Supplier; public class EnergyRequestC2SPacket { - private WorldPosition position; + private BlockPos position; + private String dimension; private UUID player; - public EnergyRequestC2SPacket(WorldPosition position, Player player) + public EnergyRequestC2SPacket(BlockPos position, Level level, Player player) { this.position=position; + dimension = level.dimension().location().getNamespace() + ":" + level.dimension().location().getPath(); this.player=player.getUUID(); } public EnergyRequestC2SPacket(FriendlyByteBuf buf) { - try { - position = new WorldPosition(buf.readAnySizeNbt(), false); - } catch (InvalidDeserialization e) { - throw new RuntimeException(e); - } + position = buf.readBlockPos(); + dimension = buf.readUtf(); player = buf.readUUID(); } public void toBytes(FriendlyByteBuf buf) { - buf.writeNbt(position.serialize()); + buf.writeBlockPos(position); + buf.writeUtf(dimension); buf.writeUUID(player); } @@ -49,8 +54,9 @@ public class EnergyRequestC2SPacket context.enqueueWork(()-> { if(position==null)return; - BlockPos pos = position.Position.asBlockPos(); - BlockEntity entity = position.getActualDimension().getBlockEntity(pos); + BlockPos pos = position; + ServerLevel lvl = getActualDimension(); + BlockEntity entity = lvl.getBlockEntity(pos); if(entity instanceof IThresholdsEnergy ite) { int energy = ite.getEnergy(); @@ -59,4 +65,28 @@ public class EnergyRequestC2SPacket }); } + + public ServerLevel getActualDimension() { + String dim = this.dimension; + String[] dims = dim.split(":"); + ResourceLocation rl = new ResourceLocation(dims[0], dims[1]); + ServerLevel dimL = null; + Iterator var5 = ServerLifecycleHooks.getCurrentServer().getAllLevels().iterator(); + + while(var5.hasNext()) { + ServerLevel lServerLevel = (ServerLevel)var5.next(); + ResourceLocation XL = lServerLevel.dimension().location(); + if (XL.getNamespace().equals(rl.getNamespace()) && XL.getPath().equals(rl.getPath())) { + dimL = lServerLevel; + } + } + + if (dimL == null) { + LibZontreck.LOGGER.error("DIMENSION COULD NOT BE FOUND : " + this.dimension); + return null; + } else { + return dimL; + } + } + }