diff options
author | Glease <4586901+Glease@users.noreply.github.com> | 2021-05-19 01:57:54 +0800 |
---|---|---|
committer | Glease <4586901+Glease@users.noreply.github.com> | 2021-05-19 02:53:16 +0800 |
commit | ce0818a785bfbe928ca08e451e0b2d486cd32cdf (patch) | |
tree | 86c02f17ae60e52138b3e52339a99c2c35dd5d3b /src/main/java/gregtech/api/net | |
parent | 03cf58782d86d31e9feae88baa4939a70c4bee42 (diff) | |
download | GT5-Unofficial-ce0818a785bfbe928ca08e451e0b2d486cd32cdf.tar.gz GT5-Unofficial-ce0818a785bfbe928ca08e451e0b2d486cd32cdf.tar.bz2 GT5-Unofficial-ce0818a785bfbe928ca08e451e0b2d486cd32cdf.zip |
Get rid of intermediate byte array while sending packet
Diffstat (limited to 'src/main/java/gregtech/api/net')
9 files changed, 130 insertions, 95 deletions
diff --git a/src/main/java/gregtech/api/net/GT_Packet.java b/src/main/java/gregtech/api/net/GT_Packet.java index e2b7b247b2..4bf435ca0c 100644 --- a/src/main/java/gregtech/api/net/GT_Packet.java +++ b/src/main/java/gregtech/api/net/GT_Packet.java @@ -1,8 +1,13 @@ package gregtech.api.net; import com.google.common.io.ByteArrayDataInput; +import io.netty.buffer.ByteBuf; import net.minecraft.world.IBlockAccess; +/** + * @deprecated Use {@link GT_Packet_New} instead + */ +@Deprecated public abstract class GT_Packet { public GT_Packet(boolean aIsReference) { // @@ -17,13 +22,28 @@ public abstract class GT_Packet { /** * @return encoded byte Stream + * @deprecated Use {@link #encode(ByteBuf)} instead */ + @Deprecated public abstract byte[] encode(); /** + * Encode the data into given byte buffer without creating an intermediate byte array. + * Default implementation just throw {@link UnsupportedOperationException}. + */ + public void encode(ByteBuf aOut) { + throw new UnsupportedOperationException(); + } + + /** * @return encoded byte Stream */ public abstract GT_Packet decode(ByteArrayDataInput aData); + /** + * Process the packet + * + * @param aWorld null if message is received on server side, the client world if message is received on client side + */ public abstract void process(IBlockAccess aWorld); } diff --git a/src/main/java/gregtech/api/net/GT_Packet_Block_Event.java b/src/main/java/gregtech/api/net/GT_Packet_Block_Event.java index 5a804b9511..9060144e22 100644 --- a/src/main/java/gregtech/api/net/GT_Packet_Block_Event.java +++ b/src/main/java/gregtech/api/net/GT_Packet_Block_Event.java @@ -1,15 +1,14 @@ package gregtech.api.net; import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; +import io.netty.buffer.ByteBuf; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.IBlockAccess; /** * Used to transfer Block Events in a much better fashion */ -public class GT_Packet_Block_Event extends GT_Packet { +public class GT_Packet_Block_Event extends GT_Packet_New { private int mX, mZ; private short mY; private byte mID, mValue; @@ -28,18 +27,16 @@ public class GT_Packet_Block_Event extends GT_Packet { } @Override - public byte[] encode() { - ByteArrayDataOutput tOut = ByteStreams.newDataOutput(10); - tOut.writeInt(mX); - tOut.writeShort(mY); - tOut.writeInt(mZ); - tOut.writeByte(mID); - tOut.writeByte(mValue); - return tOut.toByteArray(); + public void encode(ByteBuf aOut) { + aOut.writeInt(mX); + aOut.writeShort(mY); + aOut.writeInt(mZ); + aOut.writeByte(mID); + aOut.writeByte(mValue); } @Override - public GT_Packet decode(ByteArrayDataInput aData) { + public GT_Packet_New decode(ByteArrayDataInput aData) { return new GT_Packet_Block_Event(aData.readInt(), aData.readShort(), aData.readInt(), aData.readByte(), aData.readByte()); } diff --git a/src/main/java/gregtech/api/net/GT_Packet_New.java b/src/main/java/gregtech/api/net/GT_Packet_New.java new file mode 100644 index 0000000000..59f6dc7251 --- /dev/null +++ b/src/main/java/gregtech/api/net/GT_Packet_New.java @@ -0,0 +1,28 @@ +package gregtech.api.net; + +import com.google.common.io.ByteArrayDataInput; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +@SuppressWarnings("deprecation") +public abstract class GT_Packet_New extends GT_Packet { + public GT_Packet_New(boolean aIsReference) { + super(aIsReference); + } + + @Override + @Deprecated + public final byte[] encode() { + ByteBuf tOut = Unpooled.buffer(); + encode(tOut); + byte[] bytes = new byte[tOut.readableBytes()]; + tOut.readBytes(bytes); + return bytes; + } + + @Override + public abstract void encode(ByteBuf aOut); + + @Override + public abstract GT_Packet_New decode(ByteArrayDataInput aData); +} diff --git a/src/main/java/gregtech/api/net/GT_Packet_Pollution.java b/src/main/java/gregtech/api/net/GT_Packet_Pollution.java index 0a365bf818..20efe41679 100644 --- a/src/main/java/gregtech/api/net/GT_Packet_Pollution.java +++ b/src/main/java/gregtech/api/net/GT_Packet_Pollution.java @@ -2,12 +2,11 @@ package gregtech.api.net; import com.google.common.io.ByteArrayDataInput; import gregtech.common.GT_Client; +import io.netty.buffer.ByteBuf; import net.minecraft.world.ChunkCoordIntPair; import net.minecraft.world.IBlockAccess; -import java.nio.ByteBuffer; - -public class GT_Packet_Pollution extends GT_Packet { +public class GT_Packet_Pollution extends GT_Packet_New { private ChunkCoordIntPair chunk; private int pollution; @@ -22,17 +21,12 @@ public class GT_Packet_Pollution extends GT_Packet { } @Override - public byte[] encode() { - return ByteBuffer - .allocate(12) - .putInt(chunk.chunkXPos) - .putInt(chunk.chunkZPos) - .putInt(pollution) - .array(); + public void encode(ByteBuf aOut) { + aOut.writeInt(chunk.chunkXPos).writeInt(chunk.chunkZPos).writeInt(pollution); } @Override - public GT_Packet decode(ByteArrayDataInput aData) { + public GT_Packet_New decode(ByteArrayDataInput aData) { return new GT_Packet_Pollution( new ChunkCoordIntPair(aData.readInt(), aData.readInt()), aData.readInt() diff --git a/src/main/java/gregtech/api/net/GT_Packet_Sound.java b/src/main/java/gregtech/api/net/GT_Packet_Sound.java index be381dbf69..be66400609 100644 --- a/src/main/java/gregtech/api/net/GT_Packet_Sound.java +++ b/src/main/java/gregtech/api/net/GT_Packet_Sound.java @@ -1,12 +1,16 @@ package gregtech.api.net; import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; +import gregtech.api.util.GT_Log; import gregtech.api.util.GT_Utility; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufOutputStream; import net.minecraft.world.IBlockAccess; -public class GT_Packet_Sound extends GT_Packet { +import java.io.DataOutput; +import java.io.IOException; + +public class GT_Packet_Sound extends GT_Packet_New { private int mX, mZ; private short mY; private String mSoundName; @@ -27,19 +31,23 @@ public class GT_Packet_Sound extends GT_Packet { } @Override - public byte[] encode() { - ByteArrayDataOutput tOut = ByteStreams.newDataOutput(10); - tOut.writeUTF(mSoundName); - tOut.writeFloat(mSoundStrength); - tOut.writeFloat(mSoundPitch); - tOut.writeInt(mX); - tOut.writeShort(mY); - tOut.writeInt(mZ); - return tOut.toByteArray(); + public void encode(ByteBuf aOut) { + DataOutput tOut = new ByteBufOutputStream(aOut); + try { + tOut.writeUTF(mSoundName); + tOut.writeFloat(mSoundStrength); + tOut.writeFloat(mSoundPitch); + tOut.writeInt(mX); + tOut.writeShort(mY); + tOut.writeInt(mZ); + } catch (IOException e) { + // this really shouldn't happen, but whatever + e.printStackTrace(GT_Log.err); + } } @Override - public GT_Packet decode(ByteArrayDataInput aData) { + public GT_Packet_New decode(ByteArrayDataInput aData) { return new GT_Packet_Sound(aData.readUTF(), aData.readFloat(), aData.readFloat(), aData.readInt(), aData.readShort(), aData.readInt()); } diff --git a/src/main/java/gregtech/api/net/GT_Packet_TileEntity.java b/src/main/java/gregtech/api/net/GT_Packet_TileEntity.java index 6d71cbd818..051be672ab 100644 --- a/src/main/java/gregtech/api/net/GT_Packet_TileEntity.java +++ b/src/main/java/gregtech/api/net/GT_Packet_TileEntity.java @@ -1,14 +1,13 @@ package gregtech.api.net; import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; import gregtech.api.metatileentity.BaseMetaPipeEntity; import gregtech.api.metatileentity.BaseMetaTileEntity; +import io.netty.buffer.ByteBuf; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.IBlockAccess; -public class GT_Packet_TileEntity extends GT_Packet { +public class GT_Packet_TileEntity extends GT_Packet_New { private int mX, mZ, mC0, mC1, mC2, mC3, mC4, mC5; private short mY, mID; private byte mTexture, mTexturePage, mUpdate, mRedstone, mColor; @@ -58,32 +57,28 @@ public class GT_Packet_TileEntity extends GT_Packet { } @Override - public byte[] encode() { - ByteArrayDataOutput tOut = ByteStreams.newDataOutput(41); + public void encode(ByteBuf aOut) { + aOut.writeInt(mX); + aOut.writeShort(mY); + aOut.writeInt(mZ); + aOut.writeShort(mID); - tOut.writeInt(mX); - tOut.writeShort(mY); - tOut.writeInt(mZ); - tOut.writeShort(mID); + aOut.writeInt(mC0); + aOut.writeInt(mC1); + aOut.writeInt(mC2); + aOut.writeInt(mC3); + aOut.writeInt(mC4); + aOut.writeInt(mC5); - tOut.writeInt(mC0); - tOut.writeInt(mC1); - tOut.writeInt(mC2); - tOut.writeInt(mC3); - tOut.writeInt(mC4); - tOut.writeInt(mC5); - - tOut.writeByte(mTexture); - tOut.writeByte(mTexturePage); - tOut.writeByte(mUpdate); - tOut.writeByte(mRedstone); - tOut.writeByte(mColor); - - return tOut.toByteArray(); + aOut.writeByte(mTexture); + aOut.writeByte(mTexturePage); + aOut.writeByte(mUpdate); + aOut.writeByte(mRedstone); + aOut.writeByte(mColor); } @Override - public GT_Packet decode(ByteArrayDataInput aData) { + public GT_Packet_New decode(ByteArrayDataInput aData) { return new GT_Packet_TileEntity(aData.readInt(), aData.readShort(), aData.readInt(), aData.readShort(), aData.readInt(), aData.readInt(), aData.readInt(), aData.readInt(), aData.readInt(), aData.readInt(), aData.readByte(), aData.readByte(), aData.readByte(), aData.readByte(), aData.readByte()); } diff --git a/src/main/java/gregtech/api/net/GT_Packet_TileEntityCover.java b/src/main/java/gregtech/api/net/GT_Packet_TileEntityCover.java index df64444622..29220e05ab 100644 --- a/src/main/java/gregtech/api/net/GT_Packet_TileEntityCover.java +++ b/src/main/java/gregtech/api/net/GT_Packet_TileEntityCover.java @@ -1,10 +1,9 @@ package gregtech.api.net; import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; import gregtech.api.interfaces.tileentity.ICoverable; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import io.netty.buffer.ByteBuf; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; @@ -14,7 +13,7 @@ import net.minecraftforge.common.DimensionManager; * Client -> Server: Update cover data */ -public class GT_Packet_TileEntityCover extends GT_Packet { +public class GT_Packet_TileEntityCover extends GT_Packet_New { protected int mX; protected short mY; protected int mZ; @@ -57,23 +56,20 @@ public class GT_Packet_TileEntityCover extends GT_Packet { } @Override - public byte[] encode() { - ByteArrayDataOutput tOut = ByteStreams.newDataOutput(4+2+4+1+4+4+4); - tOut.writeInt(mX); - tOut.writeShort(mY); - tOut.writeInt(mZ); + public void encode(ByteBuf aOut) { + aOut.writeInt(mX); + aOut.writeShort(mY); + aOut.writeInt(mZ); - tOut.writeByte(side); - tOut.writeInt(coverID); - tOut.writeInt(coverData); + aOut.writeByte(side); + aOut.writeInt(coverID); + aOut.writeInt(coverData); - tOut.writeInt(dimID); - - return tOut.toByteArray(); + aOut.writeInt(dimID); } @Override - public GT_Packet decode(ByteArrayDataInput aData) { + public GT_Packet_New decode(ByteArrayDataInput aData) { return new GT_Packet_TileEntityCover( aData.readInt(), aData.readShort(), diff --git a/src/main/java/gregtech/api/net/GT_Packet_TileEntityCoverGUI.java b/src/main/java/gregtech/api/net/GT_Packet_TileEntityCoverGUI.java index 642e9102b8..2a8091144d 100644 --- a/src/main/java/gregtech/api/net/GT_Packet_TileEntityCoverGUI.java +++ b/src/main/java/gregtech/api/net/GT_Packet_TileEntityCoverGUI.java @@ -1,12 +1,11 @@ package gregtech.api.net; import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; import gregtech.api.enums.GT_Values; import gregtech.api.interfaces.tileentity.ICoverable; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.common.GT_Proxy; +import io.netty.buffer.ByteBuf; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.entity.player.EntityPlayerMP; @@ -18,7 +17,7 @@ import net.minecraft.world.World; * Server -> Client: Show GUI */ -public class GT_Packet_TileEntityCoverGUI extends GT_Packet { +public class GT_Packet_TileEntityCoverGUI extends GT_Packet_New { protected int mX; protected short mY; protected int mZ; @@ -79,24 +78,21 @@ public class GT_Packet_TileEntityCoverGUI extends GT_Packet { } @Override - public byte[] encode() { - ByteArrayDataOutput tOut = ByteStreams.newDataOutput(4+2+4+1+4+4+4+4); - tOut.writeInt(mX); - tOut.writeShort(mY); - tOut.writeInt(mZ); + public void encode(ByteBuf aOut) { + aOut.writeInt(mX); + aOut.writeShort(mY); + aOut.writeInt(mZ); - tOut.writeByte(side); - tOut.writeInt(coverID); - tOut.writeInt(coverData); + aOut.writeByte(side); + aOut.writeInt(coverID); + aOut.writeInt(coverData); - tOut.writeInt(dimID); - tOut.writeInt(playerID); - - return tOut.toByteArray(); + aOut.writeInt(dimID); + aOut.writeInt(playerID); } @Override - public GT_Packet decode(ByteArrayDataInput aData) { + public GT_Packet_New decode(ByteArrayDataInput aData) { return new GT_Packet_TileEntityCoverGUI( aData.readInt(), aData.readShort(), diff --git a/src/main/java/gregtech/api/net/IGT_NetworkHandler.java b/src/main/java/gregtech/api/net/IGT_NetworkHandler.java index d35fedf7f9..2949ec6791 100644 --- a/src/main/java/gregtech/api/net/IGT_NetworkHandler.java +++ b/src/main/java/gregtech/api/net/IGT_NetworkHandler.java @@ -4,12 +4,13 @@ import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.world.World; +@SuppressWarnings("deprecation") public interface IGT_NetworkHandler { - public void sendToPlayer(GT_Packet aPacket, EntityPlayerMP aPlayer); + void sendToPlayer(GT_Packet aPacket, EntityPlayerMP aPlayer); - public void sendToAllAround(GT_Packet aPacket, TargetPoint aPosition); + void sendToAllAround(GT_Packet aPacket, TargetPoint aPosition); - public void sendToServer(GT_Packet aPacket); + void sendToServer(GT_Packet aPacket); - public void sendPacketToAllPlayersInRange(World aWorld, GT_Packet aPacket, int aX, int aZ); + void sendPacketToAllPlayersInRange(World aWorld, GT_Packet aPacket, int aX, int aZ); } |