Skip to content

Commit

Permalink
Fix GUI opening properly and get client/server sync.
Browse files Browse the repository at this point in the history
Currently doesn't save extended armor and backpack on disconnect due to MinecraftForge/FML#381
  • Loading branch information
portablejim committed Feb 26, 2014
1 parent 548c12d commit 66da232
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 118 deletions.
56 changes: 20 additions & 36 deletions src/main/java/tconstruct/client/TControls.java
@@ -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;
Expand All @@ -11,8 +9,10 @@
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
{
Expand Down Expand Up @@ -83,7 +83,7 @@ public void keyDown (Type types, KeyBinding kb, boolean tickEnd, boolean isRepea
}

midairJumps--;
resetFallDamage(mc.thePlayer.getDisplayName());
resetFallDamage();
}

if (!jumping)
Expand Down Expand Up @@ -120,26 +120,15 @@ public void resetControls ()
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
{
Expand All @@ -152,33 +141,28 @@ void updateSize (String name, float size)
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);
}

}
31 changes: 15 additions & 16 deletions src/main/java/tconstruct/common/TProxyCommon.java
@@ -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;
Expand All @@ -10,31 +11,29 @@
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
*/

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 int smelteryGuiID = 7;
public static int furnaceID = 8;
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 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

Expand Down
38 changes: 27 additions & 11 deletions src/main/java/tconstruct/util/network/packet/PacketArmorSync.java
Expand Up @@ -10,15 +10,36 @@
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
Expand All @@ -28,27 +49,22 @@ public void decodeInto (ChannelHandlerContext ctx, ByteBuf buffer)
{
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()));


Expand Down
Expand Up @@ -2,41 +2,35 @@

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
Expand All @@ -48,29 +42,17 @@ public void handleClientSide (EntityPlayer player)
@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;
}
}

}
15 changes: 15 additions & 0 deletions src/main/java/tconstruct/util/player/KnapsackInventory.java
@@ -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;
Expand Down Expand Up @@ -205,4 +208,16 @@ public void closeInventory ()
{
}

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);
}

}
27 changes: 6 additions & 21 deletions src/main/java/tconstruct/util/player/TPlayerHandler.java
@@ -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;
Expand All @@ -24,6 +26,8 @@
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;
Expand Down Expand Up @@ -156,30 +160,11 @@ public void onPlayerLogin (EntityPlayer entityplayer)

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)
Expand Down

0 comments on commit 66da232

Please sign in to comment.