Custom packet handler for 98.7% less stack traces when joining worlds

This commit is contained in:
Ellpeck 2016-01-17 20:46:51 +01:00
parent 060a4dcbb3
commit 4b835f8df8
5 changed files with 115 additions and 60 deletions

View file

@ -29,5 +29,6 @@ public class PacketHandler{
theNetwork.registerMessage(PacketGuiNumber.Handler.class, PacketGuiNumber.class, 1, Side.SERVER);
theNetwork.registerMessage(PacketGuiString.Handler.class, PacketGuiString.class, 2, Side.SERVER);
theNetwork.registerMessage(PacketParticle.Handler.class, PacketParticle.class, 3, Side.CLIENT);
theNetwork.registerMessage(PacketUpdateTileEntity.Handler.class, PacketUpdateTileEntity.class, 4, Side.CLIENT);
}
}

View file

@ -0,0 +1,79 @@
/*
* This file ("PacketUpdateTileEntity.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense/
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2016 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.network;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class PacketUpdateTileEntity implements IMessage{
private NBTTagCompound compound;
private BlockPos pos;
@SuppressWarnings("unused")
public PacketUpdateTileEntity(){
}
public PacketUpdateTileEntity(TileEntityBase tile){
this.compound = tile.getSyncCompound();
this.pos = tile.getPos();
}
@Override
public void fromBytes(ByteBuf buf){
PacketBuffer buffer = new PacketBuffer(buf);
try{
this.compound = buffer.readNBTTagCompoundFromBuffer();
this.pos = buffer.readBlockPos();
}
catch(Exception e){
ModUtil.LOGGER.error("Something went wrong trying to receive a TileEntity packet!", e);
}
}
@Override
public void toBytes(ByteBuf buf){
PacketBuffer buffer = new PacketBuffer(buf);
buffer.writeNBTTagCompoundToBuffer(this.compound);
buffer.writeBlockPos(this.pos);
}
public static class Handler implements IMessageHandler<PacketUpdateTileEntity, IMessage>{
@Override
@SideOnly(Side.CLIENT)
public IMessage onMessage(PacketUpdateTileEntity message, MessageContext ctx){
if(message.pos != null && message.compound != null){
World world = Minecraft.getMinecraft().theWorld;
TileEntity tile = world.getTileEntity(message.pos);
if(tile != null && tile instanceof TileEntityBase){
((TileEntityBase)tile).receiveSyncCompound(message.compound);
}
}
return null;
}
}
}

View file

@ -1,40 +0,0 @@
/*
* This file ("VanillaPacketSyncer.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense/
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2016 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.network;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import java.util.List;
public class VanillaPacketSyncer{
public static void sendTileToNearbyPlayers(TileEntity tile){
List allPlayers = tile.getWorld().playerEntities;
for(Object player : allPlayers){
if(player instanceof EntityPlayerMP){
sendTileToPlayer(tile, (EntityPlayerMP)player, 64);
}
}
}
public static void sendTileToPlayer(TileEntity tile, EntityPlayerMP player, int maxDistance){
if(player.getDistance(tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ()) <= maxDistance){
sendTileToPlayer(tile, player);
}
}
public static void sendTileToPlayer(TileEntity tile, EntityPlayerMP player){
if(player.getEntityWorld().getTileEntity(tile.getPos()) == tile){
player.playerNetServerHandler.sendPacket(tile.getDescriptionPacket());
}
}
}

View file

@ -11,7 +11,8 @@
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
import de.ellpeck.actuallyadditions.mod.network.VanillaPacketSyncer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketUpdateTileEntity;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
@ -22,6 +23,7 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ITickable;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.registry.GameRegistry;
public abstract class TileEntityBase extends TileEntity implements ITickable{
@ -90,7 +92,7 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
@Override
public void update(){
public final void update(){
this.updateEntity();
}
@ -99,15 +101,21 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
@Override
public Packet getDescriptionPacket(){
NBTTagCompound tag = new NBTTagCompound();
this.writeSyncableNBT(tag, true);
return new S35PacketUpdateTileEntity(this.pos, 3, tag);
public final Packet getDescriptionPacket(){
NBTTagCompound compound = this.getSyncCompound();
if(compound != null){
return new S35PacketUpdateTileEntity(this.pos, 3, compound);
}
else{
return null;
}
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt){
this.readSyncableNBT(pkt.getNbtCompound(), true);
public final void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt){
if(pkt != null){
this.receiveSyncCompound(pkt.getNbtCompound());
}
}
@Override
@ -127,12 +135,12 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
}
public void setRedstonePowered(boolean powered){
public final void setRedstonePowered(boolean powered){
this.isRedstonePowered = powered;
this.markDirty();
}
protected boolean sendUpdateWithInterval(){
protected final boolean sendUpdateWithInterval(){
if(this.ticksElapsed%ConfigIntValues.TILE_ENTITY_UPDATE_INTERVAL.getValue() == 0){
this.sendUpdate();
return true;
@ -142,7 +150,17 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
}
public void sendUpdate(){
VanillaPacketSyncer.sendTileToNearbyPlayers(this);
public final void sendUpdate(){
PacketHandler.theNetwork.sendToAllAround(new PacketUpdateTileEntity(this), new NetworkRegistry.TargetPoint(this.worldObj.provider.getDimensionId(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), 64));
}
public NBTTagCompound getSyncCompound(){
NBTTagCompound tag = new NBTTagCompound();
this.writeSyncableNBT(tag, true);
return tag;
}
public void receiveSyncCompound(NBTTagCompound compound){
this.readSyncableNBT(compound, true);
}
}

View file

@ -21,9 +21,6 @@ import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import io.netty.util.internal.ConcurrentSet;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.fml.relauncher.Side;
@ -58,7 +55,7 @@ public class TileEntityLaserRelay extends TileEntityBase implements IEnergyRecei
}
@Override
public Packet getDescriptionPacket(){
public NBTTagCompound getSyncCompound(){
NBTTagCompound compound = new NBTTagCompound();
BlockPos thisPos = this.pos;
@ -70,18 +67,18 @@ public class TileEntityLaserRelay extends TileEntityBase implements IEnergyRecei
list.appendTag(pair.writeToNBT());
}
compound.setTag("Connections", list);
return new S35PacketUpdateTileEntity(thisPos, 3, compound);
return compound;
}
return null;
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt){
public void receiveSyncCompound(NBTTagCompound compound){
BlockPos thisPos = this.pos;
if(pkt != null && pkt.getNbtCompound() != null){
if(compound != null){
LaserRelayConnectionHandler.getInstance().removeRelayFromNetwork(thisPos);
NBTTagList list = pkt.getNbtCompound().getTagList("Connections", 10);
NBTTagList list = compound.getTagList("Connections", 10);
for(int i = 0; i < list.tagCount(); i++){
LaserRelayConnectionHandler.ConnectionPair pair = LaserRelayConnectionHandler.ConnectionPair.readFromNBT(list.getCompoundTagAt(i));
LaserRelayConnectionHandler.getInstance().addConnection(pair.firstRelay, pair.secondRelay);