Solved Strange error with lore of an item

Discussion in 'Spigot Plugin Development' started by Soolz, Jul 9, 2018.

  1. Hello, I'm creating a plugin that would make a durability with the description of an item, it works properly as the number on the right (if 3/9 is 9) is less than 10

    Sorry for my bad english


    Code (Java):
    @EventHandler
            public void DurabilityLore(PlayerItemDamageEvent e){
                Player p = e.getPlayer();
                if(e.getItem() != null && e.getItem().hasItemMeta() && e.getItem().getItemMeta().hasLore() && e.getItem().getItemMeta().getLore().contains("test")){
                    if(e.getItem().getItemMeta().getLore().get(0).contains("test)){
                        ItemMeta itm = e.getItem().getItemMeta();
                        String lore = e.getItem().getItemMeta().getLore().get(1); // Pretend this is the lore you just got.
                        String lastWord = lore.substring(lore.lastIndexOf("
    "));
                        String Durability = lastWord.substring(lastWord.lastIndexOf("
    /"));
                        //String lastWord2 = lastWord.substring(lastWord.indexOf("
    /"));
                        String lastWord2 = lastWord.substring(0, lastWord.length() - 1);
                       
                        Durability = Durability.substring(1);
                        lastWord2 = lastWord2.substring(1);
                       
                        lastWord2 = lastWord2.substring(0, lastWord2.length() - 1);
                       
                       
                        int result = Integer.parseInt(lastWord2);
                        int result2 = Integer.parseInt(Durability);
                       
                        result = result - 1;
                       
                        itm.setLore(Arrays.asList("
    §1", "Durabilité: " + result + "/" + result2));
                       
                        e.getItem().setItemMeta(itm);
                       
                        if(result == 0){
                            p.sendMessage("
    0");
                        }
                       
                       
                            e.setCancelled(true);
                       
                   
                   
                    }
                }
            }
     
  2. Durability starts from 0, the higher the amount of the durability is the higher the damage the item was (can be really confusing).

    I would so something like:
    Code (Text):
    int maxdura = item#getMaxDurability();
    int currdura = meta#getDurability();
    String line = "Durability: " + maxdura + "/" + currdura;
    // Not tested.
    If that does not work try to switch the maxdura and currdura.
     
  3. Thank you, but I create different durabilities, and the lore, I add it
     
  4. Different durabilities? But this can be used for every item.
     
    • Like Like x 1
  5. I do that when an item is damaged, the event is canceled and the durability lore is subtracted by one, example:
    "Durabilité: 3/3" change to "Durabilité: 2/3"
    But if the maximum durability lore is greater than 9 then everything goes as if I did not do anything, but if I put this: "Durabilité: 100/9" to the description of an item, it works very well.
     
  6. I stared at this for a while but honestly had a really hard time seeing what was wrong. I think this would be a lot easier/clearer if you use StringUtils.split from Apache commons (provided by Spigot)

    So basically like
    Code (Text):
    String[] pieces = StringUtils.split(lastWord, '/');
    int durability = Integer.parseInt(pieces[0]);
    int maxDurability = Integer.parseInt(pieces[1]);
    StringUtils.split gives you an array of the two strings on either side of "durability/max".

    You could also use it with a ' ' to get "lastWord" but if that part's working fine for you with substring you can just leave it alone, too.
     
  7. This will hopefully be useful to you.

    Code (Java):

    String randomDur = "Durability: 5/100";

    /*
    This will return the current durability of our item.
    Example:
    int dur = getDurLevel(randomDur); // returns 5
    */

    int getDurLevel(String dur) {
    String diff = dur.split(" ")[1]; // get 5/100
    return Integer.valueOf(diff.split("/")[0]); // get 5
    }

    /*
    This will hopefully decrease the dur by X and return the full string
    */

    String decreaseDur(String dur, int decreaseAmount) {

    int dur = getDurLevel(dur) - decreaseAmount < 0 ? 0 : getDurLevel(dur) - decreaseAmount; // Get the current level - the decrease.

      return "Durability: " + dur + "/100"; // or you can do -> return dur.replace(getDurLevel(dur), dur);

        }
    }


     
     
    • Winner Winner x 1
  8. Thank you very much!