[solved]getSpawnReason on entityDeathEvent

Discussion in 'Plugin Development' started by BobbyD441, Jun 4, 2012.

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

    BobbyD441

    I'm looking for a way to determinate the spawn reason of any mob when it dies. This is to cancel out abuse of my plugin KillsPay when using a mob spawner. I'm hoping this can be done without logging all spawning mobs =)
     
  2. I guess you need to do just that.
     
  3. Offline

    Malikk

    rather than logging them all, you might consider assigning them all metadata. This, of course, isn't persistent, but it's a lot more lightweight.
     
  4. It's not all that inefficient when you only need to save wether the spawn was by a mob spawner or not.
    When the mob spawns, check if SpawnReason == MOB_SPAWNER (or whatever it's called).
    If so, use entity.getUniqueId() and put it in a HashSet. Then, when the mob dies, check if it's id is contained there.

    That way, you only keep track of the mobspawner-mobs, not all of them. So if someone decides to spawn 500 mobs with a plugin command, your plugin won't create any impact. Considering that there are most likely not 1000 mob-spawners in the world, the HashSet shouldn't become too large.

    Seriously, 16 bytes per mob and a hashed contains-check when a mob dies, that's nothing to worry about.
     
  5. Offline

    BobbyD441

    Thanks for all the replies, I think the hashmap of MOB_SPAWNER is my best option. I'll let you know how it goes =)

    Well I made it into a hashmap like so:
    Code:
    public HashMap<UUID, Boolean> spawnerList = new HashMap<UUID, Boolean>();
    @EventHandler
        public void onCreatureSpawnEvent(CreatureSpawnEvent evt)
        {
            if(evt.getSpawnReason() == SpawnReason.SPAWNER)
            {
                spawnerList.put(evt.getEntity().getUniqueId(), true);
            }
        }
    In the onEntityDeath event I read it out like so:
    Code:
    boolean spawnedFromSpawner = false;
                try
                {
                    spawnedFromSpawner = spawnerList.get(evt.getEntity().getUniqueId());
                }
                catch (Exception e)
                {
                   
                }
                if(spawnedFromSpawner)
                {
                    spawnerList.remove(evt.getEntity().getUniqueId());
                    return;
                }
    This works fine, thanks for the help =)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  6. It would be better if you just use just a list or set since you don't need the additional boolean value right?
     
  7. Please re-read my post carefully, I was talking about a HashSet, not a HashMap. All you need is a yes/no (a contains check).

    Just do yourSet.add(evt.getEntity().getUniqueId()) in your spawn listener.
    And that's how you check it efficiently:
    Code:
    boolean spawnedFromSpawner = yourSet.remove(entity.getUniqueId());
    Background: The remove-method removes the element if it exists and returns true if it existed, it simply returns false if it didn't exist. That's only one method call, beat that!

    kumpelblase2 Change "list" to "set", then I agree. Contains-checks on lists are slow ;)
     
  8. :p
     
Thread Status:
Not open for further replies.

Share This Page