Mob spawner specify drop - need help

Discussion in 'Plugin Development' started by Pedesmix, Jul 23, 2014.

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

    Pedesmix

    I'm really new at this plugin coding and found my first problem that i really can't solve...

    I'm trying to specify what a zombie mob spawners, should drop, when you kill the zombies its spawning.

    I only know how to make the default drop, for all zombies which also encludes natural spawned zombies, but i need to specify it only to spawners.

    My code so far:
    Code:
        @EventHandler
        public void onEntityZ(EntityDeathEvent event) {
            if(event.getEntityType() == EntityType.ZOMBIE){
                event.getDrops().clear();
                ItemStack stack = new ItemStack(Material.GOLDEN_APPLE);
                event.getDrops().add(stack);
               
            }
        }
    
    I know it something with SpawnReason, but not sure how i should add that in?
     
  2. Offline

    Zettelkasten

    Pedesmix Bukkit does not "remember" the reason why an entity spawned, so you will have to do that. First, you want to listen to CreatureSpawnEvent and find out the SpawnReason why the entity spawned. If it is from a spawner, you want to save that entity to a list. Now when an entity dies, you check if the entity is from your list and then modify the drops.
    Saving could be done multiple ways:
    • Using a simple ArrayList
    • Using Metadata
    The problem: None of them are consistent, so your data will be removed when the server is reloaded. You will need to store the entities in a database, config file or what so ever. Using Entity.getUniqueId() you can get an ID which identifies that entity. But you might really want to consider just using Metadata and not to care if data is lost on server reload.

    Here is a forum post that might interest you: https://forums.bukkit.org/threads/check-if-entity-was-spawned-from-mob-spawner.114451/
     
  3. Offline

    DevilMental

    Pedesmix Correct me if I'm wrong, but put every entity spawned by a mob spawner in a list and on the method onEntityZ check if the entity is in the list, if true then add the item into the drops ;)

    Code:java
    1. public static List<LivingEntity> spawnedMobSpawner = new ArrayList<LivingEntity>();
    2.  
    3. @EventHandler
    4. public void onCreatureSpawn(CreatureSpawnEvent e){
    5. if (e.getSpawnReason()==SpawnReason.SPAWNER){
    6. spawnerMobSpawner.add(e.getEntity());
    7. }
    8. }
    9.  
    10. @EventHandler
    11. public void onEntityZ(EntityDeathEvent event) {
    12. if(event.getEntityType() == EntityType.ZOMBIE){
    13. event.getDrops().clear();
    14. if (this.spawnedMobSpawner.contains(event.getEntity()){
    15. ItemStack stack = new ItemStack(Material.GOLDEN_APPLE);
    16. event.getDrops().add(stack);
    17. this.spawnedMobSpawner.remove(event.getEntity());
    18. }
    19. }
    20. }
    21.  


    Hope that helps,
    ~DevilMental
     
  4. Offline

    Pedesmix

    Zettelkasten I see what you mean by that. The only other way, is to kill all the mobs when i restart the server, but the databass is much more usefull in long terms.

    DevilMental Thanks alot! That works perfectly! Just need to add the databass somehow, but that something i will try to solf myself.

    But there is one other think i was thinking about now tho, that im not sure i can find a solution to that easy.. What if i got 2 Zombie Spawners, and i want the zombies from those 2 Spawners to drop diffrence items, when you kill the zombies they spawn?
     
  5. Offline

    Zettelkasten

    You have two options here: Save from which spawner the zombie got spawned, or the drops it should drop. For that, make you List into a Map:
    Code:java
    1. Map<LivingEntity, Collection<ItemStack>> spawnedMobSpawnerDrops = new HashMap<LivingEntity, Collection<ItemStack>>();
     
  6. Offline

    Pedesmix

    Zettelkasten I just learned about HashMaps and know somewhat how to use it, but I'm not sure how i can save which spawner the zombies get spawned from?

    I'm not sure what you mean with the other option you gave me. Could you maybe explain it a little more? Sorry, it's really new to me
     
  7. Offline

    Zettelkasten

    Pedesmix You can store a spawner by storing its coordinates, I think just the Location of the spawner block should do the trick. Your Map would look like this then:
    Code:java
    1. Map<LivingEntity, Location> spawnedMobSpawner = new HashMap<LivingEntity, Location>();

    With storing the drops I mean to save what a zombie will drop when he gets killed other then from which spawner he spawned. So you would save something like this: Zombie 1 -> 1 Rotten Flesh; Zombie 2 -> 3 Rotten Flesh, 1 Diamond ...
    The map for that is a bit more complicated, as it uses a Collection (that is something like an unsorted list) in the map:
    Code:java
    1. Map<LivingEntity, Collection<ItemStack>> spawnedMobSpawnerDrops = new HashMap<LivingEntity, Collection<ItemStack>>();

    The EntityDeathEvent also uses Collections to store what items to drop:
    Code:java
    1. event.getDrops();


    Or, here is a simpler solution not using maps (but therefore not as flexible):
    Use multiple lists, one list for each drop type:
    Code:java
    1. List<LivingEntity> zombiesThatDropDiamonds;
    2. List<LivingEntity> zombiesThatDropEggs;
    3. List<LivingEntity> zombiesThatDropNothing;
    4. // ...
     
Thread Status:
Not open for further replies.

Share This Page