Jump to content

How do i create custom packets?


Seynox

Recommended Posts

Hello!

I'm trying to make a packet when the player press his jump keybind.

I have a custom item that allow you to double jump when equipped in a Bauble Slot (Belt slot).

 

Everything seems to work, i only need to know when the player press space when he's not touching the ground.

I already saw the Forge Doc about SimpleImpl, but i don't really get it.. Do i need 3 class? PacketHandler, MyMessage and MyMessageHandler? (I guess i can rename the Messages classes)

 

Here's the item im making :

Spoiler

boolean usedDoubleJump = false;
	public void onWornTick(ItemStack itemstack, EntityLivingBase player) {
		World world = player.world;
		
		if(!player.onGround) {
			if(/*player press space &&*/!usedDoubleJump) {
				usedDoubleJump = true;
				player.motionY = 0.42D;
				player.fallDistance = 0;
			}
			
			
		} else if(usedDoubleJump) {usedDoubleJump = false;}
	}

 

Can you guys give me examples?

Link to comment
Share on other sites

What version

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Okay so here's what i did. Every println are showing up in the console, which mean everything is working, but the player.motionY = 0.42D; (Inside PacketJump) is not happening, and i don't have any errors. I think it might be a side problem, but i don't see how i can fix it

 

ClientProxy :

Spoiler

@Override
    public void preInit(FMLPreInitializationEvent event) {

        MinecraftForge.EVENT_BUS.register(new KeyPressHandler());

}

 

 

KeyPressHandler :

Spoiler

@SideOnly(Side.CLIENT)
public class KeyPressHandler {
    
    @SubscribeEvent
    public void onKeyPress(InputEvent.KeyInputEvent event) {
        
        EntityPlayerSP player = Minecraft.getMinecraft().player;
        World world = player.world;

        if (Minecraft.getMinecraft().gameSettings.keyBindJump.isPressed() && !player.onGround) {
            IMessage jumpmsg = new PacketJump.JumpMessage(player.getEntityId());
            PacketHandler.INSTANCE.sendToServer(jumpmsg);
            System.out.println("Jump Packet sent. Player : " + player);
            return;
        }
    }

}

 

 

PacketHandler :

Spoiler

public class PacketHandler {
	
	public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MOD_ID.toLowerCase());
	
	public static void init() {
		registerMessage(PacketJump.class, PacketJump.JumpMessage.class);
	}
	
	private static int nextPacketId = 0;
	  
	  private static void registerMessage(Class packet, Class message)
	  {
	    INSTANCE.registerMessage(packet, message, nextPacketId, Side.CLIENT);
	    INSTANCE.registerMessage(packet, message, nextPacketId, Side.SERVER);
	    nextPacketId++;
	}

}

 

 

PacketJump :

Spoiler

public class PacketJump implements IMessageHandler<PacketJump.JumpMessage, IMessage>
{
	  @Override
		public IMessage onMessage(JumpMessage message, MessageContext ctx)
		{
		  EntityPlayer playerID = (ctx.side.isClient() ? Minecraft.getMinecraft().player : ctx.getServerHandler().player);
		  EntityPlayer player = (EntityPlayer)playerID.world.getEntityByID(message.playerid);
		  
		  boolean usedDoubleJump = false;
		  
		  ItemStack belt = BaublesApi.getBaublesHandler(player).getStackInSlot(3);
		  if(!belt.isEmpty() && belt.getItem() instanceof CloudInJar && !usedDoubleJump) {
			    usedDoubleJump = true;
				player.motionY = 0.42D;
				player.fallDistance = 0;
				System.out.println("Added motionY to " + player);
		  }
		  
		return null;
		}
	  
	  public static class JumpMessage implements IMessage
	  {
	    int playerid;
	    
	    public JumpMessage() {}
	    
	    public JumpMessage(int playerid)
	    {
	      this.playerid = playerid;
	    }
	    
	    @Override
	    public void fromBytes(ByteBuf buf)
	    {
	      this.playerid = ByteBufUtils.readVarInt(buf, 4);
	    }
	    
	    @Override
	    public void toBytes(ByteBuf buf)
	    {
	    	ByteBufUtils.writeVarInt(buf, playerid, 4);
	    }
	}
	  
	  
}

 

 

The console output when i press space while in the air :

Spoiler

