Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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]

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.

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

  • Author

I feel SO stupid now, it just clicked for me on how this whole setup kinda functions.. thanks for the help both of you!!

I think i'll leave this thread open just incase someone else has a question

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

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

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.