Replace a word with another word in a lore.

Discussion in 'Plugin Development' started by SmallDesk, Feb 7, 2018.

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

    SmallDesk

    So what I am trying to do is, when a player runs a command, to get a certain item, and replace a word in it.

    So for example, if a player has "+111 Strength" on their item, when they run the command it would replace the "Strength" with "Dexterity". So now it's "+111 Dexterity".

    I have attempted this but it keeps giving an IndexOutOfBounds exception.

    Here's my code:

    if(cmd.getLabel().equalsIgnoreCase("fixstats") && sender instanceof Player){
    Player p = (Player) sender;
    if(p.hasPermission("c.demonhunter")){
    ItemStack[] arrayOfItemStack;
    int j = (arrayOfItemStack = p.getEquipment().getArmorContents()).length;
    int s = this.strengthRegex.LITERAL;
    ItemStack target = p.getInventory().getItem(0);
    List<String> lore = target.getItemMeta().getLore();
    // For each line in lore...
    for(int x = 0; x < s; x++) {
    String st = lore.get(s);
    st.replaceAll("Strength", "Dexterity");
    }
    }
     
  2. Offline

    Zombie_Striker

    S is not equal to the lore's length, so you go over the actual amount of lores and recieve the IOOB. Change "s" to be equal to lore.size()
     
  3. Offline

    SmallDesk

    Hey, thanks for replying so quickly, I have changed int s to lore.size();.
    But, it still gives me the IOOB exception.

    My code:

    if(cmd.getLabel().equalsIgnoreCase("fixstats") && sender instanceof Player){
    Player p = (Player) sender;
    if(p.hasPermission("c.demonhunter")){
    ItemStack[] arrayOfItemStack;
    int j = (arrayOfItemStack = p.getEquipment().getArmorContents()).length;
    ItemStack target = p.getInventory().getItem(0);
    List<String> lore = target.getItemMeta().getLore();
    int s = lore.size();
    for(int x = 0; x < s; x++) {
    String st = lore.get(s);
    st.replaceAll("Strength", "Dexterity");
    }
    }
     
  4. Offline

    Zombie_Striker

    @SmallDesk
    Seems I overlooked this. You are getting the lore for S, not X. S is the max size, while X is the individual lines. replace the get with "lore.get(x)".

    Also, replaceAll does not change the string instance; instead, it creates a new string with the replaced word. In order for the lore to change, you will need to set "st = st.replaceAll(...)", and then you will need to reset the line at X to be equal to st.
     
  5. Offline

    SmallDesk

    Hey I have tried what you said, and it does not work, but it does not return any errors/exceptions.

    Code:

    if(cmd.getLabel().equalsIgnoreCase("fixstats") && sender instanceof Player){
    Player p = (Player) sender;
    if(p.hasPermission("c.demonhunter")){
    ItemStack[] arrayOfItemStack;
    int j = (arrayOfItemStack = p.getEquipment().getArmorContents()).length;
    int s = this.strengthRegex.LITERAL;
    ItemStack target = p.getInventory().getItem(0);
    List<String> lore = target.getItemMeta().getLore();
    // For each line in lore...
    for(int x = 0; x < s; x++) {
    String st = lore.get(x);
    String str = st.replaceAll("Strength", "Dexterity");
    lore.set(x, str);
    }
    }

    EDIT: My bad, it still gives an IOOB but it says "Index: 1, Size: 1"
     
  6. Offline

    Zombie_Striker

    @SmallDesk
    For some index (most likely, X), the value is 1 while the array only contains one entry (Size: 1). Check to make sure that "st"/the lore exists and that the lore contains Strength before replacing and setting the string.
     
  7. Offline

    SmallDesk


    Hey, do you know why this code is not replacing the lore?

    if(cmd.getLabel().equalsIgnoreCase("fixstats") && sender instanceof Player){
    Player p = (Player) sender;
    if(p.hasPermission("c.demonhunter")){
    ItemStack[] arrayOfItemStack;
    ItemStack target = p.getInventory().getItem(0);
    List<String> lore = target.getItemMeta().getLore();
    for(int j = 0; j < lore.size(); j++){
    String str = lore.get(j).toLowerCase();
    if(str.endsWith(" strength")){
    str.replace(" strength", "Dexterity");
    p.sendMessage(ChatColor.GREEN + "Your Main Stat was fixed according to your class. "
    + ChatColor.GOLD + "(Demon Hunter)" + ChatColor.GREEN + ".");
    }
    }
    }
     
    Last edited: Feb 7, 2018
  8. Offline

    Lorinthio

    You are probably missing target.getItemMeta().setLore(lore);

    But thats assuming your player has the permission, and the item you're looking at is in slot 0 in the inventory. Might be easier to use player.getItemInMainHand() and apply this logic to that ItemStack
     
  9. Offline

    SmallDesk


    Hey, this is my new code, but it never replaces anything:

    if(cmd.getLabel().equalsIgnoreCase("fixstats") && sender instanceof Player){
    Player p = (Player) sender;
    if(p.hasPermission("c.demonhunter")){
    ItemStack[] arrayOfItemStack;
    ItemStack target = p.getItemInHand();
    List<String> lore = target.getItemMeta().getLore();
    for(int j = 0; j < lore.size(); j++){
    String str = lore.get(j).toLowerCase();
    if(str.endsWith(" strength")){
    str.replaceAll(" strength", "Dexterity");
    target.getItemMeta().setLore(lore);
    p.sendMessage(ChatColor.GREEN + "Your Main Stat was fixed according to your class. "
    + ChatColor.GOLD + "(Demon Hunter)" + ChatColor.GREEN + ".");
    }
    }
    }
    }
     
  10. Offline

    Lorinthio

    Not sure if this is asking too much can you pastebin your class that contains this logic? Might help me see what is wrong.

    Also can I see a picture of the item you are trying to use this on?
     
  11. Offline

    SmallDesk


    Sure! Here is the pastebin:

    https://pastebin.com/K0Z9FL53

    The item is just a regular diamond sword with the lore "strength" on it, I just tried to keep it as simple as possible for this.
     
  12. Offline

    Lorinthio

    So it looks like you're using str.endsWith(" strength").

    Instead I would say if str.contains("strength"). Even then if you are always gonna replace the value with a new value don't even do the check. Simply use str = str.replace("strength", "Dexterity"), and I would ensure your casing matches exactly what is on the item, and also match casing to make your life easier.

    aka
    str = str.replace("Strength", "Dexterity");

    Another follow up you can easily loop over lists by using...

    for(String line : lore){
    //Do stuff with the line
    }

    instead of using index-based interation. This saves you an assignment step and I think it looks cleaner!
     
    Last edited: Feb 10, 2018
Thread Status:
Not open for further replies.

Share This Page