MobSpawners: Which Mob? (Break-Event)

Discussion in 'Plugin Development' started by Neui, Feb 7, 2012.

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

    Neui

    Hello. I deplovent a Plugin. So, now, if somebody break a Mobspawner, then it drops the Spawner-Eggs. But: How can i see, if its a Zombie/skeleton/spider/cavespider/blaze spawner?

    (And: How i drop metaData-ID's?)

    (And²: I'm a German, so sorry for my BAD english)
     
  2. Offline

    Njol

    Neui likes this.
  3. Offline

    mushroomhostage

    If you want a complete example, check out my SilkSpawners plugin, it does exactly this (and is open source). In short you can use the CreatureSpawner block state to get the creature type, then set the damage value of a monster egg item to the corresponding entity ID.
     
  4. Offline

    Neui

    THANKS Njol! It works!
    But, i must change a little the Code, but it works. It says an error in Line 32. ( CreatureSpawner spawner = (CreatureSpawner) event.getBlock().getState(); )

    Here is my Code:
    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onBlockBreaki(BlockBreakEvent event){
    3. Random dr = new Random();//LINE 30
    4.  
    5. CreatureSpawner spawner = (CreatureSpawner) event.getBlock().getState(); //LINE 32
    6. int spawneridI = getCreatureID(spawner);//LINE 33
    7.  
    8. if(spawneridI != 0){
    9. short metadata = (short)spawneridI;
    10. event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation()
    11. ,new ItemStack(344, 4 + dr.nextInt(12), metadata));}
    12. }
    13.  
    14. private int getCreatureID(CreatureSpawner cp){
    15. String cps = cp.getCreatureTypeId(); int isConv = 0;
    16. if (cps.equalsIgnoreCase("creeper")) isConv = 50;
    17. if (cps.equalsIgnoreCase("skeleton")) isConv = 51;
    18. if (cps.equalsIgnoreCase("spider")) isConv = 52;
    19. if (cps.equalsIgnoreCase("giant")) isConv = 53;
    20. if (cps.equalsIgnoreCase("zombie")) isConv = 54;
    21. if (cps.equalsIgnoreCase("slime")) isConv = 55;
    22. if (cps.equalsIgnoreCase("ghast")) isConv = 56;
    23. if (cps.equalsIgnoreCase("pigman")) isConv = 57;
    24. if (cps.equalsIgnoreCase("enderman")) isConv = 58;
    25. if (cps.equalsIgnoreCase("cavespider")) isConv = 59;
    26. if (cps.equalsIgnoreCase("silverfish")) isConv = 60;
    27. if (cps.equalsIgnoreCase("blaze")) isConv = 61;
    28. if (cps.equalsIgnoreCase("magmacub")) isConv = 62;
    29. if (cps.equalsIgnoreCase("enderdragon")) isConv = 63;
    30.  
    31. if (cps.equalsIgnoreCase("pig")) isConv = 90;
    32. if (cps.equalsIgnoreCase("sheep")) isConv = 91;
    33. if (cps.equalsIgnoreCase("cow")) isConv = 92;
    34. if (cps.equalsIgnoreCase("chicken")) isConv = 93;
    35. if (cps.equalsIgnoreCase("squid")) isConv = 94;
    36. if (cps.equalsIgnoreCase("wolf")) isConv = 95;
    37. if (cps.equalsIgnoreCase("mooshroom")) isConv = 96;
    38. if (cps.equalsIgnoreCase("snowgolem")) isConv = 97;
    39. if (cps.equalsIgnoreCase("ocelot")) isConv = 98;
    40.  
    41. if (cps.equalsIgnoreCase("villager")) isConv = 98;
    42. return isConv;
    43. }
    (You can use the "getCreatureID" ;) )
     
  5. Offline

    Father Of Time

    Good afternoon,

    Have you thought about changing everything inside the following function with a switch:

    Code:
    private int getCreatureID(CreatureSpawner cp)
    Seeing as the variable is an Enum you should be able to create a switch that will help optimize that up a bit, something like:

    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
    Code:
        private int getCreatureID(CreatureSpawner cp)
        {
            Integer results = -1;
            CreatureType type = spawner.getCreatureType();
                switch (type)
            {
                  case CreatureType.Cow:
                    returnval = 1;
                    break;
     
                  case CreatureType.Pig:
                    returnval = 2;
                    break;
     
                //etc...
     
                default:
                returnval = -1;
            }
            return results;
        }
    
    That was written freehand with notepad, so it likely has errors; however the concept is there. Just some food for thought. :D
     
  6. Offline

    Neui

    Father Of Time:

    I like it, if its small. But, with the CreatureType.*TYPE* is a good Idea. :)
     
  7. Offline

    Father Of Time

    The actual code block won't be much smaller, in fact it may even be larger; but execution wise it's much more efficient. :D
     
  8. Offline

    Njol

    add
    Code:java
    1. public void onBlockBreaki(BlockBreakEvent event) {
    2. if (!(block.getType() == Material.MOB_SPAWNER))
    3. return;
    4. // your code here
    5. }
    to the beginning of onBlockBreaki, or put everything into an if statement:
    Code:java
    1. public void onBlockBreaki(BlockBreakEvent event) {
    2. if (block.getType() == Material.MOB_SPAWNER) {
    3. // your code here
    4. }
    5. }
    the latter is useful if you want to check oter types of blocks as well, not only mob spawners.
     
  9. Offline

    Neui

    I check one Block, not more.
     
Thread Status:
Not open for further replies.

Share This Page