From 4b835f8df89bd1be3bb10d0bd39358f47b12a99b Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 17 Jan 2016 20:46:51 +0100 Subject: [PATCH] Custom packet handler for 98.7% less stack traces when joining worlds --- .../mod/network/PacketHandler.java | 1 + .../mod/network/PacketUpdateTileEntity.java | 79 +++++++++++++++++++ .../mod/network/VanillaPacketSyncer.java | 40 ---------- .../mod/tile/TileEntityBase.java | 42 +++++++--- .../mod/tile/TileEntityLaserRelay.java | 13 ++- 5 files changed, 115 insertions(+), 60 deletions(-) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketUpdateTileEntity.java delete mode 100644 src/main/java/de/ellpeck/actuallyadditions/mod/network/VanillaPacketSyncer.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java b/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java index 7d5c5fb26..143233276 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java @@ -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); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketUpdateTileEntity.java b/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketUpdateTileEntity.java new file mode 100644 index 000000000..2f46f58bc --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketUpdateTileEntity.java @@ -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{ + + @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; + } + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/network/VanillaPacketSyncer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/network/VanillaPacketSyncer.java deleted file mode 100644 index 05e2b0feb..000000000 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/network/VanillaPacketSyncer.java +++ /dev/null @@ -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()); - } - } -} diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java index 2309c7621..4d50427eb 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java @@ -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); } } \ No newline at end of file diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java index 39bf833f4..977d1e20a 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java @@ -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);