Anyone know why this won't work?

Discussion in 'Plugin Development' started by DoggyCode™, Jul 5, 2015.

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

    DoggyCode™

    Can anyone tell me why this won't work?

    Code:
      public static ItemStack createItem(Material material, int amount, short shrt,
           String displayname, String lore) {
          ItemStack item = new ItemStack(material, amount, (short) shrt);
          ItemMeta meta = item.getItemMeta();
          meta.setDisplayName(displayname);
          ArrayList<String> Lore = new ArrayList<String>();
          Lore.add(lore);
          meta.setLore(Lore);
           
          item.setItemMeta(meta);
          return item;
       
      }
    
    That's my skeleton (not the actual mob ingame). Now is the thing that won't work:

    Code:
      inv.setItem(1, Main.createItem(Material.COOKED_BEEF, (int)p.getFoodLevel(), (short)0, "§6Hunger", "§e" + p.getFoodLevel() + ".0 §8/ §e20.0"));
    As you might can see, I want the amount of the cooked beef to be the player's health (out of 20), and same with the lore.. It works with "(int)p.getFoodLevel()" as amount, and just "p.getFoodLevel()" as lore. But it just doesn't work with health, does anyone know what I'm doing wrong... because as health, it just keep displaying 20, even though the player might be as low as 5.

    Screenshot_1.png
     
  2. 1. Why are you casting a short to a short? That's unnecessary.

    2. ItemMeta can be null, you should do a null check.

    3. Code to the interface, not the implementation. So it should be: List<String> lore = new ArrayList<>();
    I also changed 'Lore' to 'lore', follow Java programming conventions please (camelCasing).

    4. You do realise you're setting the item in slot 1 to the food level, not the health? It should be:
    Code:
    inv.setItem(0, Main.createItem(Material.COOKED_BEEF, (int) p.getHealth(), (short) 0, "§6Health", "§e"+ p.getHealth() +" §8/ §e" + p.getMaxHealth()));
     
    Last edited: Jul 6, 2015
  3. Offline

    DoggyCode™

    No, I'm putting the health thing in slot 0, which is the first slot in a inventory... Btw, with the "p.getHealth()" gives me error saying "The method getHealth() is ambiguous for the type Player".

    Here's my code:
    Code:
      inv.setItem(0, Main.createItem(Material.getMaterial(382), (int)p.getHealthScale(), (short)0, "§6Health", "§e" + p.getHealthScale() + " §8/ §e20.0"));
      inv.setItem(1, Main.createItem(Material.COOKED_BEEF, (int)p.getFoodLevel(), (short)0, "§6Hunger", "§e" + p.getFoodLevel() + ".0 §8/ §e20.0"));
      inv.setItem(2, Main.createItem(Material.ROTTEN_FLESH, (int)p.getSaturation(), (short)0, "§6Saturation", "§e" + p.getSaturation()));
      inv.setItem(3, RidingFalse);
      inv.setItem(4, Main.createItem(Material.EXP_BOTTLE, 1, (short)0, "§6Experience", "§7Exp§8: §e" + p.getExpToLevel()));
      inv.setItem(5, loc);
      inv.setItem(6, Main.createItem(Material.WOOD_PICKAXE, 1, (short)0, "§6Game Mode", "§e" + p.getGameMode()));
      inv.setItem(7, Ip);
    
     
  4. Offline

    Tecno_Wizard

    @DoggyCode™, I have no idea how many times I will need to answer this in my time on this site...
    STOP COMPILING ON CRAFTBUKKIT. It will create the ambiguous method error.
     
  5. Oh sorry, I misread your post. It now should be for the first slot (0):
    Code:
    inv.setItem(0, Main.createItem(Material.getMaterial(382), (int) p.getHealth(), (short) 0, "§6Health", "§e"+ p.getHealth() +" §8/ §e" + p.getMaxHealth()));
    
    Keep your food level one to what it is/was before, don't do what I said before (for the food level one).

    For the ambiguous thing, do you have both Bukkit and CraftBukkit in your library/dependencies? If so, remove one of them (the one you don't need, i.e. if using NMS or Craft<Class>, keep CraftBukkit, if not, remove CraftBukkit). If you only have CraftBukkit, use Bukkit instead (unless you use NMS or Craft<Class>, in which case, I don't know what to do). I believe 1.8 Spigot fixed this, or IntelliJ doesn't show this error, but I'm not sure.
     
  6. Offline

    DoggyCode™

    I only have Craftbukkit..

    Huh? So the error means nothing?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  7. Offline

    Tecno_Wizard

    @DoggyCode™, it means that the error is caused by the jar compiling incorrectly because craftbukkit looks for jars compiled on bukkit, not craftbukkit.
     
  8. Offline

    DoggyCode™

    And how exactly to I fix this?
     
  9. Offline

    Tecno_Wizard

    @DoggyCode™, compile on bukkit. Unfortunately, spigot does not release the Bukkit jar for 1.8, which makes it really hard to get.
     
  10. They do, located in: BuildDirectory/Bukkit/target/

    Anyway, this is assuming he is even using 1.8.
     
  11. Offline

    Tecno_Wizard

    @KingFaris11, okay, i wish i knew that months ago. I've been building off Bukkit 1.7.9

    EDIT: I can only find the RSS feed. No BuildDirectory
     
    Last edited: Jul 6, 2015
  12. By BuildDirectory, I mean the directory in which BuildTools.jar is in, not from the official Maven repo.

    http://gyazo.com/561bb0df92033129378c4fd9c71f547b
    This is how your directory should look like, excluding the "Build.bat" file I made.
     
  13. Offline

    Tecno_Wizard

    @KingFaris11, well that way dumb on my part. thx. I haven't run buildtools in ages. Nothing has changed that required me to do it.
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page