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

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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