Solved Getting mob name?

Discussion in 'Plugin Development' started by shohouku, May 7, 2014.

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

    shohouku

    I'm trying to get an entities name but it's not working?'
    I get no errors, I have registered all my events and I have listeners.

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.NORMAL)
    3. public void onCreatureSpawn(final CreatureSpawnEvent e) {
    4. String name = e.getEntity().getCustomName();
    5.  
    6. EntityEquipment wanderer = e.getEntity().getEquipment();
    7. boolean hasCustomName = name != null;
    8.  
    9. if (e.getEntity() instanceof LivingEntity)
    10. {
    11.  
    12. if(hasCustomName) {
    13. if (name.equalsIgnoreCase("LOL")) {
    14. @SuppressWarnings("deprecation")
    15. ItemStack skull = new ItemStack(397, 1, (short) 3);
    16. SkullMeta meta = (SkullMeta) skull.getItemMeta();
    17. meta.setOwner("Pro");
    18. skull.setItemMeta(meta);
    19. wanderer.setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE, 1));
    20. wanderer.setItemInHand(new ItemStack(Material.WOOD_SWORD));
    21.  
    22. }
    23. }
    24. }
    25. }
     
  2. Offline

    Schaakmatth

    Error log?
     
  3. shohouku Put in some debug code to see what parts are executed.
     
  4. Offline

    Schaakmatth

    Plugin.yml?
     
  5. Offline

    shohouku


    I think it's just the string that isn't getting executed.

    What else method though?..
     
  6. shohouku Erm. Run this and tell me which numbers print.

    Code:
    @EventHandler(priority = EventPriority.NORMAL)
    public void onCreatureSpawn(final CreatureSpawnEvent e) {
    System.out.println(1);
    String name = e.getEntity().getCustomName();
     
    EntityEquipment wanderer = e.getEntity().getEquipment();
    boolean hasCustomName = name != null;
     
    if (e.getEntity() instanceof LivingEntity)
    {
    System.out.println(2);
    if(hasCustomName) {
    System.out.println(3);
    if (name.equalsIgnoreCase("LOL")) {
    System.out.println(4);
    @SuppressWarnings("deprecation")
    ItemStack skull = new ItemStack(397, 1, (short) 3);
    SkullMeta meta = (SkullMeta) skull.getItemMeta();
    meta.setOwner("Pro");
    skull.setItemMeta(meta);
    wanderer.setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE, 1));
    wanderer.setItemInHand(new ItemStack(Material.WOOD_SWORD));
     
    }
    }
    }
    }
     
  7. Offline

    shohouku

    System.out.println(2);

    Is the only number that prints out which checks if name is not null.
     
  8. shohouku I assume 1 prints as well? If nothing higher than 2 prints, then your entity does not have a custom name.
     
  9. Offline

    shohouku



    I'm setting this in a scheduler in the OnEnable function:

    Code:java
    1. Skeleton pig = (Skeleton)Bukkit.getWorld("World").spawnEntity(spawnLocation, EntityType.SKELETON);
    2. pig.setCustomName("LOL");
    3. pig.setCustomNameVisible(true);
    4. pig.setMaxHealth(100);
    5. pig.setHealth(((Damageable)pig).getMaxHealth());

    So.. I'm assuming it should have a custom name...
     
  10. shohouku Why is your Skeleton called pig? And Skeleton inherits from Damageable, so there's no real reason to cast. Also, try printing out the custom name, see what happens.
     
  11. Offline

    shohouku


    I was planning change pig to skeleton anyways,

    Yes it does set the custom name to "LOL", because it system printed it out on my server console.

    Code:java
    1. Skeleton pig = (Skeleton)Bukkit.getWorld("World").spawnEntity(spawnLocation, EntityType.SKELETON);
    2. System.out.println(5);
    3. pig.setCustomName("LOL");
     
  12. shohouku Try this, and tell me everything that prints

    PHP:
    @EventHandler(priority EventPriority.NORMAL)
    public 
    void onCreatureSpawn(final CreatureSpawnEvent e) {
    System.out.println(1);
    String name e.getEntity().getCustomName();
     
    EntityEquipment wanderer e.getEntity().getEquipment();
    boolean hasCustomName name != null;
     
    if (
    e.getEntity() instanceof LivingEntity)
    {
    System.out.println(2);
    System.out.println(name);
    System.out.println(hasCustomName);
    if(
    hasCustomName) {
    System.out.println(3);
    if (
    name.equalsIgnoreCase("LOL")) {
    System.out.println(4);
    @
    SuppressWarnings("deprecation")
    ItemStack skull = new ItemStack(3971, (short3);
    SkullMeta meta = (SkullMetaskull.getItemMeta();
    meta.setOwner("Pro");
    skull.setItemMeta(meta);
    wanderer.setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE1));
    wanderer.setItemInHand(new ItemStack(Material.WOOD_SWORD));
     
    }
    }
    }
    }
     
  13. Offline

    shohouku

    System.out.println(1);
    System.out.println(2);
    System.out.println(hasCustomName);

    And idk what "False" was but it printed in my console, anyways these are the following system print outs that showed.
     
  14. shohouku Your hasCustomName is false - it doesn't have a custom name apparently. And the name should have been printed too, above the false. Did it say null?
     
  15. Offline

    shohouku


    Yeah it did, sorry for not indicating it above.
     
  16. shohouku In that case the entity definitely doesn't have a name. If you're sure that this is happening when you spawn your custom skeleton, I would suggest delaying the listener for a tick - it might be that you're not giving it enough time to set the name
     
  17. Offline

    shohouku


    Hmmh this is my code when I try to spawn it with a custom name:

    Code:java
    1. for(Player p : Bukkit.getOnlinePlayers()){
    2. final World world = Bukkit.getServer().getWorld("world");
    3. final Location deathloc = new Location(world, 266, 63, 586);// how ever you are loading your location from your config
    4. final org.bukkit.Chunk bChunk = p.getWorld().getChunkAt(deathloc);
    5.  
    6. int min = 0;
    7. int max = 15;
    8. Random rand = new Random ();
    9. int r = rand.nextInt (max - min) + min;
    10.  
    11. int lx = bChunk.getX()*16+r;
    12. int lz = bChunk.getZ()*16+r;
    13. final Location spawnLocation = new Location(bChunk.getWorld(), lx, bChunk.getWorld().getHighestBlockAt(lx,lz).getLocation().getY(), lz);
    14.  
    15.  
    16. Location playerloc = p.getLocation();
    17.  
    18. if( playerloc.distance( deathloc ) < 10 )
    19. {
    20. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    21. public void run() {
    22.  
    23.  
    24.  
    25.  
    26. for (int i = 0; i <= 4; i ++) {
    27.  
    28. Skeleton pig = (Skeleton)Bukkit.getWorld("World").spawnEntity(spawnLocation, EntityType.SKELETON);
    29. System.out.println(5);
    30. pig.setCustomName("LOL");
    31. pig.setCustomNameVisible(true);
    32. pig.setMaxHealth(100);
    33. pig.setHealth(((Damageable)pig).getMaxHealth());
    34.  
    35.  
    36. }
    37.  
    38.  
    39. }
    40.  
    41. },0L,200L);
    42. }
    43. }
    44. }




    Scheduler:

    Code:java
    1. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3.  
    4.  
    5.  
    6.  
    7. for (int i = 0; i <= 4; i ++) {
    8.  
    9. Skeleton pig = (Skeleton)Bukkit.getWorld("World").spawnEntity(spawnLocation, EntityType.SKELETON);
    10. System.out.println(5);
    11. pig.setCustomName("LOL");
    12. pig.setCustomNameVisible(true);
    13. pig.setMaxHealth(100);
    14. pig.setHealth(((Damageable)pig).getMaxHealth());
    15.  
    16.  
    17. }
    18.  
    19.  
    20. }
    21.  
    22. },0L,200L);
     
  18. shohouku Yeah, put a delay on the listener as well.
     
  19. Offline

    shohouku


    How would I put a delay on the listener?

    Had never put a delay on a listener before....
     
  20. shohouku By that I just mean something like this

    PHP:
    @EventHandler(priority EventPriority.NORMAL)
        public 
    void onCreatureSpawn(final CreatureSpawnEvent e) {
            
    Bukkit.getScheduler().runTask(plugin, new Runnable() {
                public 
    void run() {
                    
    System.out.println(1);
                    
    String name e.getEntity().getCustomName();
     
                    
    EntityEquipment wanderer e.getEntity().getEquipment();
                    
    boolean hasCustomName name != null;
     
                    if (
    e.getEntity() instanceof LivingEntity) {
                        
    System.out.println(2);
                        
    System.out.println(name);
                        
    System.out.println(hasCustomName);
                        if (
    hasCustomName) {
                            
    System.out.println(3);
                            if (
    name.equalsIgnoreCase("LOL")) {
                                
    System.out.println(4);
                                @
    SuppressWarnings("deprecation")
                                
    ItemStack skull = new ItemStack(3971, (short3);
                                
    SkullMeta meta = (SkullMetaskull.getItemMeta();
                                
    meta.setOwner("Pro");
                                
    skull.setItemMeta(meta);
                                
    wanderer.setChestplate(new ItemStack(
                                        
    Material.LEATHER_CHESTPLATE1));
                                
    wanderer.setItemInHand(new ItemStack(
                                        
    Material.WOOD_SWORD));
     
                            }
                        }
                    }
                }
            });
        }
     
    shohouku likes this.
  21. Offline

    shohouku



    Thank you for your help!

    I'm marking this thread as SOLVED! :)
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page