Jump to content

Recommended Posts

Posted

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

 

 

Posted
  On 11/16/2014 at 3:55 PM, diesieben07 said:

a) Why do you register your packet for being sent both ways? It should only go client -> server.

b) Why do you send and integer and a boolean when you never use them?

c) You need to use the serverside player when receiving the packet, you get it from the MessageContext.

d) Your packet has the potential of allowing players to infinitely create your Item over and over again. The server needs to check if it is ok for the client to send that packet, otherwise hacked clients could exploit it.

 

How do I get it from the MessageContext?

Posted
  On 11/16/2014 at 4:06 PM, diesieben07 said:

MessageContext#getServerHandler().playerEntity

 

Ok, so now when I press it, it says "a fatal error has occured, this connection is terminated"

Posted
  On 11/16/2014 at 4:15 PM, diesieben07 said:

Post the full log.

 

 

  Reveal hidden contents

 

Posted
  On 11/16/2014 at 4:47 PM, diesieben07 said:

Copy and paste the full console output from your IDE.

 

Oh, right

 

  Reveal hidden contents

 

Posted
  On 11/16/2014 at 5:02 PM, diesieben07 said:

Don't return the message in onMessage! What you return there is send as a response to whoever sent the incoming packet. Normally you want to return null there. And again: Do not register your packets for both sides! (If you had fixed that the error would be different).

 

Where am I registering it on both sides?

Posted
  On 11/16/2014 at 5:40 PM, diesieben07 said:

registerMessage

in your PacketHandler class.

 

I fixed that, now nothing is happening at all when I press the button

Posted
  On 11/16/2014 at 5:48 PM, diesieben07 said:

Show your updated PacketHandler class and the Message & MessageHandler.

 

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

 

SimpleMessage:

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())
{
	ctx.getServerHandler().playerEntity.inventory.addItemStackToInventory(new ItemStack(FinancialMod.Wallet));
}
return null;
}
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);
}
}
}

 

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

Posted
  On 11/16/2014 at 5:57 PM, diesieben07 said:

a) Now you register your packet to be received on the Client, but you sent it to the server.

b) In your MessageHandler you check for client side, but your packet is only ever received on the server.

c) You are still sending unused data in your packet.

 

How do I send it to the client? I've tried this code, but the player variable doesn't exist

public void actionPerformed(GuiButton guibutton){
	if(guibutton.id == 24){
		// Sending packet to client
		if (player instanceof EntityPlayerMP)
		{
		IMessage msg = new SimplePacket.SimpleMessage(800, false);
		PacketHandler.net.sendTo(msg, (EntityPlayerMP)player);
		}
	}
}

Posted
  On 11/16/2014 at 6:08 PM, diesieben07 said:

You should send it to the server! You need to register it as being received on the server, too.

 

You said about sending unused data, where am I doing that?

Posted
  On 11/17/2014 at 4:02 AM, diesieben07 said:

It doesn't matter. Static inner classes behave just like a normal class.

 

Ok, Now

 

I stopped sending unused data

I'm now sending to the server

Now checks for server and recieves on server

 

But still, nothing happens when I click the button

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi! I've been running a server on Minecraft using the Prodigium Reforged pack on curse forge using the site exaroton. It was working fine until randomly encountering this error every time anyone tries to connect to the server, I've already reloaded backups and deleted json files  Here is the crash report https://mclo.gs/EwKw51q. Thanks for the help in advance
    • nevermind i figured out the issue  
    • I was trying to host a server with the 1.21.1 cobbleverse forge mod, but when I tried starting it, it kept giving me this error. Crash report here: https://pastebin.com/AsE1X5QY   Thanks
    • This mod lets you choose an element power, Earth, Wind, Fire, or Water. Also two secret element powers Light and Darkness. Each power gives good a bad things, Earth lets you dig through anything but ores and bedrock, Wind lets you jump high in the air every three seconds, Fir lets you throw fire balls and you are immune to any flame/lava but you can't go into water or else you take damage, Water you swim and break blocks under water quicker and you can get rid of water like a sponge but you take more damage when in fire/lava, Light makes you faster at day but slower at night you are able to throw light spears that do four hearts and can go through any armor, Darkness let's you be faster at night but slower at day mobs don't harm you but peaceful mobs run away from you you are able to spawn ink creatures around you that are like dogs but are stronger. another thing in the mod is that there are other dimensions that can get you ores to make better armor and weapons, each dimensions are are good for a certain element though each dimension has a boss depending on which dimension one dimension has more than one boss, each elements are required to defeat the bosses with each others help.
    • Read the posts above yours, it tells you exactly how to do it, instructions are the same if it's making a forge installation or a vanilla one, just make a new folder for the game directory.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.