Solved lore problem

Discussion in 'Plugin Development' started by Ezrab_, Feb 15, 2017.

  1. Offline

    Ezrab_

    So in a class called: 'ItemStacks' I use this
    Code:
        public static ItemStack createItemStack(Material m, int amount, String displayname, byte data, List<String> lore) {
    
    And then in another class I want to use the method so for example I am in a class called "PlayerJoin" I use this
    Code:
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            e.getPlayer().getInventory().addItem(ItemStacks.createItemStack(m, amount, displayname, data, lore));
    Can anyone tell me what I should do with lore?
    As you can tell I'm not the best Bukkit Developer but I'm trying to learn it by experimenting
    Also sorry for my bad English, I hope that I'm clear enough



    I have figured it out I'm using this:


    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Action a = e.getAction();
            ItemStack is = e.getPlayer().getItemInHand();
    
            if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
                return;
    
            if ((e.getPlayer().getItemInHand() != null) && (e.getPlayer().getItemInHand().hasItemMeta()) && (e.getPlayer().getItemInHand().getItemMeta().hasDisplayName())) {
                String ItemName = is.getItemMeta().getDisplayName();
                if ((ItemName.contains(ChatColor.RED + "Kit Selector"))) {
                    Menu(e.getPlayer());
                }
        }
        }
     
    Last edited: Feb 17, 2017
  2. Online

    timtower Ninja on the waves Moderator

    @Ezrab_ What do you want to do with lore?
     
  3. Offline

    Ezrab_

    I want it so that a item has a description and I read that that's the lore.
     
  4. Online

    timtower Ninja on the waves Moderator

    @Ezrab_ Then input a lore into the method.
     
  5. Offline

    Ezrab_

    I don't understand what you mean...
     
  6. Online

    timtower Ninja on the waves Moderator

    @Ezrab_ You have a method that can make a new ItemStack for you, has all the arguments that you need.
    Why not use that method?
    Don't see your problem to be honest.
     
  7. Offline

    Ezrab_

    Okay uhm...
    Code:
            e.getPlayer().getInventory().addItem(ItemStacks.createItemStack(Material.DIAMOND_SWORD, 1, "", (byte) 0, lore));
    
    So my problem is, is that I don't know what to put at lore...

    Code:
            e.getPlayer().getInventory().addItem(ItemStacks.createItemStack(Material.DIAMOND_SWORD, 1, "", (byte) 0, lore /*<-- here*/));
    
    Because if I do
    Code:
            e.getPlayer().getInventory().addItem(ItemStacks.createItemStack(Material.DIAMOND_SWORD, 1, "", (byte) 0, ""));
    
    It will say that it's not a String.
     
  8. Online

    timtower Ninja on the waves Moderator

  9. Offline

    Ezrab_

    That's the problem I want to use the lore here take a look:

    Code:
    public static ItemStack createItemStack(Material m, int amount, String displayname, byte data, List<String> lore) {
            ItemStack is = new ItemStack(m, amount, data);
            ItemMeta im = is.getItemMeta();
            im.setDisplayName(displayname);
            im.setLore(lore);
            is.setItemMeta(im);
            return is;
        }

    I hope you understand what I mean now...
     
  10. Online

    timtower Ninja on the waves Moderator

    @Ezrab_ Then you input a list.
    You can make that on a different line of code just fine. Before the createItemStack call in the event.
     
  11. Offline

    MaxFireIce

    @Ezrab_
    Code:java
    1. Arrays.asList("This is my lore", "and I can add as many", "strings as I want");

    Put that in for lore parameter and change string stuff to what you want. That should work.
     
  12. Offline

    PhantomUnicorns

    You could edit the method to accept String... instead of a list, so you can put multiple lines of lore. Just create a List<String> inside the method, and run through the String... and put them all in there, then set the lore.

    NOTE: String... is the variable name
     
  13. Offline

    Ezrab_

    Thanks that did the job!

    I have another question why doesn't this work?
    Code:
    if (is.getType() == Material.FEATHER && (is.getItemMeta().equals(ChatColor.RED + "Kit Selector")))
    So I'm making an inventory that you can only open with a feather that has this ^ name. But it doesn't work if I use .equals(ChatColor.RED + "Kit Selector") Anyone knows what else I could do?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  14. Offline

    PhantomUnicorns

    @Ezrab_ Are you gettings errors?
    Also is.getItemMeta() returns a itemmeta, not a string! You would do is.getItemMeta().getDisplayName() or getting the lore, I recommend that you learn how ItemMeta works and it's functions. If you want me to explain it just tell me.
     
  15. Offline

    MaxFireIce

    @Ezrab_ ItemMeta is an Object that has functions like set displayName(), setLore(), setEnchants(), etc. You then have to set that ItemMeta to an ItemStack, or you can compare it to other ItemMetas.
     
  16. Offline

    Ezrab_

    Okay so I did this

    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Action a = e.getAction();
            ItemStack is = e.getItem();
           
    
            if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
                return;
    
            if (is.getType() == Material.FEATHER && is.getItemMeta().getDisplayName().contains(ChatColor.RED + "Kit Selector")) {
                Menu(e.getPlayer());
        } else {
            return;
        }
    }
    It does work now, but if you just use a normal feather it gives a NullPointerException and I dont know why that is
     
    Last edited: Feb 17, 2017
  17. Online

    timtower Ninja on the waves Moderator

    @Ezrab_ Check if there is itemMeta, check if there is a displayName
     
  18. Offline

    Ezrab_

    Could you give write it in code for me?
     
  19. Online

    timtower Ninja on the waves Moderator

  20. Offline

    Ezrab_

  21. Online

    timtower Ninja on the waves Moderator

    I linked you the methods that you need to make the checks.
    Should be able to figure out how they work by looking at the description.
     
  22. Offline

    Ezrab_

    No, and just giving me the site where all methods are listed isn't helping me out.
     
  23. Online

    timtower Ninja on the waves Moderator

    Then look at the links, last word in the link is the method.
    Look at the method name, look at what it returns.
    You have a method to check if there is item meta, and a method to check if there is a display name.
     
  24. Offline

    PhantomUnicorns

    Ok here:
    Code:
    ITEM.hasItemMeta(); // Returns a boolean seeing if it HAS ItemMeta
    ITEM.getItemMeta(); // Gets ItemMeta, if this ^ is false returns null
    
    ITEM.getItemMeta().hasDisplayName(); // Returns a boolean seeing if it has a display name in ItemMeta
    ITEM.getItemMeta().getDisplayName(); // Gets DisplayName, if this ^ is false returns material name
    
    ITEM.getItemMeta().hasLore(); // Returns a boolean seeing if it has a lore in ItemMeta
    ITEm.getItemMeta().getLore(); // Gets the Lore in a List<String>, if this ^ is false probably returns null or zero length List
    
    A layout of the basic methods of the ItemMeta class.
     
  25. Offline

    TheEnderman

    Null check ItemMeta#getDisplayName()
     
  26. Offline

    PhantomUnicorns

    @TheEnderman The getDisplayName() Will not return null (I think) plus you check it with hasDisplayName()... If it returns false, then just get the Material then get the name.
     
    ipodtouch0218 likes this.

Share This Page