Getting a ID of a SpawnEgg? 383:??

Discussion in 'Plugin Development' started by Flash619, May 10, 2012.

  1. Offline


    Hello everyone!

    I'm trying to have a event take place when a player uses a spawn egg. For specified purpose, I need to know the specific meta ID of it. An example of this would be if a player used a pig egg it would send me 383:90.

    So far, I can definitely get it to sell me "MONSTER_EGG", or "383" or, some huge hash code xD

    But I'm not sure on the specific string of methods to get the actual meta code. I'm currently trying things like:
    Integer VariableName = player.getItemInHand().*rest of the stuff here*

    Anyone have any ideas? I'm open for anything! :)
  2. First check whether the player has a spawn egg in his hands with player.getItemInHand().getType() == Material.MONSTER_EGG, then check whether it's a pig spawn egg with player.getItemInHand().getDurability() == EntityType.PIG.getTypeId():
    Code (Text):
    1. if (player.getItemInHand().getType() == Material.MONSTER_EGG && player.getItemInHand().getDurability() == EntityType.PIG.getTypeId()) {
    2.     // your code here
    3. }
  3. Offline


    Yes but I'm using a separate method to check through an array that holds several spawn egg types. If I did use that, I would have to use a separate statement for every type. I could loop it, but then I couldn't use item ID's because I would have to link them to strings. I do thank you for your help as it does provide a usable option, but is there no way to get a 383:91 item id?
  4. You can use player.getItemInHand().getTypeId() == 383 && player.getItemInHand().getDurability() == 91, but It's not easy to see what kind of item this is from the code.
    Using an array of spawn eggs can be done like this:
    1. short[] spawnEggs = ...;// data values of the eggs
    3. onInteract(...) {// or whatever event you use
    4. if (player.getItemInHand().getType() == Material.MONSTER_EGG) {
    5. for (short egg:spawnEggs) {
    6. if (player.getItemInHand().getDurability() == egg) {
    7. // some code
    8. break;
    9. }
    10. }
    11. }
    12. }

    If you want more help you should specify exactly what you want to do.
  5. Offline


    Hmnn but the array I have looks like:
    2. public static Integer[] Mounts = new Integer[]{
    3. //Enderdragon will no longer be listed here but
    4. //instead as a seperate config entity.
    5. 95,//Wo14lf
    6. 52,//Spider
    7. 90,//Pig
    8. 91,//Sheep
    9. 92 //Cow
    10. };

    Then everything in the plugin just refers to that for reference on the available eggs.

    Would a Integer array work? I mean. You could just replace the number from the id with:

    I would think that may work?
  6. Offline


    For anyone else who has this conundrum, this is what I ended up using:
    2. @EventHandler(priority = EventPriority.HIGHEST)
    3. public void onThrow(PlayerInteractEvent event){
    4. Player player=event.getPlayer();
    5. if (player.getItemInHand().getType() == Material.MONSTER_EGG) {
    6. if(!IgnoreMounts.IsIgnoring(player)){
    7. if(IsValidEgg(player)){
    8. //TODO
    9. }
    10. }
    11. }
    13. }
    14. public boolean IsValidEgg(Player player){
    15. Short ID = player.getItemInHand().getDurability();
    16. Integer IDI = ID.intValue();
    17. System.out.println(IDI);
    18. if(Permissions.hasEggPerm(player, IDI)){
    19. for(Integer i=0;i<Mounts.Mounts.length;i++){
    20. if(IDI==Mounts.Mounts[I]){[/I]
    21. [I] return true;[/I]
    22. [I] }[/I]
    23. [I] }[/I]
    24. [I] return false;[/I]
    25. [I] }else{[/I]
    26. [I] return false;[/I]
    27. [I] }[/I]
    28. [I] }[/I]
    29. [I][/I]

Share This Page