CreeperPowerEvent causing chat spam

Discussion in 'Plugin Development' started by Windows_i7_920, Feb 25, 2012.

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

    Windows_i7_920

    I am making a custom plugin for my server and one of its functions is to announce to everyone when and where a creeper gets hit by lightning. This event did trigger when I used Essentials thor command on a creeper, but it spammed the chat 4+ times. Why did it do that? I am using the latest RB.

    Code:
        @EventHandler
        public void onCreeperPower(CreeperPowerEvent e)
        {
            String world = e.getEntity().getWorld().getName();
            String coords = "(" + Integer.toString(e.getEntity().getLocation().getBlockX()) + "," +
                                  Integer.toString(e.getEntity().getLocation().getBlockY()) + "," +
                                  Integer.toString(e.getEntity().getLocation().getBlockZ()) + ")";
            e.getEntity().getServer().broadcastMessage("§e[ActionCraft PL] §2A creeper has just been hit by lightning in '" + world + "'!");
            e.getEntity().getServer().broadcastMessage("§2Location: §e" + coords);
            return;
        }
     
  2. Offline

    Gravity

    I'm not sure, but I have a hunch that the creeper might actually get "powered" multiple times... Maybe try storing all the entities that get struck and not announcing them a second time, or just killing the mob after the event ends or something.
     
  3. Offline

    Taco

    A possible solution would be to add their entity ID to a list and to only say that they've been powered if they're not in that list. Then listen in on death and despawn events and remove them there to keep the list from becoming massive.
     
  4. Offline

    nisovin

    You might also try checking to make sure the creeper isn't already powered before sending the message. Something like:

    if (((Creeper)event.getEntity()).isPowered()) return;
     
    Windows_i7_920 likes this.
  5. Offline

    Windows_i7_920

    That sounds like an easy, and clean way of handling this.

    Edit: Tried, and it did not work. Is this perhaps a Bukkit bug causing this odd behavior?

    Edit: Finally got it to work properly with:
    Return statements seem to have no effect with void functions. Java quirk?
    Code:
        @EventHandler
        public void onCreeperPower(CreeperPowerEvent e)
        {
            if(!((Creeper)e.getEntity()).isPowered())
            {
                String world = e.getEntity().getWorld().getName();
                String coords = "(" + Integer.toString(e.getEntity().getLocation().getBlockX()) + "," +
                                      Integer.toString(e.getEntity().getLocation().getBlockY()) + "," +
                                      Integer.toString(e.getEntity().getLocation().getBlockZ()) + ")";
                e.getEntity().getServer().broadcastMessage("§e[ActionCraft PL] §2A creeper has just been hit by lightning in '" + world + "'!");
                e.getEntity().getServer().broadcastMessage("§2Location: §e" + coords);
         
            }
        }
     
  6. Offline

    nisovin

    Return statements should work fine inside void functions.
     
  7. Offline

    Windows_i7_920

    Yeah, it should but it was not. When I use a return statement to try to exit the function immediately, control continues anyway. I tried several times. I will try some more though, as most likely something else went wrong that caused it to behave like that. I'm thinking maybe I forgot to re-export the jar before I tested again or something...

    However, the general method you mentioned does the trick.
     
Thread Status:
Not open for further replies.

Share This Page