Solved How to add a lore to your current item in cursor

Discussion in 'Plugin Development' started by RandomHashTags, Oct 3, 2015.

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

    RandomHashTags

    How to add a lore to your item in your cursor, and be able to have multiple lines of lores.

    Code:
    package randomArmorEffects.Books;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class ClickEvent implements Listener {
       
        @SuppressWarnings("deprecation")
        @EventHandler
        private void inventory(InventoryClickEvent event){
           
            if(!(event.getCursor().hasItemMeta())) {
                System.out.println("Current Item does not have ItemMeta");
                return;
            }
           
            if(event.getCursor().getType().name().endsWith("BOOK") && !(event.getCurrentItem().getType().name().endsWith("BOOTS"))) {
                System.out.println("Returned");
                return;
            }
           
            ItemStack air = new ItemStack(Material.AIR);
           
            if(event.getCursor().getType().name().endsWith("BOOK")) {
               
                if(!(event.getCursor().hasItemMeta()) || !(event.getCursor().getItemMeta().hasLore())) {
                    return;
                }
               
                if(event.getCursor().getItemMeta().getDisplayName().contains(ChatColor.DARK_GREEN + "" + ChatColor.BOLD + ChatColor.UNDERLINE + "Springs V")) {
                    event.getInventory().addItem(event.getCurrentItem()); // How do you add a lore to this item?
                    event.setCursor(air);                                                        // Without errors?
                    event.getCursor().clone();
                    event.getWhoClicked().closeInventory();
                    System.out.println("Springs 5 test");
                }
            }
        }
    }
    
     
  2. Offline

    RoboticPlayer

    A) You already check if the item has ItemMeta earlier in your code, get rid of the second statement checking for meta
    B) What if the cursor is empty?
    C) For adding the lore, do something like this
    Code:
    ItemStack cursor = event.getCursor();
    ItemMeta cursormeta = cursor.getItemMeta();
    cursormeta.setLore(Arrays.asList(string 1, string 2, string 3, etc);
    cursor.setItemMeta(cursormeta);
    Edit: Also, package name should be all lowercase
     
  3. Offline

    kampai

    Why do you have (!( instead of just (! :confused:
     
  4. Offline

    RandomHashTags

    @kampai So it's easier to read, for me.

    @henderry2019 I don't want to set the item meta, I want to add a lore to the item.
    And its
    ItemStack cursor = event.getCurrentItem();

    Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 30, 2015
  5. Offline

    RoboticPlayer

    You add lore to an item by setting the item meta...
     
  6. Offline

    RandomHashTags

    @henderry2019 If you set the item meta, you remove all the other item metas...
     
  7. Offline

    kampai

    @RandomHashTags
    You do realize (!( means if it DOES have a lore.
    ( means does ! means not ( means does again.
    (!( means if the player does have something
     
  8. Offline

    RandomHashTags

  9. @kampai
    Where did yóu learn java?
    ! means not, ()'s are just for organising stuff, (!object == anotherObject) isn't even correct syntax, () are added to make it work properly.
     
  10. Offline

    RoboticPlayer

    Then in that case, do this
    Code:
    ItemStack cursor = event.getCursor();
    ItemMeta cursormeta = cursor.getItemMeta();
    cursormeta.setLore(Arrays.asList(cursormeta.getLore(), string 1, string 2, string 3, etc);
    cursor.setItemMeta(cursormeta);
     
  11. Offline

    kampai

    @megamichiel
    Umm ????
    (!( means if the player does have something.
    ! means not that literately what I just said

    Plus if it's just for organizing stuff then (!( won't work for if the player does, it would be if the player doesn't
     
  12. @kampai
    You're confusing :/, I want an example:
    Code:
    !(event.getCursor().getItemMeta().hasLore())
    Do you think this returns true if it has a lore or if it doesn't have a lore?
     
  13. Offline

    RoboticPlayer

  14. Offline

    kampai

    @megamichiel
    I'm guessing you mean (!( instead of !(
    But that would return true if it has a lore.
     
  15. Offline

    RandomHashTags

    @henderry2019 The
    Code:
    ItemStack cursor = event.getCursor();
    ItemMeta cursormeta = cursor.getItemMeta();
    cursormeta.setLore(Arrays.asList(cursormeta.getLore(), string 1, string 2, string 3, etc);
    cursor.setItemMeta(cursormeta);
    Gives the cursormeta.getLore() errors...
    The method setLore(List<String>) in the type ItemMeta is not applicable for the arguments (List<Object>)
     
  16. @RandomHashTags
    Probably because string 1, string 2 isn't correct syntax. For what you want to do, create a new list and store the cursormeta's lore in it, then add new stuff, and finally apply the new lore.


    @henderry2019
    We have a winner!
    @kampai
    No, because only ! makes something opposite, I don't know where you got that (! is negative and (!( is positive, maybe this will help you.
     
  17. Offline

    RandomHashTags

  18. @RandomHashTags
    Code:
    List<String> lore = new ArrayList<String>(cursormeta.getLore()); //cursormeta.getLore() is not modifyable, therefor we're creating a new list and storing the lore in it
    //Add some strings to it (lore.add(String))
    cursormeta.setLore(lore);
    //Set the curor's itemmeta to the meta and then you're good to go
     
  19. Offline

    RandomHashTags

    @megamichiel Sends an error.

    GOT IT TOO WORK!
    Using @megamichiel 's idea, and adding a few things, I got this:
    Code:
    package randomArmorEffects.Books;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class ClickEvent implements Listener {
       
        @SuppressWarnings("deprecation")
        @EventHandler
        private void inventory(InventoryClickEvent event) {
           
            if(!(event.getCursor().hasItemMeta()) || !(event.getCursor().getItemMeta().hasLore())) {
                System.out.println("Current Item does not have ItemMeta");
                return;
            }
           
            if(event.getCursor().getType().name().endsWith("BOOK") && !(event.getCurrentItem().getType().name().endsWith("BOOTS"))) {
                System.out.println("Returned");
                return;
            }
           
            if(!(event.getCursor().getType().name().endsWith("BOOK"))) {
                return;
            }
           
           
           
            ItemStack air = new ItemStack(Material.AIR);
            ItemStack currentItem = new ItemStack(event.getCurrentItem());
            ItemMeta currentItemMeta = currentItem.getItemMeta();       
            List<String> lore = new ArrayList<String>();
            lore.addAll(currentItemMeta.getLore());
            lore.add("TEST");
            currentItemMeta.setLore(lore);
            currentItem.setItemMeta(currentItemMeta);
           
            if(event.getCursor().getType().name().endsWith("BOOK")) {
               
                if(event.getCursor().getItemMeta().getDisplayName().contains(ChatColor.DARK_GREEN + "" + ChatColor.BOLD + ChatColor.UNDERLINE + "Springs V")) {
                    HumanEntity player = event.getWhoClicked();
                    event.getInventory().addItem(currentItem);
                    event.setCursor(air);
                    event.setCurrentItem(air);
                    event.getWhoClicked().closeInventory();
                    ((Player) player).updateInventory();
                    System.out.println("Springs 5 test");
                }
            }
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 30, 2015
Thread Status:
Not open for further replies.

Share This Page