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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Here is a tutorial from this same forum that I tried and kinda made work. Take into account that you will have to manage the offset (like rotation, and the offset relative to things like the main hand, offhand etc) by yourself and that can get very troublesome at times.  
    • I have done this now but have got the error:   'food(net.minecraft.world.food.FoodProperties)' in 'net.minecraft.world.item.Item.Properties' cannot be applied to                '(net.minecraftforge.registries.RegistryObject<net.minecraft.world.item.Item>)' public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register( "lemon_juice", () -> new Item( new HoneyBottleItem.Properties().stacksTo(1).food( (new FoodProperties.Builder()) .nutrition(3) .saturationMod(0.25F) .effect(() -> new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 1500), 0.01f ) .build() ) )); The code above is from the ModFoods class, the one below from the ModItems class. public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register("lemon_juice", () -> new Item(new Item.Properties().food(ModFoods.LEMON_JUICE)));   I shall keep going between them to try and figure out the cause. I am sorry if this is too much for you to help with, though I thank you greatly for your patience and all the effort you have put in to help me.
    • I have been following these exact tutorials for quite a while, I must agree that they are amazing and easy to follow. I have registered the item in the ModFoods class, I tried to do it in ModItems (Where all the items should be registered) but got errors, I think I may need to revert this and figure it out from there. Once again, thank you for your help! 👍 Just looking back, I have noticed in your code you added ITEMS.register, which I am guessing means that they are being registered in ModFoods, I shall go through the process of trial and error to figure this out.
    • ♈+2349027025197ஜ Are you a pastor, business man or woman, politician, civil engineer, civil servant, security officer, entrepreneur, Job seeker, poor or rich Seeking how to join a brotherhood for protection and wealth here’s is your opportunity, but you should know there’s no ritual without repercussions but with the right guidance and support from this great temple your destiny is certain to be changed for the better and equally protected depending if you’re destined for greatness Call now for enquiry +2349027025197☎+2349027025197₩™ I want to join ILLUMINATI occult without human sacrificeGREATORLDRADO BROTHERHOOD OCCULT , Is The Club of the Riches and Famous; is the world oldest and largest fraternity made up of 3 Millions Members. We are one Family under one father who is the Supreme Being. In Greatorldrado BROTHERHOOD we believe that we were born in paradise and no member should struggle in this world. Hence all our new members are given Money Rewards once they join in order to upgrade their lifestyle.; interested viewers should contact us; on. +2349027025197 ۝ஐℰ+2349027025197 ₩Greatorldrado BROTHERHOOD OCCULT IS A SACRED FRATERNITY WITH A GRAND LODGE TEMPLE SITUATED IN G.R.A PHASE 1 PORT HARCOURT NIGERIA, OUR NUMBER ONE OBLIGATION IS TO MAKE EVERY INITIATE MEMBER HERE RICH AND FAMOUS IN OTHER RISE THE POWERS OF GUARDIANS OF AGE+. +2349027025197   SEARCHING ON HOW TO JOIN THE Greatorldrado BROTHERHOOD MONEY RITUAL OCCULT IS NOT THE PROBLEM BUT MAKE SURE YOU'VE THOUGHT ABOUT IT VERY WELL BEFORE REACHING US HERE BECAUSE NOT EVERYONE HAS THE HEART TO DO WHAT IT TAKES TO BECOME ONE OF US HERE, BUT IF YOU THINK YOU'RE SERIOUS MINDED AND READY TO RUN THE SPIRITUAL RACE OF LIFE IN OTHER TO ACQUIRE ALL YOU NEED HERE ON EARTH CONTACT SPIRITUAL GRANDMASTER NOW FOR INQUIRY +2349027025197   +2349027025197 Are you a pastor, business man or woman, politician, civil engineer, civil servant, security officer, entrepreneur, Job seeker, poor or rich Seeking how to join
  • Topics

×
×
  • Create New...

Important Information

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