Jump to content

Recommended Posts

Posted

I'm not really sure how to use packets. I've followed this tutorial:

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

 

I've set up a handler and such, yet I still don't know how to USE a packet. Can someone provide an explanation or example(even better) of how to use them??

 

(Note: It also tells me to register my channel handler with:

	EnumMap<Side, FMLEmbeddedChannel> channels = NetworkRegistry.INSTANCE.newChannel("MetroidChannel", new MetroidChannelHandler());

But I have no idea where i'm supposed to register it.)

 

Sorry if i'm being stupid or missing something obvious here, but I've looked around a bit and haven't been able to really find anything on this

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Posted

First, take a look at this tutorial: http://www.minecraftforge.net/wiki/Netty_Packet_Handling

Then, just copy AbstractPacket and PacketPipeline to your workspace. Register the Pipeline in your mod class.

Next thing you gonna do is to create a class named, say, PacketMyThingy, and write the stuff you want to transfer. Here's and example of my class:

package net.row.network;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.entity.player.EntityPlayer;
import net.row.stock.core.RoWSteamLocomotive;

public class PacketLocomotive extends AbstractPacket{

byte reverse;
boolean zeroReverse;
boolean brake;
byte hornStage;

public PacketLocomotive(){
	this.reverse = 0;
	this.zeroReverse = false;
	this.brake = false;
	this.hornStage = 0;
}

public PacketLocomotive(byte r, boolean z, boolean b, byte h){
	reverse = r;
	zeroReverse = z;
	brake = b;
	hornStage = h;
}

@Override
public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer){
	buffer.writeByte(reverse);
	buffer.writeBoolean(zeroReverse);
	buffer.writeBoolean(brake);
	buffer.writeByte(hornStage);
}

@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer){
	reverse = buffer.readByte();
	zeroReverse = buffer.readBoolean();
	brake = buffer.readBoolean();
	hornStage = buffer.readByte();
}

@Override
public void handleClientSide(EntityPlayer player){
}

@Override
public void handleServerSide(EntityPlayer player){
	if(!player.worldObj.isRemote && player.ridingEntity != null && player.ridingEntity instanceof RoWSteamLocomotive){
		RoWSteamLocomotive loco = (RoWSteamLocomotive)player.ridingEntity;
		loco.reverse = reverse;
		loco.zeroReverse = zeroReverse;
		loco.brake = brake;
		loco.hornStage = hornStage;
	}
}

}

Then, go to your PacketPipeline.initialise() and a) rename the channel, b) register packet in such way registerPacket(PacketLocomotive.class);

Finally, go to a place where you want to use a packet, and add line like this RoW.packetPipeline.sendToServer(new PacketLocomotive(loco.reverse, loco.zeroReverse, loco.brake, loco.hornStage));

As an instance, my key handler class:

package net.row.handlers;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.row.RoW;
import net.row.network.PacketLocomotive;
import net.row.registry.RoWKeys;
import net.row.stock.core.RoWSteamLocomotive;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.InputEvent;

public class RoWKeyHandler{

@SubscribeEvent
public void onKeyInput(InputEvent.KeyInputEvent event){
	if(Minecraft.getMinecraft().theWorld != null && Minecraft.getMinecraft().thePlayer != null){
		EntityPlayer player = Minecraft.getMinecraft().thePlayer;
		if(player.ridingEntity instanceof RoWSteamLocomotive){
			RoWSteamLocomotive loco = (RoWSteamLocomotive)player.ridingEntity;

			if(RoWKeys.ReverseUp.getIsKeyPressed() && !loco.zeroReverse){
				if(loco.reverse < 127 && !loco.zeroReverse){
					loco.reverse += 1;
				}
			}else if(RoWKeys.ReverseDown.getIsKeyPressed() && !loco.zeroReverse){
				if(loco.reverse > -127){
					loco.reverse -= 1;
				}
			}
			if(RoWKeys.ReverseZero.isPressed()){
				loco.zeroReverse = true;
			}
			if(RoWKeys.Brake.isPressed() && (loco.brakeStage == 0 || loco.brakeStage == 30)){
				loco.brake = !loco.brake;
			}
			if(RoWKeys.Horn.isPressed() && loco.hornStage == 0){
				loco.hornStage = 50;
			}
			RoW.packetPipeline.sendToServer(new PacketLocomotive(loco.reverse, loco.zeroReverse, loco.brake, loco.hornStage));
		}
	}
}
}

And the last thing - press „Thank you“ button if this was helpful.

 

If i helped you, don't forget pressing "Thank You" button. Thanks for your time.

Posted

Ok, that helps a bit, but how do I make a packet do something once it's sent, like say, spawn an entity?

Same way you spawn an entity anywhere else. AbstractPacket's handleServerSide method has an EntityPlayer parameter, so you can use that to get the world object, create your entity as normal, then player.worldObj.spawnEntityInWorld(yourEntity).

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.