Ok, this may potentially be the most nooby question you will ever see, and it might make you want to shoot yourself. Please dont. So, I want to give the player an item when I click a button, but this isn't working
Button:
public void actionPerformed(GuiButton guibutton){
if(guibutton.id == 24){
// Sending packet to server
IMessage msg = new SimplePacket.SimpleMessage(500, true);
PacketHandler.net.sendToServer(msg);
}
}
PacketHandler:
package com.bugzoo.FinancialMod;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
public class PacketHandler
{
public static SimpleNetworkWrapper net;
public static void initPackets()
{
net = NetworkRegistry.INSTANCE.newSimpleChannel("YourModId".toUpperCase());
registerMessage(SimplePacket.class, SimplePacket.SimpleMessage.class);
}
private static int nextPacketId = 0;
private static void registerMessage(Class packet, Class message)
{
net.registerMessage(packet, message, nextPacketId, Side.CLIENT);
net.registerMessage(packet, message, nextPacketId, Side.SERVER);
nextPacketId++;
}
}
SimplePacket:
package com.bugzoo.FinancialMod;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import com.bugzoo.FinancialMod.SimplePacket.SimpleMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
public class SimplePacket implements IMessageHandler<SimpleMessage, IMessage>
{
Minecraft mc;
@Override
public IMessage onMessage(SimpleMessage message, MessageContext ctx)
{
// just to make sure that the side is correct
if (ctx.side.isClient())
{
mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(FinancialMod.Wallet));
int integer = message.simpleInt;
boolean bool = message.simpleBool;
}
return message;
}
public static class SimpleMessage implements IMessage
{
private int simpleInt;
private boolean simpleBool;
// this constructor is required otherwise you'll get errors (used somewhere in fml through reflection)
public SimpleMessage() {}
public SimpleMessage(int simpleInt, boolean simpleBool)
{
this.simpleInt = simpleInt;
this.simpleBool = simpleBool;
}
@Override
public void fromBytes(ByteBuf buf)
{
// the order is important
this.simpleInt = buf.readInt();
this.simpleBool = buf.readBoolean();
}
@Override
public void toBytes(ByteBuf buf)
{
buf.writeInt(simpleInt);
buf.writeBoolean(simpleBool);
}
}
}