Solved Event Timing Problem

Discussion in 'Spigot Plugin Development' started by 123SteveHead, May 27, 2016.

  1. Hello Everyone

    I've actually got to issues, but the event timing issue is the show stopper, so I'll ask about it first.

    The plugin I am working on contains several event listeners. Two of the event listeners are PlayerEggThrowEvent and EntityDamageByEntityEvent. For some reason, when I throw an egg at a mob (testing with a horse), the EntityDamageByEntityEvent fires before the PlayerEggThrowEvent. Thus, the plugin is not checking if the egg that damaged the entity is a special egg. Question: Why are the events not firing in chronological order and how can I fix it?

    Code (Text):
    @EventHandler
        public void onPlayerEggThrow(PlayerEggThrowEvent pete)
        {
            Egg theEgg = pete.getEgg();
            Player player = pete.getPlayer();
            ItemStack iih = player.getItemInHand();
            if (iih.hasItemMeta())
            {
                ItemMeta iihData = iih.getItemMeta();
                if (iihData.hasDisplayName())
                {
                    String iihDN = iihData.getDisplayName();
                    if (iihDN.contains("Mob Trapper"))
                    {
                        theEgg.setCustomNameVisible(false);
                        theEgg.setCustomName("pokemon");
                        getLogger().info("1. eggs new custom name = " + theEgg.getCustomName()); // testing
                    }
                }
            }
        }
       
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent edbee)
        {
            Entity victom = edbee.getEntity();
            getLogger().info("2. victom = " + victom.getType().toString()); // testing
            Entity damager = edbee.getDamager();
            getLogger().info("3. damager = " + damager.getType().toString()); // testing
            String damagerName = damager.getCustomName();
            getLogger().info("4. damagerName = " + damagerName); // testing
            if (damagerName != null)
            {
                if (damagerName.contains("pokemon"))
                {
                    getLogger().info("5. the name was pokemon"); // testing
                }
            }
           
        }
    Server output shows:

    [13:31:46 INFO]: [EF_Uts] 2. victom = HORSE
    [13:31:46 INFO]: [EF_Uts] 3. damager = EGG
    [13:31:46 INFO]: [EF_Uts] 4. damagerName = null
    [13:31:46 INFO]: [EF_Uts] 1. eggs new custom name = pokemon

    Question 2:

    Since getItemInHand() is deprecated, what method(s) are replacing it?

    Thanks
     
  2. According to the documents for PlayerEggThrowEvent, the event is called when an egg is thrown and it might hatch. As a substitute to that event, I would use ProjectileLaunchEvent and see how that goes.
     
  3. Try Event Priorities
     
    • Optimistic Optimistic x 1
  4. Just wondering, why are you naming your events "pete" and "edbee?"
     
    • Like Like x 1
  5. p.e.t.e = PlayerEggThrowEvent
    e.d.b.e.e = EntityDamageByEntityEvent

    It's all personal preference for naming them, as long as you follow the conventions. He seems to understand it, so it's all good.
     
  6. Thanks for clearing that up, haha.
     
  7. Actually, they were both called e, but I thought using the same name might be what is causing the problem; it isn't.
     
  8. Code (Text):

    PlayerInventory#getItemInMainHand()
    PlayerInventory#getItemInOffHand()

    //i.e.
    ItemStack handItem = player.getInventory().getItemInMainHand();
     
    Have you tried testing the ProjectileHitEvent to see when it fires in relation to the damage event?
     
  9. Thank You

    Changing the event to ProjectileLaunchEvent worked. It did require a few more extra statements to get the itemStack from the player's inventory; but at least I can now continue.

    Its strange that the playerThrowEggEvent doesn't work since logically it is just a more specific version of the ProjectileLaunchEvent. Now I know not all events are created equally.
     
    • Like Like x 1
  10. Thanks for your reply. I tried your suggestion but it didn't work. The spigot documentation seems to indicate that the event-priorities commands are intended to keep multiple plugins from fighting over the same event. My problem concerns to separate events. GamerzKing's suggestion worked (using projectileLaunchEvent instead of playerEggThrowEvent.
     
  11. Does anyone know how to mark this thread as solved?
     
  12. Top of the page -> Edit Title
     
  13. thanks