Fix a client crash

This commit is contained in:
Zontreck 2024-03-04 18:06:23 -07:00
parent 6a4250e601
commit 4101ff70be
3 changed files with 42 additions and 12 deletions

View file

@ -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

View file

@ -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));
}
}
}

View file

@ -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;
}
}
}