[15:23:06] [main/INFO] [STDOUT]: [com.seynox.seynoxstuffmod.util.handlers.KeyPressHandler:onKeyPress:28]: Jump Packet sent. Player : EntityPlayerSP['Player644'/79, l='MpServer', x=601.12, y=83.25, z=648.43]
[15:23:06] [Netty Server IO #1/INFO] [STDOUT]: [com.seynox.seynoxstuffmod.network.PacketJump:onMessage:33]: Added motionY to EntityPlayerMP['Player644'/79, l='border', x=601.12, y=83.25, z=648.43]

 

Link to comment
Share on other sites

You can get rid of your client proxy and use @EventBusSubscriber(modid = YourMod.MODID, value = Side.CLIENT) with static methods in your class

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

6 hours ago, V0idWa1k3r said:

I know it seems counter-intuitive but player motion must be changed on the client, the server doesn't actually control it.

How can i make the client execute player.motionY? world.isRemote isnt working here

Link to comment
Share on other sites

I just tried the mod on server, and it's crashing about the EntityPlayerSP

Spoiler

Caused by: java.lang.NoClassDefFoundError: net/minecraft/client/entity/EntityPlayerSP
	at java.lang.Class.getDeclaredConstructors0(Native Method) ~[?:1.8.0_211]
	at java.lang.Class.privateGetDeclaredConstructors(Unknown Source) ~[?:1.8.0_211]
	at java.lang.Class.getConstructor0(Unknown Source) ~[?:1.8.0_211]
	at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_211]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper.instantiate(SimpleNetworkWrapper.java:166) ~[SimpleNetworkWrapper.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper.registerMessage(SimpleNetworkWrapper.java:159) ~[SimpleNetworkWrapper.class:?]
	at com.seynox.adventum.network.PacketHandler.registerMessage(PacketHandler.java:21) ~[PacketHandler.class:?]
	at com.seynox.adventum.network.PacketHandler.init(PacketHandler.java:14) ~[PacketHandler.class:?]
	at com.seynox.adventum.Adventum.init(Adventum.java:42) ~[Adventum.class:?]

 

How can i make this thread-safe?

Link to comment
Share on other sites

5 minutes ago, Seynox said:

How can i make this thread-safe?

This isn’t about thread safety, this is physical sides. Read https://mcforge.readthedocs.io/en/latest/concepts/sides/. Post your packet class

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

3 minutes ago, Cadiboo said:

Post your packet class

 

PacketHandler :

Spoiler

public class PacketHandler {
	
	public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MODID.toLowerCase());
	
	public static void init() {
		registerMessage(PacketJump.class, PacketJump.JumpMessage.class);
	}
	
	private static int nextPacketId = 0;
	  
	  private static void registerMessage(Class packet, Class message)
	  {
	    INSTANCE.registerMessage(packet, message, nextPacketId, Side.CLIENT);
	    INSTANCE.registerMessage(packet, message, nextPacketId, Side.SERVER);
	    nextPacketId++;
	}

}

 

 

JumpPacket :

Spoiler

public class PacketJump implements IMessageHandler<PacketJump.JumpMessage, IMessage>
{
	  @Override
		public IMessage onMessage(JumpMessage message, MessageContext ctx)
		{
		  EntityPlayer playerID = (ctx.side.isClient() ? Minecraft.getMinecraft().player : ctx.getServerHandler().player);
		  EntityPlayer player = (EntityPlayer)playerID.world.getEntityByID(message.playerid);
		  
		  boolean usedDoubleJump = false;
		  
		  ItemStack belt = BaublesApi.getBaublesHandler(player).getStackInSlot(3);
		  if(!belt.isEmpty() && belt.getItem() instanceof CloudInJar && !usedDoubleJump) {
			    usedDoubleJump = true;
				player.fallDistance = 0;
		  }
		  
		return null;
		}
	  
	  public static class JumpMessage implements IMessage
	  {
	    int playerid;
	    
	    public JumpMessage() {}
	    
	    public JumpMessage(int playerid)
	    {
	      this.playerid = playerid;
	    }
	    
	    @Override
	    public void fromBytes(ByteBuf buf)
	    {
	      this.playerid = ByteBufUtils.readVarInt(buf, 4);
	    }
	    
	    @Override
	    public void toBytes(ByteBuf buf)
	    {
	    	ByteBufUtils.writeVarInt(buf, playerid, 4);
	    }
	}
	  
	  
}

 

 

Link to comment
Share on other sites

5 minutes ago, diesieben07 said:

Why do you check the side in onMessage? Your messages should usually only go one way.

You cannot access client-only classes from common code (such as your IMessageHandler). You need to encapsulate such access using the @SidedProxy system

Can you give me an exemple? I have no idea how i can do this

 

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