Jump to content

[SOLVED]Changing NBTTagName on Keypress


Taji34

Recommended Posts

Okay, so the keypress is working now, but the packet isn't. Here's my packet handler:

 

TajiPacketHandler.java:

package taji34.troncraft;

import java.util.logging.Logger;

import taji34.troncraft.TajiPacket.ProtocolException;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;
import cpw.mods.fml.relauncher.Side;

public class TajiPacketHandler implements IPacketHandler {

    @Override
    public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
            try {
                    EntityPlayer entityPlayer = (EntityPlayer)player;
                    ByteArrayDataInput in = ByteStreams.newDataInput(packet.data);
                    int packetId = in.readUnsignedByte(); // Assuming your packetId is between 0 (inclusive) and 256 (exclusive). If you need more you need to change this
                    TajiPacket demoPacket = TajiPacket.constructPacket(packetId);
                    demoPacket.read(in);
                    demoPacket.execute(entityPlayer, entityPlayer.worldObj.isRemote ? Side.CLIENT : Side.SERVER);
            } catch (ProtocolException e) {
                    if (player instanceof EntityPlayerMP) {
                            ((EntityPlayerMP) player).playerNetServerHandler.kickPlayerFromServer("Protocol Exception!");
                            Logger.getLogger("DemoMod").warning("Player " + ((EntityPlayer)player).username + " caused a Protocol Exception!");
                    }
            } catch (ReflectiveOperationException e) {
                    throw new RuntimeException("Unexpected Reflection exception during Packet construction!", e);
            }
    }
}

 

BatonModePacket.java:

package taji34.troncraft;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;

import cpw.mods.fml.relauncher.Side;

public class BatonModePacket extends TajiPacket {

    private String text;
   
    public BatonModePacket(String text) {
            this.text = text;
    }

    public BatonModePacket() { } // Be sure to always have the default constructor in your class, or the reflection code will fail!

    @Override
protected void write(ByteArrayDataOutput out) {
            out.writeUTF(text);
    }

    @Override
    protected void read(ByteArrayDataInput in) throws ProtocolException {
            text = in.readUTF();
    }

    @Override
    protected void execute(EntityPlayer player, Side side) throws ProtocolException {
         //   if (side.isClient()) {
         //           player.addChatMessage(text);
          //  } else {
           //         throw new ProtocolException("Cannot send this packet to the server!");
          //  }
    	ItemStack itemstack = player.getHeldItem();
           if (side.isClient()) {
                   player.addChatMessage(itemstack.getDisplayName() + text);
           } else {
           //        throw new ProtocolException("Cannot send this packet to the server!");
        	   System.out.println(itemstack.getDisplayName() + text);
           }
    }
}

 

I'm sending the packet using the following line of code:

PacketDispatcher.sendPacketToServer(new BatonModePacket("Hello World!").makePacket());

 

Do I have something wrong somewhere?

Link to comment
Share on other sites

debug 101, are you RECEIVING the packet server side ?

question 2, is your packet handler even registered properly? (println inside the constructor if you need )

 

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

debug 101, are you RECEIVING the packet server side ?

question 2, is your packet handler even registered properly? (println inside the constructor if you need )

 

Would something print to the console if the packet was received server side? I put some println in both the constructor for BatonModePacket, and the makePacket() method that is inherited and called, and both ran and printed what I told them to the console. Debugging is obviously not my forte...

Link to comment
Share on other sites

I put some println in both the constructor for BatonModePacket, and the makePacket() method that is inherited and called, and both ran and printed what I told them to the console

cool

 

Would something print to the console if the packet was received server side?

yes, to the console

 

Debugging is obviously not my forte...

some people mod without knowing what an int is, you can learn and practice

 

inherited

the fact that you know what this word mean means that you know way more then 95% of the posts here

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

Would something print to the console if the packet was received server side?

yes, to the console

*FACEPALM* I figured out my problem. I changed the channel name in my packet handler, but not in my main class, so it works now Thank you!

Link to comment
Share on other sites

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
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.



×
×
  • Create New...

Important Information

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