Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [SOLVED] Handler to send packet when player is Hurt?
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 0
ScottehBoeh

[SOLVED] Handler to send packet when player is Hurt?

By ScottehBoeh, July 9, 2016 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

ScottehBoeh    2

ScottehBoeh

ScottehBoeh    2

  • Stone Miner
  • ScottehBoeh
  • Forge Modder
  • 2
  • 68 posts
Posted July 9, 2016

Having issues setting up a Handler that sends a packet when the player is hurt. This packet then creates red particle effects at the player. (Basically blood).

Sadly the event handler does not pick up when the player is hurt, and I'm having troubles with finding out why its not detecting damage.

 

Any chance someone can have a crack at this code?

 

My Damage Event Handler:

public class DamageHandler {

 

    public boolean PlayerHurt = false;

 

    @SubscribeEvent

    public void Hurt(LivingHurtEvent event) {

        System.out.println("Hurt");

        if(event.entity instanceof EntityPlayer) {

            PlayerHurt = false;

            System.out.println("Damage Taken");

        }

    }

 

    @SubscribeEvent

    public void playerTick(TickEvent.PlayerTickEvent event) {

        if(PlayerHurt == true) {

            System.out.println("Sent Bleed Packet");

            PacketHandler.INSTANCE.sendToServer((IMessage)new MessageBleed(event.player.lastTickPosX, event.player.lastTickPosY, event.player.lastTickPosZ));

            PlayerHurt = false;

            return;

            }

        }

    }

 

 

My Packet/Message Sent:

public class MessageBleed

implements IMessage,

IMessageHandler<MessageBleed, IMessage> {

    private Random rand = new Random();

    private double x;

    private double y;

    private double z;

 

    public MessageBleed() {

    }

 

    public MessageBleed(double posX, double posY, double posZ) {

        this.x = posX;

        this.y = posY;

        this.z = posZ;

    }

 

    public void fromBytes(ByteBuf buf) {

        this.x = buf.readDouble();

        this.y = buf.readDouble();

        this.z = buf.readDouble();

    }

 

    public void toBytes(ByteBuf buf) {

        buf.writeDouble(this.x);

        buf.writeDouble(this.y);

        buf.writeDouble(this.z);

    }

 

 

    public IMessage onMessage(MessageBleed message, MessageContext ctx) {

        EntityPlayerMP player = ctx.getServerHandler().playerEntity;

        player.worldObj.spawnParticle("reddust", player.posX, player.posY, player.posZ, 0, 0, 0);

        System.out.println("Spawned Blood");

        return null;

    }

}

 

 

 

Could it be that I'm setting/registering my packets wrong?

How my packet messages are registered:

public class PacketHandler {

    public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("ctx");

 

    public static void init() {

        INSTANCE.registerMessage((Class)MessageWhistle.class, (Class)MessageWhistle.class, 0, Side.SERVER);

        INSTANCE.registerMessage((Class)MessageBleed.class, (Class)MessageBleed.class, 1, Side.SERVER);

    }

}

(The other packet plays a whistle sound when the player presses F. That works perfectly fine.)

 

  • Quote

Share this post


Link to post
Share on other sites

Ernio    598

Ernio

Ernio    598

  • Reality Controller
  • Ernio
  • Forge Modder
  • 598
  • 2638 posts
Posted July 9, 2016

You SOOOO can't do it.

You can't just combine 2 events with global variable and expect it to work - I hope that was for testing.

 

As to hurting - LivingHurtEvent is fired only on server and should be enough - just send packet from there.

 

Also - wtf?

PacketHandler.INSTANCE.sendToServer

 

You are supposed to send packet from server to client about entity hurt.

 

You also don't need new Random - use entity.rand or world.rand instances. Waste of power.

 

Make sure you registered event and packets on right side (for handler) - So handler = Side.CLIENT, not SERVER like you did.

  • Quote

Share this post


Link to post
Share on other sites

ScottehBoeh    2

ScottehBoeh

ScottehBoeh    2

  • Stone Miner
  • ScottehBoeh
  • Forge Modder
  • 2
  • 68 posts
Posted July 9, 2016

You SOOOO can't do it.

You can't just combine 2 events with global variable and expect it to work - I hope that was for testing.

 

As to hurting - LivingHurtEvent is fired only on server and should be enough - just send packet from there.

 

Also - wtf?

PacketHandler.INSTANCE.sendToServer

 

You are supposed to send packet from server to client about entity hurt.

 

You also don't need new Random - use entity.rand or world.rand instances. Waste of power.

 

Make sure you registered event and packets on right side (for handler) - So handler = Side.CLIENT, not SERVER like you did.

 

