Fix GUI opening properly and get client/server sync.

Currently doesn't save extended armor and backpack on disconnect due to MinecraftForge/FML#381
This commit is contained in:
James McKay committed 2014-02-26 22:40:22 +11:00
1 parent 548c12dc22
commit 66da232f34
6 files changed
+98 -117

No files matched your search

+20 -36
View File
@@ -1,8 +1,6 @@
package tconstruct.client;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import cpw.mods.fml.common.gameevent.TickEvent.Type;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.gui.inventory.GuiInventory;
@@ -11,8 +9,10 @@ import net.minecraft.potion.Potion;
import tconstruct.TConstruct;
import tconstruct.client.event.EventCloakRender;
import tconstruct.client.tabs.TabRegistry;
import tconstruct.common.TProxyCommon;
import tconstruct.util.network.packet.AbstractPacket;
import tconstruct.util.network.packet.PacketDoubleJump;
import cpw.mods.fml.common.gameevent.TickEvent.Type;
import tconstruct.util.network.packet.PacketExtendedInventory;
public class TControls extends TKeyHandler
{
@@ -83,7 +83,7 @@ public class TControls extends TKeyHandler
}
midairJumps--;
resetFallDamage(mc.thePlayer.getDisplayName());
resetFallDamage();
}
if (!jumping)
@@ -120,26 +120,15 @@ public class TControls extends TKeyHandler
onStilts = false;
}
void resetFallDamage (String name)
void resetFallDamage()
{
ByteArrayOutputStream bos = new ByteArrayOutputStream(8);
DataOutputStream outputStream = new DataOutputStream(bos);
try
{
outputStream.writeByte(10);
outputStream.writeUTF(name);
}
catch (Exception ex)
{
ex.printStackTrace();
}
updateServer(bos);
AbstractPacket packet = new PacketDoubleJump();
updateServer(packet);
}
void updateSize (String name, float size)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream(8);
/*ByteArrayOutputStream bos = new ByteArrayOutputStream(8);
DataOutputStream outputStream = new DataOutputStream(bos);
try
{
@@ -152,33 +141,28 @@ public class TControls extends TKeyHandler
ex.printStackTrace();
}
updateServer(bos);
updateServer(bos);*/
//TODO: Enable code with right packet
//AbstractPacket packet = new ();
//updateServer(packet);
}
public static void openArmorGui ()
{
// Position is unused. Set to 0.
mc.thePlayer.openGui(TConstruct.instance, TProxyClient.armorGuiID, mc.theWorld, 0, 0, 0);
AbstractPacket packet = new PacketExtendedInventory(TProxyCommon.armorGuiID);
updateServer(packet);
}
public static void openKnapsackGui ()
{
// Position is unused. Set to 0.
mc.thePlayer.openGui(TConstruct.instance, TProxyClient.knapsackGuiID, mc.theWorld, 0, 0, 0);
AbstractPacket packet = new PacketExtendedInventory(TProxyCommon.knapsackGuiID);
updateServer(packet);
}
static void updateServer (ByteArrayOutputStream bos)
static void updateServer (AbstractPacket abstractPacket)
{
/*
* Packet250CustomPayload packet = new Packet250CustomPayload();
* packet.channel = "TConstruct"; packet.data = bos.toByteArray();
* packet.length = bos.size();
*
* PacketDispatcher.sendPacketToServer(packet);
*/
// TODO Find out what packet should be used here
TConstruct.packetPipeline.sendToServer(new PacketDoubleJump());
TConstruct.packetPipeline.sendToServer(abstractPacket);
}
}
@@ -1,5 +1,6 @@
package tconstruct.common;
import cpw.mods.fml.common.network.IGuiHandler;
import mantle.blocks.abstracts.InventoryLogic;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
@@ -10,8 +11,6 @@ import tconstruct.inventory.ArmorExtendedContainer;
import tconstruct.inventory.ContainerLandmine;
import tconstruct.inventory.KnapsackContainer;
import tconstruct.util.player.TPlayerStats;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.registry.LanguageRegistry;
/**
* Common proxy class for InfiTools
@@ -19,22 +18,22 @@ import cpw.mods.fml.common.registry.LanguageRegistry;
public class TProxyCommon implements IGuiHandler
{
public static int toolStationID = 0;
public static int partBuilderID = 1;
public static int patternChestID = 2;
public static int stencilTableID = 3;
public static int frypanGuiID = 4;
public static int toolForgeID = 5;
public static final int toolStationID = 0;
public static final int partBuilderID = 1;
public static final int patternChestID = 2;
public static final int stencilTableID = 3;
public static final int frypanGuiID = 4;
public static final int toolForgeID = 5;
public static int smelteryGuiID = 7;
public static int furnaceID = 8;
public static final int smelteryGuiID = 7;
public static final int furnaceID = 8;
// public static int drawbridgeID = 9; // Moved to TMechworks
public static int landmineID = 10;
public static int craftingStationID = 11;
public static final int landmineID = 10;
public static final int craftingStationID = 11;
// public static int advDrawbridgeID = 12; // Moved to TMechworks
public static int inventoryGui = 100;
public static int armorGuiID = 101;
public static int knapsackGuiID = 102;
public static final int inventoryGui = 100;
public static final int armorGuiID = 101;
public static final int knapsackGuiID = 102;
// public static int manualGuiID = -1;// moved to Mantle
@@ -10,15 +10,36 @@ import tconstruct.common.TProxyCommon;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.entity.player.EntityPlayer;
import tconstruct.util.player.ArmorExtended;
import tconstruct.util.player.KnapsackInventory;
public class PacketArmorSync extends AbstractPacket
{
ByteBuf b;
ArmorExtended armor;
KnapsackInventory knapsack;
public PacketArmorSync()
{
armor = new ArmorExtended();
knapsack = new KnapsackInventory();
}
public PacketArmorSync(ArmorExtended armor, KnapsackInventory knapsack)
{
this.armor = armor;
this.knapsack = knapsack;
}
@Override
public void encodeInto (ChannelHandlerContext ctx, ByteBuf buffer)
{
try {
armor.writeInventoryToStream(buffer);
knapsack.writeInventoryToStream(buffer);
} catch (IOException e) {
TConstruct.logger.warn("Failed at writing Server packet for TConstruct.");
e.printStackTrace();
}
}
@Override
@@ -28,27 +49,22 @@ public class PacketArmorSync extends AbstractPacket
{
try
{
byte packetID = buffer.readByte();
armor.readInventoryFromStream(buffer);
knapsack.readInventoryFromStream(buffer);
}
catch (Exception e)
{
TConstruct.logger.warn("Failed at reading Client packet for TConstruct.");
e.printStackTrace();
}
try
{
TProxyClient.armorExtended.readInventoryFromStream(buffer);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Override
public void handleClientSide (EntityPlayer player)
{
armor.saveToNBT(player);
knapsack.saveToNBT(player);
TProxyClient.armorExtended.recalculateHealth(player, TConstruct.playerTracker.getPlayerStats(player.getDisplayName()));
@@ -2,41 +2,35 @@ package tconstruct.util.network.packet;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import tconstruct.TConstruct;
import tconstruct.common.TProxyCommon;
public class PacketExtendedInventory extends AbstractPacket
{
byte[] type;
int type;
public PacketExtendedInventory()
{
}
public PacketExtendedInventory(byte[] bs)
public PacketExtendedInventory(int type)
{
this.type = bs;
TConstruct.logger.error(type);
this.type = type;
}
@Override
public void encodeInto (ChannelHandlerContext ctx, ByteBuf buffer)
{
// buffer.writeBytes(type);
buffer.writeInt(type);
}
@Override
public void decodeInto (ChannelHandlerContext ctx, ByteBuf buffer)
{
// type = buffer.(buffer);
type = buffer.readInt();
}
@Override
@@ -48,29 +42,17 @@ public class PacketExtendedInventory extends AbstractPacket
@Override
public void handleServerSide (EntityPlayer player)
{
// String user = inputStream.readUTF();
// EntityPlayer player = TConstruct.playerTracker.getEntityPlayer(user);
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(type));
try
switch (type)
{
switch (inputStream.readByte())
{
case 0:
player.openGui(TConstruct.instance, TConstruct.proxy.inventoryGui, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
break;
case 1:
player.openGui(TConstruct.instance, TConstruct.proxy.armorGuiID, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
break;
case 2:
player.openGui(TConstruct.instance, TConstruct.proxy.knapsackGuiID, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
break;
}
}
catch (IOException e)
{
TConstruct.logger.error("Failed at reading server packet for TConstruct.");
e.printStackTrace();
case TProxyCommon.inventoryGui:
player.openGui(TConstruct.instance, TConstruct.proxy.inventoryGui, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
break;
case TProxyCommon.armorGuiID:
player.openGui(TConstruct.instance, TConstruct.proxy.armorGuiID, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
break;
case TProxyCommon.knapsackGuiID:
player.openGui(TConstruct.instance, TConstruct.proxy.knapsackGuiID, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
break;
}
}
}
@@ -1,7 +1,10 @@
package tconstruct.util.player;
import java.io.IOException;
import java.lang.ref.WeakReference;
import cpw.mods.fml.common.network.ByteBufUtils;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
@@ -205,4 +208,16 @@ public class KnapsackInventory implements IInventory
{
}
public void writeInventoryToStream (ByteBuf os) throws IOException
{
for (int i = 0; i < 27; i++)
ByteBufUtils.writeItemStack(os, inventory[i]);
}
public void readInventoryFromStream (ByteBuf is) throws IOException
{
for (int i = 0; i < 27; i++)
inventory[i] = ByteBufUtils.readItemStack(is);
}
}
@@ -1,6 +1,8 @@
package tconstruct.util.player;
import static io.netty.buffer.Unpooled.*;
import cpw.mods.fml.common.network.FMLNetworkEvent.ClientDisconnectionFromServerEvent;
import io.netty.buffer.ByteBuf;
import java.io.ByteArrayOutputStream;
@@ -24,6 +26,8 @@ import tconstruct.TConstruct;
import tconstruct.common.TRepo;
import tconstruct.library.tools.AbilityHelper;
import tconstruct.util.config.PHConstruct;
import tconstruct.util.network.packet.AbstractPacket;
import tconstruct.util.network.packet.PacketArmorSync;
import tconstruct.util.network.packet.PacketDoubleJump;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
@@ -156,30 +160,11 @@ public class TPlayerHandler
void updatePlayerInventory (EntityPlayer player, TPlayerStats stats)
{
ByteBuf outputStream = buffer(8);
try
{
outputStream.writeByte(4);
stats.armor.writeInventoryToStream(outputStream);
}
catch (Exception ex)
{
ex.printStackTrace();
}
if (player instanceof EntityPlayerMP)
{
TConstruct.packetPipeline.sendTo(new PacketDoubleJump(), (EntityPlayerMP) player);
AbstractPacket packet = new PacketArmorSync(stats.armor, stats.knapsack);
TConstruct.packetPipeline.sendTo(packet, (EntityPlayerMP) player);
}
/*
* Packet250CustomPayload packet = new Packet250CustomPayload();
* packet.channel = "TConstruct"; packet.data = bos.toByteArray();
* packet.length = bos.size();
*
* PacketDispatcher.sendPacketToPlayer(packet, (Player) player);
*/
// TODO find out what packet needs to be used here (and make sure that
// player actually is a playerMP and this gets called)
}
void savePlayerStats (EntityPlayer player, boolean clean)