[solved]Need help with EntityDamageByEntityEvent

Discussion in 'Plugin Development' started by llamasaylol, Feb 17, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    llamasaylol

    Here is the code:
    Code:Java
    1. public class Tagging implements Listener {
    2.  
    3. public Tag plugin;
    4.  
    5. @EventHandler(priority = EventPriority.NORMAL)
    6. public void onEntityDamage (EntityDamageByEntityEvent event) {
    7. Entity wounded = event.getEntity();
    8. if (event.getDamager() instanceof Player) {
    9. ((Player)event.getDamager()).sendMessage("d1");
    10. if (wounded instanceof Player) {
    11. ((Player)wounded).sendMessage("w2");
    12. ((Player)event.getDamager()).sendMessage("d2");
    13. //My code.
    14. } else {
    15. ((Player)event.getDamager()).sendMessage("Hit Humans.");
    16. }
    17. }
    18. }
    19. }

    I added in the d1, w2 etc. to help me find where the problem is.
    When I hit an animal I receive "d1" and "Hit Humans.". When I Hit a Human tagged or not I only receive the "d1" and nothing else. (I should receive either "Hit Humans." or "w2" and "d2" (and then some more stuff after that, depends on the 'if' statements though))
    Please could you explain to me why it is not working.
    KTHXBAI ~rrama

    EDIT: got rid of the code that isn't concerned
     
  2. Offline

    Father Of Time

    Oh dear lord that is sooo ugly... no offense but jesus...

    Dude, don't keep getting and casting the player object, get and cast it once to a local variable and reuse that variable; doing it this way is extremely inefficient and ugly to read...

    With that said, what are you exactly trying to do, differentiate between when a player hits a player and and when a player hits anything else?
     
  3. Offline

    llamasaylol

    I'm only putting the 'sendMessage()' in for now, that's why it's not pretty. Also, yes. I'm just confused about why I receive nothing from the 'if' and 'else' statement (not even an error).
     
  4. Offline

    Father Of Time

    I wrote this freehand in notepad so it may not work out of the gate, but the concept is there:

    Code:
        @EventHandler(priority = EventPriority.NORMAL)
        public void onEntityDamage ( EntityDamageEvent event )
        {
            if( event.getCause() == DamageCause.ENTITY_ATTACK )
            {
                EntityDamageByEntityEvent EvEevent = (EntityDamageByEntityEvent)event;
                Entity attackerentity = EvEevent.getDamager();
                Entity defenderentity = EvEevent.getEntity();
               
                if( attackerentity instanceof Player && defenderentity instanceof Player )
                {
                    Player attacker = (Player)attackerentity;
                    Player defender = (Player)defenderentity;
                   
                    // Do what ever with players here, its gurenteed its a player attacking a player here.
                }
            }
        }
    You first catch the entity damage event (because no event is triggered for entitydamageentity event). Once you get the damage event you check the source of the damage, if that damage source happens to be a source that could be a player (entity_attack, projectile or magic) you cast the event to an entitydamagebyentityevent. Once its cast to that type you gain new functions to call, two being the getdamager and getentity, simply get those entities and check to see if both are players, if so you have a player vs player attack. Pay attention to how I cast variables that I get from the events once to local variables, then reuse those variables over and over, this is something you should practice doing moving forward.

    I hope this helps, good luck with your project!
     
    ShadowDrakken likes this.
  5. Offline

    llamasaylol

    Bukkit didn't scrap 'EntityDamageByEntityEvent' In the end so this shouldn't make a difference (But I will try calling it from 'EntityDamageEvent' (Just in case they put it back but didn't make sure it worked)

    Also, I only use local variables when I use something more than about 3 times, else I just get confused with all the local variables I have (Doesn't matter what I name them, I will still forget what they are).
    Call it bad IDC, different coders have different preferences.

    EDIT: Didn't work.
     
  6. Offline

    Father Of Time

    Okay... and you expect us to do what with this information? You need to post the changes you made so that we can help you correct your mistakes...

    To be completely honest if you wish to continue to use poor coding practices because "it's just what you do" then so be it, but I'm sorry, I won't assist in the process.

    Good luck with your project, I hope you resolve your issue.
     
  7. Offline

    Arrem

    The problem is, you're using Java, not C#. You're supposed to use C# for mcforge custom commands. And you're doing it wrong
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Net;
    using System.Media;
    using System.Reflection;
    using System.Diagnostics;
    using System.Globalization;
    using System.Threading;
    using System.Security;
    using System.Runtime;
    using System.Web;
    using System.Text;
    using System.CodeDom;
    using System.Collections;
    using System.Data;
    using System.Deployment;
    using System.Dynamic;
    
    namespace MCForge
    {
        class CmdPizza : Command
        {
            public override string name { get { return "pizza"; } }
            public override string shortcut { get { return ""; } }
            public override string type { get { return "other"; } }
            public override bool museumUsable { get { return true; } }
            public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
            public CmdPizza() { }
    
            public override void Use(Player p, string message)
            {
                p.SendMessage("Have some pizza " + p.color + p.name);
            }
            public override void Help(Player p)
            {
                Player.SendMessage(p, "/pizza - get some pizza.");
            }
          }
    }
       
    Try that code it should work
     
  8. Offline

    llamasaylol

    Love you Alem xxx I believe I have found my problem. (This place is more sophisticated than the McForge forums, they don't like jokes too much :()
     
  9. Offline

    Arrem

    I'm sori I was on the wrong forum.
     
  10. Offline

    llamasaylol

    Did you make an account just for that joke?
     
  11. Offline

    Arrem

    Ummm no

    @EventHandler(priority = EventPriority.NORMAL)
    public void onEntityDamage ( EntityDamageEvent event )
    {
    if( event.getCause() == DamageCause.ENTITY_ATTACK )
    {
    EntityDamageByEntityEvent EvEevent = (EntityDamageByEntityEvent)event;
    Entity attackerentity = EvEevent.getDamager();
    Entity defenderentity = EvEevent.getEntity();

    if( attackerentity instanceof Player && defenderentity instanceof Player )
    {
    Player atk = (Player)attackerentity;
    Player def = (Player)defenderentity;

    And put your code here
    }
    }
    That shoudl work for real

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  12. Offline

    Bisa

    Just curious, how did you figure that one out? - I can't seem to understand how you realized he needed to change language, let alone how you knew he wanted to use MCForge :confused:
     
  13. Offline

    llamasaylol

    Alem stop spamming or I will start spamming McForge forums after everyone of your posts.

    Alem (I know him elsewhere) is from McForge and uses C# (AKA he doesn't know much about real coding (lol jk I wub you arrem)) He is magic...
     
  14. Offline

    Drakonix

    Use EntityDamageEvent and than check if event is an instance of EntityDamageByEntityEvent.
     
  15. Offline

    llamasaylol

    I have tried that but it has not worked :(
     
  16. Offline

    Drakonix

    Show the code.
     
  17. Offline

    llamasaylol

    I have tried this:
    Code:Java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onEntityDamage (EntityDamageEvent event1) {
    3. if (event1.getCause() == DamageCause.ENTITY_ATTACK) {
    4. EntityDamageByEntityEvent event = (EntityDamageByEntityEvent)event1;
    5. Entity wounded = event.getEntity();
    6. Entity Damager = event.getDamager();
    7. if (Damager instanceof Player) {
    8. Player Attacker = (Player)Damager;
    9. Attacker.sendMessage("d1");
    10. if (wounded instanceof Player) {
    11. Player PWounded = (Player)wounded;
    12. PWounded.sendMessage("w2");
    13. Attacker.sendMessage("d2");
    14. //my code
    15. } else {
    16. ((Player)event.getDamager()).sendMessage("Hit Humans.");
    17. }
    18. }
    19. }
    20. }

    When I hit a player I receive "d1" then nothing.

    My next step is to try making wounded come from event1 (Entity wounded = event1.getEntity();).
    I hope it will work probably won't make a difference but you never know.

    EDIT: Setting it to 'Entity wounded = event1.getEntity()' worked!
    Thanks Father Of Time and Drakonix !
     
Thread Status:
Not open for further replies.

Share This Page