Solved Updating lore

Discussion in 'Spigot Plugin Development' started by Havooc, Jan 15, 2020.

  1. Hello, Im trying to make plugin that will register and show how many stone was mined using specifig pickaxe (in lore). I did like 95% of this plugin, but now i have problem with updating lore of the item.

    Command that replaces normal pickaxe with a new one
    Code (Java):

    (...)
    p.sendMessage(chat("&aAdded "+prefix+" for this item!"));
    ItemMeta meta = p.getInventory().getItemInMainHand().getItemMeta();
    meta.setDisplayName(chat(prefix+" &citem name"));
    p.getInventory().getItemInMainHand().setItemMeta(meta);
    new STPickaxe().updateItem(p,0,0,0,0,0,0,0,0,0);
    (...)
     
    STPickaxe class
    Code (Java):

    public void updateItem(Player p, int diamond, int emerald, int gold, int iron, int redstone, int lapis, int coal, int quartz, int stone){
        ItemStack STPickaxe = p.getItemInHand();
        ItemMeta meta = STPickaxe.getItemMeta();
        if(STPickaxe.getType() == Material.DIAMOND_PICKAXE){
            if(STPickaxe.getItemMeta().getDisplayName().contains(chat(prefix))){
                meta.setLore(Arrays.asList(chat(" "),
                        chat("&6Stone: &7"+stone),
                        chat("&6Coal: &7"+coal),
                        chat("&6Iron: &7"+iron),
                        chat("&6Gold: &7"+gold),
                        chat("&6Lapis: &7"+lapis),
                        chat("&6Redstone: &7"+redstone),
                        chat("&6Diamonds: &7"+diamond),
                        chat("&6Emeralds: &7"+emerald),
                        chat("&6Quartz: &7"+quartz),
                        chat("&7Owner: "+p.getDisplayName())));
                p.getInventory().getItemInMainHand().setItemMeta(meta);
            }
        }
    }
     
    And this is my drop class, where I want to add this thing that if a player broke an block, it will update lore.
    Code (Java):

    if(b.getType() == Material.STONE){
        boolean ST = p.getInventory().getItemInMainHand().getItemMeta().getDisplayName().contains(chat(prefix));
        if (p.getItemInHand().getType() == Material.DIAMOND_PICKAXE){
        if(ST){
            p.sendMessage("testing1...");
            <UPDATE LORE HERE>
        }
    }
    }
     
     
  2. Just parse every line and edit it.
    I would have went a different route but i think you don't want to do packet stuff.

    Here is an example i wrote really quick. It can be used to parse for a specific line and increments the number behind a identifier.

    Code (Java):
      public void incrementLines(String identifier, List<String> lore) {
        // For every line in lore
        for (int index = 0; index < lore.size(); index++) {
          String line = lore.get(index);
          // Checks if line starts with identifier (Coal for example)
          if (stripColor(line).startsWith(identifier)) {
            // 1)Strip color 2)replace all but 0-9 3)parse to int
            int value = Integer.parseInt(extractNumber(stripColor(line)));
            // set a new lore line that replaces the old one
            lore.set(index, "§6" + identifier + ": §7" + ++value);
          }
        }
      }
      // Replaces everything but 0-9
      private String extractNumber(String input) {
        return input.replaceAll("[^0-9]", "");
      }
      // Strips color codes
      private String stripColor(String input) {
        return ChatColor.stripColor(input);
      }

    use:
    Code (Java):
      public void incrementLines(ItemStack item, String identifier) {
        ItemMeta meta = item.getItemMeta();
        if (!meta.hasLore()) {
          return;
        }
        List<String> lore = meta.getLore();
        incrementLines(identifier, lore);
        meta.setLore(lore);
        item.setItemMeta(meta);
      }
     
    • Winner Winner x 1
  3. Thank you so much!