Jump to content
  • Home
  • Files
  • Docs
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?
The update for 1.13 is being worked on - please be patient. (Updated 02/19/19)
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 0
ScottehBoeh

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

Started by ScottehBoeh, July 9, 2016

6 posts in this topic

ScottehBoeh    2

ScottehBoeh

ScottehBoeh    2

  • Stone Miner
  • ScottehBoeh
  • Forge Modder
  • 2
  • 68 posts
  • Report post
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.)

 

Share this post


Link to post
Share on other sites

Ernio    595

Ernio

Ernio    595

  • Reality Controller
  • Ernio
  • Forge Modder
  • 595
  • 2638 posts
  • Report post
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.

Share this post


Link to post
Share on other sites

ScottehBoeh    2

ScottehBoeh

ScottehBoeh    2

  • Stone Miner
  • ScottehBoeh
  • Forge Modder
  • 2
  • 68 posts
  • Report post
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?

 

 

Share this post


Link to post
Share on other sites

Ernio    595

Ernio

Ernio    595

  • Reality Controller
  • Ernio
  • Forge Modder
  • 595
  • 2638 posts
  • Report post
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.

Share this post


Link to post
Share on other sites

Choonster    1593

Choonster

Choonster    1593

  • Reality Controller
  • Choonster
  • Forge Modder
  • 1593
  • 4980 posts
  • Report post
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.

Share this post


Link to post
Share on other sites

ScottehBoeh    2

ScottehBoeh

ScottehBoeh    2

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

