Solved Change specific lore line

Discussion in 'Plugin Development' started by plisov, Jul 26, 2017.

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

    plisov

    Im trying to make a plugin where you need to choose a certain class and depending on what class you chose, you can you specific items. When you type /class, you can choose a variety of classes. I chose Archer for example. It saves the class I chose in a config file as a String 'Archer'. You then receive an item. The item looks like this
    https://gyazo.com/10c85ae06023755a6aaab60136881c1d
    As you can see, there is an X next to the required class. How do I make it so that when a player moves, it checks what class the player chose, loops through all the items with a lore that contains ' X Class Req' and then checks if the player chose the class specified on that line, it will change that specific line to '✔ Class Req'. Same with Lv. Min. I save the player's level in the same config. How do I check on player move, if the player has reached the specified lv.

    Class that gives the actual bow
    Code:
    package me.plisov.ventureland.classes;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Classes implements Listener {
    
        public static ItemStack starterBow(Player player) {
    
            FileConfiguration config = null;
            File file = new File("plugins" + File.separator + "VentureLand" + File.separator + "user-data" + File.separator
                    + player.getName() + ".yml");
    
            config = YamlConfiguration.loadConfiguration(file);
    
            ItemStack bow = new ItemStack(Material.BOW, 1);
            ItemMeta bowMeta = bow.getItemMeta();
    
            bowMeta.setDisplayName(ChatColor.WHITE + "Oak Wood Bow");
    
            String classString = "";
    
            if (config.getString("Class") == "Archer") {
                classString = ChatColor.GREEN + "✔ " + ChatColor.GRAY + "Class Req: Archer";
            } else {
                classString = ChatColor.RED + "✘ " + ChatColor.GRAY + "Class Req: Archer";
            }
    
            String levelString = "";
    
            if (config.getInt("Level") >= 0) {
                levelString = ChatColor.GREEN + "✔ " + ChatColor.GRAY + "Lv. Min: 0";
            } else {
                levelString = ChatColor.RED + "✘ " + ChatColor.GRAY + "Lv. Min: 0";
            }
    
            bowMeta.setLore(Arrays.asList(ChatColor.GRAY + "Slow Attack Speed", "",
                    ChatColor.GOLD + "✯ Neutral Damage: 5-13", "", classString, levelString, "",
                    ChatColor.WHITE + "Normal Item", ChatColor.RED + "Untradable Item"));
    
            bow.setItemMeta(bowMeta);
    
            player.getInventory().addItem(bow);
           
            return bow;
        }
       
        public static void archer(Player player) {
    
            FileConfiguration config = null;
            File file = new File("plugins" + File.separator + "VentureLand" + File.separator + "user-data" + File.separator
                    + player.getName() + ".yml");
    
            config = YamlConfiguration.loadConfiguration(file);
           
            if (config.getString("Class").equals("None")) {
    
                config.set("Class", "Archer");
                try {
                    config.save(file);
                } catch (IOException e) {
                }
    
                player.sendMessage(ChatColor.GREEN + "You have chosen the " + ChatColor.GOLD + "" + ChatColor.BOLD
                        + "Archer" + ChatColor.GREEN + " class.");
               
                starterBow(player);
               
            } else {
                player.sendMessage(ChatColor.RED + "You have already chosen a class!");
                return;
            }
        }
    }
    
    On Player Move Class
    Code:
    package me.plisov.ventureland;
    
    import java.io.File;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class PlayerMove implements Listener {
    
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
    
            Player player = (Player) event.getPlayer();
    
            FileConfiguration config = null;
            File file = new File("plugins" + File.separator + "VentureLand" + File.separator + "user-data" + File.separator
                    + player.getName() + ".yml");
    
            config = YamlConfiguration.loadConfiguration(file);
    
            for (ItemStack items : player.getInventory().getContents()) {
    
                ItemMeta itemsMeta = items.getItemMeta();
    
                String minLevel = ChatColor.stripColor(ChatColor.RED + "✘ " + ChatColor.GRAY + "Lv. Min:");
    
                int minLevelInt = Integer.parseInt(minLevel.replaceAll("\\D", ""));
            }
        }
    }
    
    Any help is much appreciated :)
     
  2. Offline

    Zombie_Striker

    @plisov
    To replace a line in the lore, do the following:
    1. Create a new List. This will be the new lore.
    2. Create a for int loop. Start at 0, and loop up the the item's lore length.
    3. Inside that loop, check if the int is equal to Class Req line, either by doing an index check or seeing if the line contains those words. If it does, add a new line to new List with the check mark. Else, just add the line from the old lore to the new list.
    4. Outside of the loop, get the item's instance and the item meta. Set the lore, reset the item meta, and then reset the item.
    BTW: For the file path, use getDataFolder() to get the VentureLand file. Also, don't cast getPlayer to a player. It is already a player.
     
  3. Offline

    plisov

    I didn't quite understand what you meant by checking if the int is equal to the Class Req line (which is a string). I made this little for loop
    Code:
    package me.plisov.ventureland;
    
    import java.awt.List;
    import java.io.File;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class PlayerMove implements Listener {
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
    
            List list = new List();
    
            Player player = event.getPlayer();
    
            player.sendMessage("Working 1");
           
            FileConfiguration config = null;
            File file = new File("plugins" + File.separator + "VentureLand" + File.separator + "user-data" + File.separator
                    + player.getName() + ".yml");
    
            config = YamlConfiguration.loadConfiguration(file);
    
            for (String line : player.getItemInHand().getItemMeta().getLore()) {
    
                player.sendMessage("Working 2");
               
                if (line.contains(ChatColor.RED + "✘ " + ChatColor.GRAY + "Class Req:")) {
    
                    player.sendMessage("Working 3");
    
                    if (config.getString("Class").equals("Archer")) {
                        list.add(ChatColor.GREEN + "✔" + ChatColor.GRAY + "Class Req:");
    
                        player.sendMessage("Working 4");
    
                        ItemStack item = player.getItemInHand();
                        ItemMeta itemMeta = player.getItemInHand().getItemMeta();
    
                        itemMeta.getLore().toString().replaceAll(ChatColor.RED + "✘ " + ChatColor.GRAY + "Class Req:",
                                ChatColor.GREEN + "✔" + ChatColor.GRAY + "Class Req:");
                       
                        item.setItemMeta(itemMeta);
    
                        player.sendMessage("Working 5");
                    }
                }
            }
    
        }
    }
    
    It seems to be going all the way up to 'Working 5' however nothing in the lore changes
     
  4. Offline

    Machine Maker

    @plisov The itemMeta#getLore() method returns a List<String>. Create a new ArrayList<String>, add a string to it, and set the lore using
    Code:
    itemMeta.setLore(List<String>);
     
  5. Offline

    plisov

    Won't that erase the current lore on the item?
     
  6. Offline

    Machine Maker

    @plisov
    Then first get the lore, and then add the new line.

    Code:
    List<String> lore = itemMeta.getLore();
    lore.set(index (the lore line), loreString);
    itemMeta.setLore(lore);
     
    Zombie_Striker likes this.
  7. Offline

    plisov

    Thanks I fixed it.
     
Thread Status:
Not open for further replies.

Share This Page