Skip to content

Instantly share code, notes, and snippets.

@mDiyo
Created February 19, 2013 23:18
Show Gist options
  • Select an option

  • Save mDiyo/4991168 to your computer and use it in GitHub Desktop.

Select an option

Save mDiyo/4991168 to your computer and use it in GitHub Desktop.
Gui Button to Packet
//Button
protected void actionPerformed (GuiButton button)
{
if (button.id == 0)
{
patternIndex++;
if (patternIndex > TContent.patternOutputs.length - 1)
patternIndex = 0;
}
else if (button.id == 1)
{
patternIndex--;
if (patternIndex < 0)
patternIndex = TContent.patternOutputs.length - 1;
}
ItemStack pattern = logic.getStackInSlot(0);
if (pattern != null && pattern.getItem() == TContent.blankPattern)
{
ItemStack stack = new ItemStack(TContent.woodPattern, 1, patternIndex + 1);
logic.setInventorySlotContents(1, stack);
updateServer(stack);
}
}
void updateServer (ItemStack stack)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream(8);
DataOutputStream outputStream = new DataOutputStream(bos);
try
{
outputStream.writeByte(2);
outputStream.writeInt(logic.worldObj.provider.dimensionId);
outputStream.writeInt(logic.xCoord);
outputStream.writeInt(logic.yCoord);
outputStream.writeInt(logic.zCoord);
outputStream.writeShort(stack.itemID);
outputStream.writeShort(stack.getItemDamage());
}
catch (Exception ex)
{
ex.printStackTrace();
}
Packet250CustomPayload packet = new Packet250CustomPayload();
packet.channel = "TConstruct";
packet.data = bos.toByteArray();
packet.length = bos.size();
PacketDispatcher.sendPacketToServer(packet);
}
//Packet Handler
public class TPacketHandler implements IPacketHandler
{
@Override
public void onPacketData (INetworkManager manager, Packet250CustomPayload packet, Player player)
{
Side side = FMLCommonHandler.instance().getEffectiveSide();
if (packet.channel.equals("TConstruct"))
{
//System.out.println("Recieved a packet for TConstruct");
if (side == Side.SERVER)
handleServerPacket(packet);
else
handleClientPacket(packet);
}
}
void handleClientPacket (Packet250CustomPayload packet)
{
//System.out.println("Handling client packet");
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
byte packetType;
int dimension;
byte packetID;
try
{
packetID = inputStream.readByte();
dimension = inputStream.readInt();
World world = DimensionManager.getWorld(dimension);
}
catch (IOException e)
{
System.out.println("Failed at reading client packet for TConstruct.");
e.printStackTrace();
return;
}
}
void handleServerPacket (Packet250CustomPayload packet)
{
//System.out.println("Handling server packet");
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
byte packetType;
int dimension;
byte packetID;
try
{
packetID = inputStream.readByte();
dimension = inputStream.readInt();
World world = DimensionManager.getWorld(dimension);
if (packetID == 1) //Tool Station
{
int x = inputStream.readInt();
int y = inputStream.readInt();
int z = inputStream.readInt();
TileEntity te = world.getBlockTileEntity(x, y, z);
String toolName = inputStream.readUTF();
if (te instanceof ToolStationLogic)
{
((ToolStationLogic) te).setToolname(toolName);
}
}
else if (packetID == 2) //Stencil Table
{
int x = inputStream.readInt();
int y = inputStream.readInt();
int z = inputStream.readInt();
TileEntity te = world.getBlockTileEntity(x, y, z);
Short itemID = inputStream.readShort();
Short itemDamage = inputStream.readShort();
if (te instanceof InventoryLogic)
{
((InventoryLogic) te).setInventorySlotContents(1, new ItemStack(itemID, 1, itemDamage));
}
}
}
catch (IOException e)
{
System.out.println("Failed at reading server packet for TConstruct.");
e.printStackTrace();
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment