aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/net
diff options
context:
space:
mode:
authorMatt <mtthw8198@gmail.com>2021-06-01 14:18:25 -0700
committerMatt <mtthw8198@gmail.com>2021-06-01 14:18:25 -0700
commitf87217e9a044b0f017d4b821017f697ca6568f42 (patch)
treeaad0a14640e146837c950157750e3ad6cca88ea5 /src/main/java/gregtech/api/net
parent08aa6a7f525eea881a00331bc75f055c3d275c3e (diff)
parent9555f77feccabf92d3bcc20c985c2060e2455600 (diff)
downloadGT5-Unofficial-f87217e9a044b0f017d4b821017f697ca6568f42.tar.gz
GT5-Unofficial-f87217e9a044b0f017d4b821017f697ca6568f42.tar.bz2
GT5-Unofficial-f87217e9a044b0f017d4b821017f697ca6568f42.zip
Merge remote-tracking branch 'origin/experimental' into experimental
Diffstat (limited to 'src/main/java/gregtech/api/net')
-rw-r--r--src/main/java/gregtech/api/net/GT_Packet.java28
-rw-r--r--src/main/java/gregtech/api/net/GT_Packet_Block_Event.java23
-rw-r--r--src/main/java/gregtech/api/net/GT_Packet_ClientPreference.java53
-rw-r--r--src/main/java/gregtech/api/net/GT_Packet_New.java28
-rw-r--r--src/main/java/gregtech/api/net/GT_Packet_Pollution.java18
-rw-r--r--src/main/java/gregtech/api/net/GT_Packet_Sound.java36
-rw-r--r--src/main/java/gregtech/api/net/GT_Packet_TileEntity.java45
-rw-r--r--src/main/java/gregtech/api/net/GT_Packet_TileEntityCover.java28
-rw-r--r--src/main/java/gregtech/api/net/GT_Packet_TileEntityCoverGUI.java30
-rw-r--r--src/main/java/gregtech/api/net/IGT_NetworkHandler.java11
10 files changed, 197 insertions, 103 deletions
diff --git a/src/main/java/gregtech/api/net/GT_Packet.java b/src/main/java/gregtech/api/net/GT_Packet.java
index 6fdd013d8f..48cc171d61 100644
--- a/src/main/java/gregtech/api/net/GT_Packet.java
+++ b/src/main/java/gregtech/api/net/GT_Packet.java
@@ -1,8 +1,14 @@
package gregtech.api.net;
import com.google.common.io.ByteArrayDataInput;
+import io.netty.buffer.ByteBuf;
+import net.minecraft.network.INetHandler;
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 +23,33 @@ 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);
-} \ No newline at end of file
+
+ /**
+ * This will be called just before {@link #process(IBlockAccess)} to inform the handler about the source and type of connection
+ */
+ public void setINetHandler(INetHandler aHandler) {}
+}
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 d8cab48bd0..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());
}
@@ -55,4 +52,4 @@ public class GT_Packet_Block_Event extends GT_Packet {
public byte getPacketID() {
return 2;
}
-} \ No newline at end of file
+}
diff --git a/src/main/java/gregtech/api/net/GT_Packet_ClientPreference.java b/src/main/java/gregtech/api/net/GT_Packet_ClientPreference.java
new file mode 100644
index 0000000000..a97cfb207b
--- /dev/null
+++ b/src/main/java/gregtech/api/net/GT_Packet_ClientPreference.java
@@ -0,0 +1,53 @@
+package gregtech.api.net;
+
+import com.google.common.io.ByteArrayDataInput;
+import gregtech.GT_Mod;
+import gregtech.api.util.GT_ClientPreference;
+import io.netty.buffer.ByteBuf;
+import net.minecraft.entity.player.EntityPlayerMP;
+import net.minecraft.network.INetHandler;
+import net.minecraft.network.NetHandlerPlayServer;
+import net.minecraft.world.IBlockAccess;
+
+public class GT_Packet_ClientPreference extends GT_Packet_New {
+ private GT_ClientPreference mPreference;
+ private EntityPlayerMP mPlayer;
+
+ public GT_Packet_ClientPreference() {
+ super(true);
+ }
+
+ public GT_Packet_ClientPreference(GT_ClientPreference mPreference) {
+ super(false);
+ this.mPreference = mPreference;
+ }
+
+ @Override
+ public byte getPacketID() {
+ return 9;
+ }
+
+ @Override
+ public void setINetHandler(INetHandler aHandler) {
+ if (aHandler instanceof NetHandlerPlayServer) {
+ mPlayer = ((NetHandlerPlayServer) aHandler).playerEntity;
+ }
+ }
+
+ @Override
+ public void process(IBlockAccess aWorld) {
+ if (mPlayer != null)
+ GT_Mod.gregtechproxy.setClientPreference(mPlayer.getUniqueID(), mPreference);
+ }
+
+ @Override
+ public void encode(ByteBuf aOut) {
+ aOut.writeBoolean(mPreference.isSingleBlockInitialFilterEnabled());
+ aOut.writeBoolean(mPreference.isInputBusInitialFilterEnabled());
+ }
+
+ @Override
+ public GT_Packet_New decode(ByteArrayDataInput aData) {
+ return new GT_Packet_ClientPreference(new GT_ClientPreference(aData.readBoolean(), aData.readBoolean()));
+ }
+}
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 7f717c1a47..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()
@@ -48,4 +42,4 @@ public class GT_Packet_Pollution extends GT_Packet {
public byte getPacketID() {
return 4;
}
-} \ No newline at end of file
+}
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 5490d0d390..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());
}
@@ -52,4 +60,4 @@ public class GT_Packet_Sound extends GT_Packet {
public byte getPacketID() {
return 1;
}
-} \ No newline at end of file
+}
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 457bd7f21d..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());
}
@@ -104,4 +99,4 @@ public class GT_Packet_TileEntity extends GT_Packet {
public byte getPacketID() {
return 0;
}
-} \ No newline at end of file
+}
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 bc688f47ed..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(),
@@ -96,4 +92,4 @@ public class GT_Packet_TileEntityCover extends GT_Packet {
}
}
}
-} \ No newline at end of file
+}
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 58ebf95ac6..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(),
@@ -122,4 +118,4 @@ public class GT_Packet_TileEntityCoverGUI extends GT_Packet {
}
}
}
-} \ No newline at end of file
+}
diff --git a/src/main/java/gregtech/api/net/IGT_NetworkHandler.java b/src/main/java/gregtech/api/net/IGT_NetworkHandler.java
index 488e25591f..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);
-} \ No newline at end of file
+ void sendPacketToAllPlayersInRange(World aWorld, GT_Packet aPacket, int aX, int aZ);
+}