diff --git a/src/main/java/gregtechmod/api/metatileentity/BaseMetaPipeEntity.java b/src/main/java/gregtechmod/api/metatileentity/BaseMetaPipeEntity.java index d45cf86..6be6601 100644 --- a/src/main/java/gregtechmod/api/metatileentity/BaseMetaPipeEntity.java +++ b/src/main/java/gregtechmod/api/metatileentity/BaseMetaPipeEntity.java @@ -10,6 +10,7 @@ import gregtechmod.api.util.GT_OreDictUnificator; import gregtechmod.api.util.GT_Utility; import java.util.ArrayList; +import java.util.Arrays; import net.minecraft.block.Block; import net.minecraft.entity.item.EntityItem; @@ -334,19 +335,14 @@ public class BaseMetaPipeEntity extends BaseTileEntity implements IGregTechTileE return rPacket; } - public final void receiveMetaTileEntityData(short aID, int aCover0, int aCover1, int aCover2, int aCover3, int aCover4, int aCover5, byte aTextureData, byte aUpdateData, byte aRedstoneData, byte aColorData) { + public final void receiveMetaTileEntityData(short aID, int[] aCovers, byte aTextureData, byte aUpdateData, byte aRedstoneData, byte aColorData) { issueTextureUpdate(); if (mID != aID && aID > 0) { mID = aID; createNewMetatileEntity(mID); } - mCoverSides[0] = aCover0; - mCoverSides[1] = aCover1; - mCoverSides[2] = aCover2; - mCoverSides[3] = aCover3; - mCoverSides[4] = aCover4; - mCoverSides[5] = aCover5; + mCoverSides = Arrays.copyOf(aCovers, aCovers.length); receiveClientEvent(0, aTextureData); receiveClientEvent(1, aUpdateData); diff --git a/src/main/java/gregtechmod/api/metatileentity/BaseMetaTileEntity.java b/src/main/java/gregtechmod/api/metatileentity/BaseMetaTileEntity.java index 75b6fcc..90be082 100644 --- a/src/main/java/gregtechmod/api/metatileentity/BaseMetaTileEntity.java +++ b/src/main/java/gregtechmod/api/metatileentity/BaseMetaTileEntity.java @@ -8,8 +8,10 @@ import gregtechmod.api.util.GT_Log; import gregtechmod.api.util.GT_ModHandler; import gregtechmod.api.util.GT_OreDictUnificator; import gregtechmod.api.util.GT_Utility; +import gregtechmod.common.network.packet.GT_TileEntityPacket; import java.util.ArrayList; +import java.util.Arrays; import net.minecraft.block.Block; import net.minecraft.entity.item.EntityItem; @@ -20,7 +22,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.network.Packet; -import net.minecraft.network.play.server.S3FPacketCustomPayload; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; import net.minecraft.world.EnumSkyBlock; @@ -29,9 +30,6 @@ import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidTankInfo; -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; - /** * NEVER INCLUDE THIS FILE IN YOUR MOD!!! * @@ -468,7 +466,7 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE if (isServerSide()) { if (getTimer() % 10 == 0) { if (mSendClientData) { - GT_Utility.sendPacketToAllPlayersInRange(getWorld(), getDescriptionPacket(), getXCoord(), getZCoord()); + GT_Utility.sendPacketToAllPlayersInRange(getWorld(), getMetaTileEntityData(), getXCoord(), getZCoord()); mSendClientData = false; } } @@ -505,44 +503,60 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE @Override public Packet getDescriptionPacket() { - ByteArrayDataOutput tOut = ByteStreams.newDataOutput(); - - tOut.writeInt(getXCoord()); - tOut.writeShort(getYCoord()); - tOut.writeInt(getZCoord()); - - tOut.writeShort(mID); - tOut.writeInt(mCoverSides[0]); - tOut.writeInt(mCoverSides[1]); - tOut.writeInt(mCoverSides[2]); - tOut.writeInt(mCoverSides[3]); - tOut.writeInt(mCoverSides[4]); - tOut.writeInt(mCoverSides[5]); - - tOut.writeByte(oTextureData = (byte)((getFrontFacing()&7) | (mActive?8:0) | (mRedstone?16:0) | (mLockUpgrade?32:0))); - tOut.writeByte(oUpdateData = hasValidMetaTileEntity()?mMetaTileEntity.getUpdateData():0); - tOut.writeByte(oRedstoneData = (byte)(((mSidedRedstone[0]>0)?1:0)|((mSidedRedstone[1]>0)?2:0)|((mSidedRedstone[2]>0)?4:0)|((mSidedRedstone[3]>0)?8:0)|((mSidedRedstone[4]>0)?16:0)|((mSidedRedstone[5]>0)?32:0))); - tOut.writeByte(oColor = mColor); - - oLightValue = -1; - - S3FPacketCustomPayload rPacket = new S3FPacketCustomPayload(GregTech_API.TILEENTITY_PACKET_CHANNEL, tOut.toByteArray()); - return rPacket; +// ByteArrayDataOutput tOut = ByteStreams.newDataOutput(); +// +// tOut.writeInt(getXCoord()); +// tOut.writeShort(getYCoord()); +// tOut.writeInt(getZCoord()); +// +// tOut.writeShort(mID); +// tOut.writeInt(mCoverSides[0]); +// tOut.writeInt(mCoverSides[1]); +// tOut.writeInt(mCoverSides[2]); +// tOut.writeInt(mCoverSides[3]); +// tOut.writeInt(mCoverSides[4]); +// tOut.writeInt(mCoverSides[5]); +// +// tOut.writeByte(oTextureData = (byte)((getFrontFacing()&7) | (mActive?8:0) | (mRedstone?16:0) | (mLockUpgrade?32:0))); +// tOut.writeByte(oUpdateData = hasValidMetaTileEntity()?mMetaTileEntity.getUpdateData():0); +// tOut.writeByte(oRedstoneData = (byte)(((mSidedRedstone[0]>0)?1:0)|((mSidedRedstone[1]>0)?2:0)|((mSidedRedstone[2]>0)?4:0)|((mSidedRedstone[3]>0)?8:0)|((mSidedRedstone[4]>0)?16:0)|((mSidedRedstone[5]>0)?32:0))); +// tOut.writeByte(oColor = mColor); +// +// oLightValue = -1; +// +// S3FPacketCustomPayload rPacket = new S3FPacketCustomPayload(GregTech_API.TILEENTITY_PACKET_CHANNEL, tOut.toByteArray()); +// return rPacket; + return super.getDescriptionPacket(); } - public final void receiveMetaTileEntityData(short aID, int aCover0, int aCover1, int aCover2, int aCover3, int aCover4, int aCover5, byte aTextureData, byte aUpdateData, byte aRedstoneData, byte aColorData) { + public final GT_TileEntityPacket getMetaTileEntityData() { + GT_TileEntityPacket tOut = new GT_TileEntityPacket(); + tOut.aX = getXCoord(); + tOut.aY = getYCoord(); + tOut.aZ = getZCoord(); + tOut.aID = mID; + tOut.aCovers = mCoverSides; + tOut.aTextureData = (byte)((getFrontFacing() & 7) | (mActive ? 8 : 0) | (mRedstone ? 16 : 0) | (mLockUpgrade ? 32 : 0)); + tOut.aUpdateData = (oUpdateData = hasValidMetaTileEntity() ? mMetaTileEntity.getUpdateData() : 0); + tOut.aRedstoneData = (oRedstoneData = (byte) + (((mSidedRedstone[0] >0 ) ? 1: 0) | + ((mSidedRedstone[1] > 0) ? 2 : 0) | + ((mSidedRedstone[2] > 0) ? 4 : 0) | + ((mSidedRedstone[3] >0) ? 8 : 0) | + ((mSidedRedstone[4] > 0) ? 16 : 0) | + ((mSidedRedstone[5] > 0) ? 32 : 0))); + tOut.aRedstoneData = (oColor = mColor); + return tOut; + } + + public final void receiveMetaTileEntityData(short aID, int[] aCovers, byte aTextureData, byte aUpdateData, byte aRedstoneData, byte aColorData) { issueTextureUpdate(); if (mID != aID && aID > 0) { mID = aID; createNewMetatileEntity(mID); } - mCoverSides[0] = aCover0; - mCoverSides[1] = aCover1; - mCoverSides[2] = aCover2; - mCoverSides[3] = aCover3; - mCoverSides[4] = aCover4; - mCoverSides[5] = aCover5; + mCoverSides = Arrays.copyOf(aCovers, aCovers.length); receiveClientEvent(0, aTextureData); receiveClientEvent(1, aUpdateData); diff --git a/src/main/java/gregtechmod/api/util/GT_Utility.java b/src/main/java/gregtechmod/api/util/GT_Utility.java index 24f8247..b34ef7a 100644 --- a/src/main/java/gregtechmod/api/util/GT_Utility.java +++ b/src/main/java/gregtechmod/api/util/GT_Utility.java @@ -9,6 +9,7 @@ import gregtechmod.api.interfaces.IMachineProgress; import gregtechmod.api.interfaces.IUpgradableMachine; import gregtechmod.api.items.GT_EnergyArmor_Item; import gregtechmod.common.network.GT_PacketHandler; +import gregtechmod.common.network.packet.GT_Packet; import gregtechmod.common.network.packet.GT_SoundPacket; import java.lang.reflect.Field; @@ -676,15 +677,6 @@ public class GT_Utility { public static boolean sendSoundToPlayers(World aWorld, String aSoundName, float aSoundStrength, float aSoundModulation, int aX, int aY, int aZ) { if (aSoundName == null || aSoundName.equals("") || aWorld == null || aWorld.isRemote) return false; -// ByteArrayDataOutput tOut = ByteStreams.newDataOutput(); -// -// tOut.writeInt(aX); -// tOut.writeShort(aY); -// tOut.writeInt(aZ); -// -// tOut.writeUTF(aSoundName); -// tOut.writeFloat(aSoundStrength); -// tOut.writeFloat(aSoundModulation < 0 ? (1.0F + (aWorld.rand.nextFloat() - aWorld.rand.nextFloat()) * 0.2F) * 0.7F : aSoundModulation); aSoundModulation = aSoundModulation < 0 ? (1.0F + (aWorld.rand.nextFloat() - aWorld.rand.nextFloat()) * 0.2F) * 0.7F : aSoundModulation; GT_SoundPacket tPacket = new GT_SoundPacket(aSoundName, aX, aY, aZ, aSoundStrength, aSoundModulation); sendPacketToAllPlayersInRange(aWorld, tPacket, aX, aZ); @@ -692,7 +684,7 @@ public class GT_Utility { return true; } - public static void sendPacketToAllPlayersInRange(World aWorld, GT_SoundPacket aPacket, int aX, int aZ) { + public static void sendPacketToAllPlayersInRange(World aWorld, GT_Packet aPacket, int aX, int aZ) { for (Object tObject : aWorld.playerEntities) { if (tObject instanceof EntityPlayerMP) { EntityPlayerMP tPlayer = (EntityPlayerMP)tObject; @@ -700,7 +692,7 @@ public class GT_Utility { Chunk tChunk = aWorld.getChunkFromBlockCoords(aX, aZ); if (tPlayer.getServerForPlayer().getPlayerManager().isPlayerWatchingChunk(tPlayer, tChunk.xPosition, tChunk.zPosition)) { if (GregTech_API.DEBUG_MODE) GT_Log.log.debug("sent Packet to " + tPlayer.getDisplayName()); - GT_PacketHandler.SOUND_PACKET_CHANNEL.sendTo(aPacket, tPlayer); + GT_PacketHandler.sendPacket(aPacket, tPlayer); } } } else { diff --git a/src/main/java/gregtechmod/common/network/GT_NetworkHandler.java b/src/main/java/gregtechmod/common/network/GT_NetworkHandler.java index ab73164..afb27b3 100644 --- a/src/main/java/gregtechmod/common/network/GT_NetworkHandler.java +++ b/src/main/java/gregtechmod/common/network/GT_NetworkHandler.java @@ -4,18 +4,40 @@ import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; import cpw.mods.fml.common.network.simpleimpl.MessageContext; import gregtechmod.api.GregTech_API; +import gregtechmod.api.metatileentity.BaseMetaPipeEntity; +import gregtechmod.api.metatileentity.BaseMetaTileEntity; import gregtechmod.api.util.GT_Log; import gregtechmod.api.util.GT_Utility; import gregtechmod.common.network.packet.GT_Packet; import gregtechmod.common.network.packet.GT_SoundPacket; import gregtechmod.common.network.packet.GT_TileEntityPacket; +import net.minecraft.tileentity.TileEntity; +/** + * Used to handle packets on client side + * @author iDnK + * + * @param should be GT_Packet or their child class + */ public abstract class GT_NetworkHandler implements IMessageHandler { @Override public IMessage onMessage(T message, MessageContext ctx) { if (message instanceof GT_TileEntityPacket) { - // TODO Packets + GT_TileEntityPacket packet = (GT_TileEntityPacket) message; + if (GT_Utility.sCurrentPlayer != null && GT_Utility.sCurrentPlayer.worldObj != null) { + String name = GT_Utility.sCurrentPlayer.getDisplayName(); + if (GregTech_API.DEBUG_MODE && (name.equals("Player") || name.equals("GregoriusT") || name.equals("The_DnK"))) + GT_Log.log.info(String.format("Received initial MetaTileEntity Data: %s Bytes @ (%d;%d;%d) during Tick: %s", packet.data.capacity(), packet.aX, packet.aY, packet.aZ, GregTech_API.sClientTickCounter)); + TileEntity tTile = GT_Utility.sCurrentPlayer.worldObj.getTileEntity(packet.aX, packet.aY, packet.aZ); + if (tTile != null) { + if (tTile instanceof BaseMetaTileEntity) { + ((BaseMetaTileEntity) tTile).receiveMetaTileEntityData(packet.aID, packet.aCovers, packet.aTextureData, packet.aUpdateData, packet.aRedstoneData, packet.aColorData); + } else if (tTile instanceof BaseMetaPipeEntity) { + ((BaseMetaPipeEntity) tTile).receiveMetaTileEntityData(packet.aID, packet.aCovers, packet.aTextureData, packet.aUpdateData, packet.aRedstoneData, packet.aColorData); + } + } + } } else if (message instanceof GT_SoundPacket) { GT_SoundPacket sound = (GT_SoundPacket) message; if (GT_Utility.sCurrentPlayer != null) { @@ -27,22 +49,6 @@ public abstract class GT_NetworkHandler implements IMessage } -// public void onPacketData(INetworkManager aManager, Packet250CustomPayload aPacket, Player aPlayer) { -// if (aPacket.channel.equals(GregTech_API.TILEENTITY_PACKET_CHANNEL)) { -// ByteArrayDataInput tIn = ByteStreams.newDataInput(aPacket.data); -// int tX = tIn.readInt(), tY = tIn.readShort(), tZ = tIn.readInt(); -// if (aPlayer != null && aPlayer instanceof EntityPlayer) { -// if (GregTech_API.DEBUG_MODE && (((EntityPlayer)aPlayer).username.equals("Player") || ((EntityPlayer)aPlayer).username.equals("GregoriusT"))) GT_Log.out.println("Received initial MetaTileEntity Data: " + aPacket.length + " Bytes @ (" + tX + ";" + tY + ";" + tZ + ") during Tick: " + GregTech_API.sClientTickCounter); -// TileEntity tTileEntity = ((EntityPlayer)aPlayer).worldObj.getBlockTileEntity(tX, tY, tZ); -// if (tTileEntity != null) { -// if (tTileEntity instanceof BaseMetaTileEntity) ((BaseMetaTileEntity)tTileEntity).receiveMetaTileEntityData(tIn.readShort(), tIn.readInt(), tIn.readInt(), tIn.readInt(), tIn.readInt(), tIn.readInt(), tIn.readInt(), tIn.readByte(), tIn.readByte(), tIn.readByte(), tIn.readByte()); -// else if (tTileEntity instanceof BaseMetaPipeEntity) ((BaseMetaPipeEntity)tTileEntity).receiveMetaTileEntityData(tIn.readShort(), tIn.readInt(), tIn.readInt(), tIn.readInt(), tIn.readInt(), tIn.readInt(), tIn.readInt(), tIn.readByte(), tIn.readByte(), tIn.readByte(), tIn.readByte()); -// } -// } -// } -// } - - return null; } } \ No newline at end of file diff --git a/src/main/java/gregtechmod/common/network/GT_PacketHandler.java b/src/main/java/gregtechmod/common/network/GT_PacketHandler.java index dfa0dce..d10966c 100644 --- a/src/main/java/gregtechmod/common/network/GT_PacketHandler.java +++ b/src/main/java/gregtechmod/common/network/GT_PacketHandler.java @@ -6,6 +6,7 @@ import cpw.mods.fml.relauncher.Side; import gregtechmod.common.network.packet.GT_Packet; import gregtechmod.common.network.packet.GT_SoundPacket; import gregtechmod.common.network.packet.GT_TileEntityPacket; +import net.minecraft.entity.player.EntityPlayerMP; public final class GT_PacketHandler implements Runnable { private static byte discriminator = 0; @@ -26,6 +27,16 @@ public final class GT_PacketHandler implements Runnable { GT_PacketHandler.registerClientMessage(SOUND_PACKET_CHANNEL, GT_SoundPacket.class); } + public static void sendPacket(T packet, EntityPlayerMP reciever) { + if (packet instanceof GT_TileEntityPacket) { + TILEENTITY_PACKET_CHANNEL.sendTo(packet, reciever); + } else if (packet instanceof GT_SoundPacket) { + SOUND_PACKET_CHANNEL.sendTo(packet, reciever); + } else { + GENERIC_CHANNEL.sendTo(packet, reciever); + } + } + protected static void registerServerMessage(SimpleNetworkWrapper channel, Class type) { GT_PacketHandler.register(channel, type, Side.SERVER); } diff --git a/src/main/java/gregtechmod/common/network/packet/GT_Packet.java b/src/main/java/gregtechmod/common/network/packet/GT_Packet.java index 20f291d..95e2be5 100644 --- a/src/main/java/gregtechmod/common/network/packet/GT_Packet.java +++ b/src/main/java/gregtechmod/common/network/packet/GT_Packet.java @@ -3,6 +3,12 @@ package gregtechmod.common.network.packet; import cpw.mods.fml.common.network.simpleimpl.IMessage; import io.netty.buffer.ByteBuf; +/** + * Abstract packet, contains raw data buffer. + * Fucking new packet system, in same time it is useful + * @author iDnK + * + */ public abstract class GT_Packet implements IMessage { public ByteBuf data; diff --git a/src/main/java/gregtechmod/common/network/packet/GT_TileEntityPacket.java b/src/main/java/gregtechmod/common/network/packet/GT_TileEntityPacket.java index c7cfe86..0c6a33a 100644 --- a/src/main/java/gregtechmod/common/network/packet/GT_TileEntityPacket.java +++ b/src/main/java/gregtechmod/common/network/packet/GT_TileEntityPacket.java @@ -1,18 +1,60 @@ package gregtechmod.common.network.packet; +import java.util.Arrays; +import java.util.stream.Collectors; + import io.netty.buffer.ByteBuf; +/** + * Data packet for MTE updates + * @author iDnK + * + */ public class GT_TileEntityPacket extends GT_Packet { + public int aX; + public int aY; + public int aZ; + public short aID; + public int[] aCovers; + public byte aTextureData; + public byte aUpdateData; + public byte aRedstoneData; + public byte aColorData; @Override public void fromBytes(ByteBuf buf) { + aX = buf.readInt(); + aY = buf.readInt(); + aZ = buf.readInt(); + aID = buf.readShort(); + for (int i = 0; i < 6; i++) + aCovers[i] = buf.readInt(); + aTextureData = buf.readByte(); + aUpdateData = buf.readByte(); + aRedstoneData = buf.readByte(); + aColorData = buf.readByte(); super.fromBytes(buf); - // FIXME Packets } @Override public void toBytes(ByteBuf buf) { + buf.writeInt(aX); + buf.writeInt(aY); + buf.writeInt(aZ); + buf.writeShort(aID); + for (int i = 0; i < 6; i++) + buf.writeInt(aCovers[i]); + buf.writeByte(aTextureData); + buf.writeByte(aUpdateData); + buf.writeByte(aRedstoneData); + buf.writeByte(aColorData); super.toBytes(buf); - + } + + @Override + public String toString() { + return String.format("GT_TileEntityPacket[x=%d, y=%d, z=%d, ID=%d, TextureData=%d, UpdateData=%d, RedstoneData=%d, ColorData=%d, CoverData=%s]", + aX, aY, aZ, (int)aID, (int)aTextureData, (int)aUpdateData, (int)aRedstoneData, (int)aColorData, + Arrays.stream(aCovers).mapToObj(i -> new Integer(i)).collect(Collectors.toList()).toString()); } }