Jump to content

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


ScottehBoeh

Recommended Posts

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

 

Link to comment
Share on other sites

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.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

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.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.