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

    • Keep on using the original Launcher Run Vanilla 1.12.2 once and close the game Download Optifine and run optifine as installer (click on the optifine jar) Start the launcher and make sure the Optifine profile is selected - then test it again  
    • Hi everyone, I’m hoping to revisit an old version of Minecraft — specifically around Beta 1.7.3 — for nostalgia’s sake. I’ve heard you can do this through the official Minecraft Launcher, but I’m unsure how to do it safely without affecting my current installation or save files. Are there any compatibility issues I should watch out for when switching between versions? Would really appreciate any tips or advice from anyone who’s done this before! – Adam
    • hello! i was trying to recreate item-in-hand feature for my custom mob. i figured out that my mob needs a custom iteminhandlayer. i created it - but the main problem is.. well.. you can see all on screenshots any idea how i can fix that? is there any implemented method to render the item perfect to hand? public void render(@NotNull PoseStack pPoseStack, @NotNull MultiBufferSource pBufferSource, int pPackedLight, @NotNull TuneGolemRenderState pRenderState, float pYRot, float pXRot) { ItemStackRenderState item = pRenderState.heldItem; if (!item.isEmpty()) { pPoseStack.pushPose(); ModelPart leftArm = this.getParentModel().leftArm; pPoseStack.translate(0.35,0.5,-1.25); pPoseStack.mulPose(Axis.XP.rotationDegrees(180.0F)); pPoseStack.mulPose(Axis.YP.rotationDegrees(90.0F)); leftArm.translateAndRotate(pPoseStack); // pPoseStack.translate(0,0,0); leftArm.translateAndRotate(pPoseStack); if (TuneGolemRenderState.hornPlaying) { pPoseStack.translate(0, -0.5, 0.65); pPoseStack.scale(1.25F,1.25F,1.25F); } // Minecraft.getInstance().player.displayClientMessage(Component.literal(leftArm.xRot + " " + leftArm.yRot + " " + leftArm.zRot), true); item.render(pPoseStack, pBufferSource, pPackedLight, OverlayTexture.NO_OVERLAY); pPoseStack.popPose(); // -1.0F, -2.0F, -3.0F } }  
    • I checked for any driver updates, but no new updates were found
    • Maybe it refers to an issue with the system - check for CPU/GPU driver updates
  • Topics

×
×
  • Create New...

Important Information

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