Thanks for the reply. :-) I've made sure that the handler is now running only server-side. I've also changed the code on my damage handler, however I'm not quite sure on how to set up the handler, now. Any chance you or someone else knows a good way to set up the server-side handler?

 

 

  • Quote

Share this post


Link to post
Share on other sites

Ernio    598

Ernio

Ernio    598

  • Reality Controller
  • Ernio
  • Forge Modder
  • 598
  • 2638 posts
Posted July 9, 2016

Why the hell do your want you handler to be server sided? It is supposed to be CLIENT sided! You send packet from server to client to say "Hey bro, spawn this particle for me!"

 

EDIT

 

1. @SubscribeEvent to LivingHurtEvent

2. Check if event.getLivingEntity() fulfils your requirements

3. From event send packet to ALL client tracking given entity.

Entity#World#getEntityTracker();
EntityTracker#getTrackingPlayers(Entity entity);

* Iterate over tracking players and send packet to each of them

4. Packet will contain:

int entityId;

5. On client - inside handler:

World#getEntityByID()

* Check if not null, and spawn particles at entity's x/y/z.

 

Packet handler will be registered with Side.CLIENT.

 

This is WHOLE algorithm. If you don't do it like above, you are (most likely) doing it wrong.

 

Edit 2:

In earlier versions EntityTracker also allows you to get/send to trackers, but methods are different.

  • Quote

Share this post


Link to post
Share on other sites

Choonster    1624

Choonster

Choonster    1624

  • Reality Controller
  • Choonster
  • Forge Modder
  • 1624
  • 5051 posts
Posted July 9, 2016

Always specify the Minecraft version you're using in the title. Judging by your code you're using 1.7.10, which is very outdated and no longer officially supported. Update to 1.10.2.

 

There are a few issues here.

 

You never set

PlayerHurt

to

true

, so the condition in your

PlayerTickEvent

handler is never met.

 

You can't store per-player data in event handlers, there can be any number of players on a server all doing thing simultaneously.

 

Why are you sending the packet a tick after the player is hurt instead of sending it straight away?

 

You're mixing up sides here.

LivingHurtEvent

is only fired on the server side, it's never fired on the client side. You send a packet to the server that calls

World#spawnParticle

, but this does nothing on the server. You need to listen for damage on the server and then send packets to the clients of nearby players to spawn the particles.

 

You don't need your own packet, you can use

WorldServer#func_147487_a

(

WorldServer#spawnParticle

in 1.10.2) to send a packet telling clients to spawn particles.

  • Quote

Share this post


Link to post
Share on other sites

ScottehBoeh    2

ScottehBoeh

ScottehBoeh    2

  • Stone Miner
  • ScottehBoeh
  • Forge Modder
  • 2
  • 68 posts
Posted July 9, 2016

Ah I understand now. Thankyou :-)

 

And I apologise for showing 1.7.10 code without informing it in the title.

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 0
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • DragonITA
      How create a custom mob / entity in 1.14 and above

      By DragonITA · Posted 55 minutes ago

      Why you resolved it.
    • DragonITA
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA · Posted 1 hour ago

      I want add a screenshoot.
    • DragonITA
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA · Posted 1 hour ago

      Ok, i have tried with making something, and the Unicorn spawn, but without a texture. The model is just like a white Brick it dosent have a texture.
    • diesieben07
      [1.12.2] Register EntityEntry

      By diesieben07 · Posted 1 hour ago

      Coremods are not officially supported on this forum, but that would be a way to change it, yes.
    • diesieben07
      Game crashing when the block is activated

      By diesieben07 · Posted 1 hour ago

      You don't own this domain. This makes no sense. One tile entity class registered as two tile entity types? What are you trying to achieve? You did not register this tile entity class.
  • Topics

    • Bantix
      9
      How create a custom mob / entity in 1.14 and above

      By Bantix
      Started September 2

    • DragonITA
      50
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA
      Started December 9

    • 56zt45z4th
      3
      [1.12.2] Register EntityEntry

      By 56zt45z4th
      Started Friday at 06:34 PM

    • jun2040
      10
      Game crashing when the block is activated

      By jun2040
      Started Thursday at 04:51 PM

    • daylite
      3
      server tick loop

      By daylite
      Started 11 hours ago

  • Who's Online (See full list)

    • EtherMods
    • Pitras_MLG
    • Alatyami
    • nachillodr
    • SamLikeHam
    • matt1999rd
    • GDavid
    • reyenderman_MC
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [SOLVED] Handler to send packet when player is Hurt?
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community