Changing Lore WITHOUT giving new item

Discussion in 'Plugin Development' started by pingpong1999, Apr 16, 2016.

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

    pingpong1999

    Hey guys, I know I posted an hour and a bit ago but I'm only a beginner!
    Code:
    else if(args[0].equalsIgnoreCase("Enchant") && sender instanceof Player){
                    if(args[1].equalsIgnoreCase("Poison")){
                        String myDisplayName = ("Poison Sword");
                        List<String> lores = new ArrayList<String>();
                        if(Integer.parseInt(args[2]) <= 5){
                            if(args[2].equals("1")){
                                lores.add(ChatColor.GRAY + "Poison I");
                            }else if(args[2].equals("2")){
                                lores.add(ChatColor.GRAY + "Poison II");
                            }else if(args[2].equals("3")){
                                lores.add(ChatColor.GRAY + "Poison III");
                            }else if(args[2].equals("4")){
                                lores.add(ChatColor.GRAY + "Poison IV");
                            }else if(args[2].equals("5")){
                                lores.add(ChatColor.GRAY + "Poison V");
                            }
                            ItemStack myItem = new ItemStack(Material.DIAMOND_SWORD);
                            ItemMeta im = myItem.getItemMeta(); 
                            im.setLore(lores); 
                            im.setDisplayName(myDisplayName);
                            myItem.setItemMeta(im);
                            player.getInventory().addItem(myItem);
                        }
    
    How can I implement this so It does player.getItemInHand().equals(Material.DIAMOND_SWORD); and adds a new Line of lore opposed to giving a new item?
     
  2. Offline

    Zombie_Striker

    @pingpong1999
    1. getItemInHand returns an itemstack. Itemstacks are not the same as materials, so you should use "If getItemInHand().getType() == Material.XYZ"
    2. Are you sure the player will send three args? Your current code does not show that check
    3. You do not need to brackets around the "myDisplayName" string.
    4. Instead of parsing the int, and then never treating the args[2] as an integer ever again, turn arg[2] into a int and test if that int is equal to 1,2,3,ect.
    5. Have you debugged? What is your problem?
     
  3. Offline

    pingpong1999

    Ok, I have done all these things. But I still dont know how to make it so I can change the lore of an item? What I mean by that is I wan the player to be holding an item and they could do /Enchant add Poison (or something along the lines of that) and it would add the lore 'Poison' to the item while still keeping the other lores, enchants and name.
     
  4. Offline

    Zombie_Striker

    In that case, instead of always creating a new, empty lore each time, try the following
    Code:
    List< >lore = ItemMeta.getLore()
     
  5. Offline

    pingpong1999

    How can I add this to my code so I can change an item in my hand?
     
  6. Offline

    Zombie_Striker

    @pingpong1999
    Well, you can use Player#getItemInHand() or Player#getItemInMainHand if you're on 1.9, and set that itemstack's item meta. After you do that though, you will need to call he Player#updateInventory() method in order for you to see the change.
     
  7. Offline

    pingpong1999

    I tried
    Code:
    else if(args[0].equalsIgnoreCase("add") && sender instanceof Player){
                if(args[1].equalsIgnoreCase("Enchant")){
                    if(args.length == 4){
                    if(player.getItemInHand().equals(Material.DIAMOND_SWORD)){
                       ItemStack item = player.getItemInHand();
                       player.sendMessage("test");
                       String name = "Diamond Sword";
                       ItemMeta itemMeta = item.getItemMeta();
                       itemMeta.setDisplayName(name);
                       List<String> lore = itemMeta.getLore();
                       if(args[2].equalsIgnoreCase("poison")){
                          if(args[3].equals("1")){
                              lore.add(ChatColor.GRAY + "Poison I");
                          }else if(args[3].equals("2")){
                              lore.add(ChatColor.GRAY + "Poison II");
                          }else if(args[3].equals("3")){
                              lore.add(ChatColor.GRAY + "Poison III");
                          }else if(args[3].equals("4")){
                              lore.add(ChatColor.GRAY + "Poison IV");
                          }else if(args[3].equals("5")){
                              lore.add(ChatColor.GRAY + "Poison V");
                          }
                       }
                       item.setItemMeta(itemMeta);
                        player.updateInventory();
                    }else{
                        player.sendMessage("You must be holding a sword!");
                    }
                   
                   
                    }
                }
            }
    
    but the player.update Inventory crosses itself out and says "This element neither has attached source nor attached javadoc hence no Javadoc could be found.. I have updated my Javadoc location and it still says this (and please correct any errors I made in this code) It also return "You must be holding a sword!" even if I am holding a sword...
     
    Last edited: Apr 17, 2016
  8. Offline

    mcdorli

    1.: Use switches instead of long else ifs
    2.: Lore isn't always there, you need t make a new list of string
    3.: You never actually set the lore to the itemMeta
     
Thread Status:
Not open for further replies.

Share This Page