1 input yet double output?

Discussion in 'Plugin Development' started by chris8787878787, Aug 26, 2015.

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

    chris8787878787

    Code:
    @EventHandler
        public void onEntityDeath(EntityDeathEvent e)
        {
             if (!(e.getEntity() instanceof Pig))
             {
                 return;
             }
                 else
                 {
                 e.getDrops().clear();
                 e.getEntity().getWorld().dropItem(e.getEntity().getLocation(), new ItemStack(Material.DIAMOND));
                 e.getEntity().getKiller().sendMessage("Test works");
                 return;
                 }
        }
    Should say "test works" once and drop 1 diamond but it drops 2 diamonds instead and outputs the mess age twice. Ideas?
     
  2. Offline

    SuperSniper

    try not using an "else" statement, just do this:

    Code:
    @EventHandler
        public void onEntityDeath(EntityDeathEvent e)
        {
             if (!(e.getEntity() instanceof Pig))
             {
                 return;
             }
                 e.getDrops().clear();
                 e.getEntity().getWorld().dropItem(e.getEntity().getLocation(), new ItemStack(Material.DIAMOND));
                 e.getEntity().getKiller().sendMessage("Test works");
                 return;
                 }
        }
    
    @chris8787878787
     
  3. Offline

    chris8787878787

  4. Offline

    SuperSniper

    Maybe this?

    Don't check if the Entity is NOT a Pig, check if it IS a pig, then just carry on with the code like that
    @chris8787878787
     
  5. Offline

    mythbusterma

    @chris8787878787

    Format your code properly please.


    @SuperSniper

    Why the hell would that make a difference? Like I cannot understand the logic at all.


    @chris8787878787

    I'm willing to bet you register your Listener twice, don't do that.
     
    au2001 likes this.
  6. Offline

    Tecno_Wizard

    Seconding that. You probably registered the same listener twice.
     
    ferrybig likes this.
  7. Offline

    chris8787878787

    @mythbusterma @Tecno_Wizard Sorry friends, couldn't reply on time. And no didn't listen for the event more than once, I checked all o my classes. The problem still ensues.
     
  8. Offline

    mythbusterma

    @chris8787878787

    I didn't say you listened for it more than once, I said you registered the listener more than once.
     
  9. Offline

    chris8787878787

    @mythbusterma Yup didn't do that either, assuming you mean I used "implements Listener" across multiple classes.
     
  10. Offline

    mythbusterma

    @chris8787878787

    No, that's not what I meant. I meant you register the same listener more than once.
     
  11. Offline

    Tecno_Wizard

    @chris8787878787, you called JavaPlugin#getPluginManager.registerListener(Listener) twice.
    @mythbusterma if I got the path wrong feel free to correct me. I don't have the docs open.
     
  12. Offline

    mythbusterma

    @Tecno_Wizard

    It would be PluginManager#registerListener(...), as the hash symbol indicates that the method is an instance method rather than a static one, and it isn't usually chained like that.

    But you get the general idea.
     
Thread Status:
Not open for further replies.

Share This Page