Ah I understand now. Thankyou :-)

 

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0
Go To Topic Listing Modder Support

  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Pkbldr
      Trouble Updating Forge

      By Pkbldr · Posted 23 minutes ago

    • DaemonUmbra
      I am not able to run the forge installer

      By DaemonUmbra · Posted 25 minutes ago

      That is not a log, that is a screenshot of the error window.
    • DaemonUmbra
      Trouble Updating Forge

      By DaemonUmbra · Posted 26 minutes ago

      That installer log looks incomplete, can you show a screenshot of your launcher?
    • Pkbldr
      Trouble Updating Forge

      By Pkbldr · Posted 31 minutes ago

      I'm trying to install forge-1.12.2-14.23.5.2768-installer-win over .2.2611. It downloads fine, runs fine, says "Successfully installed.....into launcher. Does NOT show up in the launcher. Log attached. Any help very much appreciated installer.log
    • Frick
      Game crash upon startup

      By Frick · Posted 31 minutes ago

      ive been trying to mod version 1.12.2 for a little bit, but when i start up the game i get this error: 
      The game crashed whilst unexpected error
      Error: java.lang.IllegalStateException: Display not created _______________________________________________________________________________ Full crash report:  ---- Minecraft Crash Report ----
      // On the bright side, I bought you a teddy bear! Time: 2/21/19 2:15 PM
      Description: Unexpected error java.lang.IllegalStateException: Display not created
          at org.lwjgl.opengl.Display.processMessages(Display.java:598)
          at net.minecraftforge.fml.client.FMLClientHandler.processWindowMessages(FMLClientHandler.java:1039)
          at net.minecraftforge.fml.common.FMLCommonHandler.processWindowMessages(FMLCommonHandler.java:660)
          at net.minecraftforge.fml.common.ProgressManager.push(ProgressManager.java:55)
          at net.minecraftforge.fml.common.ProgressManager.push(ProgressManager.java:41)
          at net.minecraft.client.resources.SimpleReloadableResourceManager.func_110542_a(SimpleReloadableResourceManager.java:118)
          at net.minecraftforge.client.CloudRenderer.<init>(CloudRenderer.java:82)
          at net.minecraftforge.fml.client.FMLClientHandler.getCloudRenderer(FMLClientHandler.java:1099)
          at net.minecraftforge.fml.client.FMLClientHandler.updateCloudSettings(FMLClientHandler.java:1105)
          at net.minecraftforge.common.ForgeInternalHandler.checkSettings(ForgeInternalHandler.java:97)
          at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_ForgeInternalHandler_checkSettings_ClientTickEvent.invoke(.dynamic)
          at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
          at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182)
          at net.minecraftforge.fml.common.FMLCommonHandler.onPostClientTick(FMLCommonHandler.java:349)
          at net.minecraft.client.Minecraft.func_71407_l(Minecraft.java:1910)
          at net.minecraft.client.Minecraft.func_71411_J(Minecraft.java:1097)
          at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:397)
          at net.minecraft.client.main.Main.main(SourceFile:123)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:497)
          at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
          at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
      A detailed walkthrough of the error, its code path and all known details is as follows:
      --------------------------------------------------------------------------------------- -- System Details --
      Details:
          Minecraft Version: 1.12.2
          Operating System: Windows 10 (amd64) version 10.0
          Java Version: 1.8.0_51, Oracle Corporation
          Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
          Memory: 270715224 bytes (258 MB) / 805306368 bytes (768 MB) up to 2147483648 bytes (2048 MB)
          JVM Flags: 8 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump -Xmx2G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M
          IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
          FML: MCP 9.42 Powered by Forge 14.23.5.2768 4 mods loaded, 4 mods active
          States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored     | State  | ID        | Version      | Source                        | Signature                                |
          |:------ |:--------- |:------------ |:----------------------------- |:---------------------------------------- |
          | UCHIJA | minecraft | 1.12.2       | minecraft.jar                 | None                                     |
          | UCHIJA | mcp       | 9.42         | minecraft.jar                 | None                                     |
          | UCHIJA | FML       | 8.0.99.99    | forge-1.12.2-14.23.5.2768.jar | e3c3d50c7c986df74c645c0ac54639741c90a557 |
          | UCHIJA | forge     | 14.23.5.2768 | forge-1.12.2-14.23.5.2768.jar | e3c3d50c7c986df74c645c0ac54639741c90a557 |     Loaded coremods (and transformers): 
          GL info: ~~ERROR~~ RuntimeException: No OpenGL context found in the current thread.
          Launched Version: 1.12.2-forge1.12.2-14.23.5.2768
          LWJGL: 2.9.4
          OpenGL: ~~ERROR~~ RuntimeException: No OpenGL context found in the current thread.
          GL Caps: Using GL 1.3 multitexturing.
      Using GL 1.3 texture combiners.
      Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported.
      Shaders are available because OpenGL 2.1 is supported.
      VBOs are available because OpenGL 1.5 is supported.     Using VBOs: Yes
          Is Modded: Definitely; Client brand changed to 'fml,forge'
          Type: Client (map_client.txt)
          Resource Packs: 
          Current Language: English (US)
          Profiler Position: N/A (disabled)
          CPU: 4x Intel(R) Core(TM) i5-4570S CPU @ 2.90GHz ____________________________________________________________________________________________________________________________________________________ i would appreciate any help on the subject Thanks! -Frick
  • Topics

    • Pkbldr
      2
      Trouble Updating Forge

      By Pkbldr
      Started 30 minutes ago

    • WesterlyClown
      3
      I am not able to run the forge installer

      By WesterlyClown
      Started Tuesday at 02:26 AM

    • Frick
      0
      Game crash upon startup

      By Frick
      Started 30 minutes ago

    • Defacto
      2
      Add Armor Material Forge 1.13.2

      By Defacto
      Started 1 hour ago

    • maciejsztos
      0
      NPC Follower Guard

      By maciejsztos
      Started 50 minutes ago

  • Who's Online (See full list)

    • DutchM
    • ZigTheHedge
    • Pkbldr
    • Kalman98
    • PulseBeat_02
    • CJWilk
    • crackedEgg
    • Drellocked
    • DaemonUmbra
    • HighCrit
    • Splint
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [SOLVED] Handler to send packet when player is Hurt?
  • Theme
  • Contact Us

Copyright © 2017 ForgeDevelopment LLC Powered by Invision Community