Jump to content

Recommended Posts

Posted

Hello, I have added a packet handler to my mod and it doesn't work. I have never used or known much about them until today. I followed a tutorial and I get no errors, but I never receive the info in the packet. Since I don't really know much about Packet Handling, I don't really know whats wrong or what could be wrong with this. Sorry my code is messy. I have been trying a lot of stuff and am basically trying to get the code functional before I try to make it more organized. I would appreciate any help anyone can give.

 

Tutorial:

http://www.minecraftforge.net/wiki/Packet_Handling

 

Packet Handler:

package mod.xtronius.rc_mod.handlers;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;

public class PacketHandler implements IPacketHandler {

        @Override
        public void onPacketData(INetworkManager manager,
                        Packet250CustomPayload packet, Player player) {
                System.out.println("hello");
                if (packet.channel.equals("RC_ModWoodCuttingLvl")) {
                	
                	handleWoodCutting(packet);
                }
        }
        
        private void handleWoodCutting(Packet250CustomPayload packet) {
                DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
                
                float WoodCuttingLvl;
                
                try {
                	WoodCuttingLvl = inputStream.readFloat();
                } catch (IOException e) {
                        e.printStackTrace();
                        return;
                }
                
                System.out.println("[Rune-Craft]" + " Client- " + WoodCuttingLvl);
        }

}

 

Method Were Packet is Sent:

public float woodBreakSpeed(World world, EntityPlayer player, PlayerXPValue xpValue, Block block, int x, int y, int z) {

	Item tool = eventBlockReplace.getTool(player);
	int toolLvl = eventBlockReplace.getToolLevel(player);
	int meta = block.getDamageValue(world, x, y, z);
	float toolSpeed = eventBlockReplace.getToolSpeed(player);

	float Wood = 4.5F;
	float Yew = 0.75F;
	float Oak = 2.5F;
	float Teak = 1.5F;

	if(meta == 0) {
		if(toolLvl == 1 || toolLvl == 2 || toolLvl == 3 || toolLvl == 4 || toolLvl == 5) {
			if(!world.isRemote){
                float WoodCuttingLvl = ((Wood*toolSpeed) * (lvlManager.getLvl(player, "WoodCuttingLvl")/10));
                
                ByteArrayOutputStream bos = new ByteArrayOutputStream(4);
                DataOutputStream outputStream = new DataOutputStream(bos);
                try {
                        outputStream.writeFloat(WoodCuttingLvl);
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
                
                Packet250CustomPayload packet = new Packet250CustomPayload();
                packet.channel = "RC_ModWoodCuttingLvl";
                packet.data = bos.toByteArray();
                packet.length = bos.size();
                
                
                if (!world.isRemote) {
                	System.out.println("Running - Server");
                            We are on the server side.
                           EntityPlayerMP playerObj = (EntityPlayerMP) player;
                        PacketDispatcher.sendPacketToPlayer(packet, (Player)player);
                } else if (world.isRemote) {
                        // We are on the client side.
                        //EntityClientPlayerMP playerObj = (EntityClientPlayerMP) player;
                       // playerObj.sendQueue.addToSendQueue(packet);
                } else {
                        // We are on the Bukkit server.
                }
                	
			//System.out.println(((Wood*toolSpeed) * (lvlManager.getLvl(player, "WoodCuttingLvl")/10)));
			}
			return ((Wood*toolSpeed));
		}
	}
	else if(meta == 1) {
		if(toolLvl == 5) {
			//eventBlockReplace.setWoodSpeed((Yew*toolSpeed));
			return (Yew*toolSpeed);
		}
	}
	else if(meta == 2) {
		if(toolLvl == 1 || toolLvl == 2 || toolLvl == 3 || toolLvl == 4 || toolLvl == 5) {
			//eventBlockReplace.setWoodSpeed((Oak*toolSpeed));
			return (Oak*toolSpeed);
		}
	}
	else if(meta == 3) {
		if(toolLvl == 4 || toolLvl == 5) {
			//eventBlockReplace.setWoodSpeed((Teak*toolSpeed));
			return (Teak*toolSpeed);
		}
	} else 
		System.out.println("[Rune-Craft] Error - WoodBreakSpeed Method");
		return 0;
}

Don't be afraid to ask question when modding, there are no stupid question! Unless you don't know java then all your questions are stupid!

Posted

I think your packet channel is too long.  I think its limited to 16 characters.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

I changed the channel name and I still get no response, from what I know is calling the the send packet method, bit nothing comes out when I do the check in the Packet Handler

Don't be afraid to ask question when modding, there are no stupid question! Unless you don't know java then all your questions are stupid!

Posted

Did you set up your @NetworkMod annotation?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Thak You I did, but I misspelled the channel name. Sorry for the late response, my Internet went down for some reason.

Don't be afraid to ask question when modding, there are no stupid question! Unless you don't know java then all your questions are stupid!

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



×
×
  • Create New...

Important Information

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