Solved Grabbing an Integer from a Lore

Discussion in 'Plugin Development' started by Coopah, Dec 23, 2015.

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

    Coopah

    Hello! I have a method which creates an item in a GUI and I want to know how to check the lore's buy price.
    My method is this:
    Code:
    public static void createDisplay(Material material, Inventory inv, int Slot, String name, int buyPrice, int sellPrice) {
    
            ItemStack item = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);
            ArrayList<String> Lore = new ArrayList<String>();
            Lore.add(ChatColor.GREEN + "Buy Price: " + ChatColor.GRAY + "$" + buyPrice);
            Lore.add(ChatColor.RED + "Sell Price: " + ChatColor.GRAY + "$" + sellPrice);
            Lore.add(ChatColor.DARK_GRAY + "Shift Left-Click: " + ChatColor.GRAY + "Buy 64");
            Lore.add(ChatColor.DARK_GRAY + "Shift Right-Click: " + ChatColor.GRAY + "Sell 64");
            meta.setLore(Lore);
            item.setItemMeta(meta);
    
            inv.setItem(Slot, item);
    
        }
    How would I grab an Integer from a lore?
     
  2. Offline

    teej107

    @Coopah Save this as a constant (for simplicity)
    Code:
    private static final String BUY_PRICE = ChatColor.GREEN + "Buy Price: " + ChatColor.GRAY + "$"
    Then get the substring of that lore starting at the length of BUY_PRICE. Now you have the number in String form and all you have to do is to convert it to an int.

    EDIT: And since you have the constant, you can just change this
    Code:
    Lore.add(ChatColor.GREEN + "Buy Price: " + ChatColor.GRAY + "$" + buyPrice);
    to this
    Code:
    Lore.add(BUY_PRICE + buyPrice);
    Also please follow naming conventions so your code is easily readable.
     
  3. Offline

    Areoace

    Coopy mate I swear we have almost identical code ahah
     
  4. Offline

    Mrs. bwfctower

    There isn't much variation in the way in which you can do this. Unless you're talking about the messages and all?
     
  5. Offline

    SirMonkeyFood

    @Coopah
    I worked on a project that stored the amount of blocks a pickaxe has broken in the pick's lore. Here's how I did it (May not be the most efficient, just so you know). Naturally, you'd want to get rid of the counting I had (Unless Your Price Changes).


    Code:
                if (p.getInventory().getItemInHand().getType() == Material.DIAMOND_PICKAXE
                        || p.getInventory().getItemInHand().getType() == Material.IRON_PICKAXE
                        || p.getInventory().getItemInHand().getType() == Material.STONE_PICKAXE
                        || p.getInventory().getItemInHand().getType() == Material.GOLD_PICKAXE
                        || p.getInventory().getItemInHand().getType() == Material.WOOD_PICKAXE) {
                    if (p.getInventory().getItemInHand().hasItemMeta() == true
                            && p.getInventory().getItemInHand().getItemMeta()
                                    .hasLore() == true) {
                        //Verifies the Object's a Pickaxe
    
                        ItemStack pick = p.getInventory().getItemInHand();
                        int last = 0;
    
                        for (String line : pick.getItemMeta().getLore()) {
                            //Checks Each Line of Lore
                            if (line.startsWith(ChatColor.WHITE + "Blocks Broken:")) {
                                //Verifies That The Pick In Question Has The Tag I'd Like It To.
                                String bs = line.replace("Blocks Broken: ", "");
                                bs = ChatColor.stripColor(bs);
                                //Deletes all Unnecessary Non-Numerical Portions of the Lore.
                                int n = Integer.parseInt(bs);
                                //Assigns the Number to an Interger
                                last = n + 1;
                                //The "Counting" Part
                            }
                        }
    
                        ItemMeta meta = pick.getItemMeta();
                        ArrayList<String> lore = new ArrayList<String>();
                        lore.add(ChatColor.WHITE + "Blocks Broken: " + last);
                        meta.setLore(lore);
                        //Re-Adds the Lore, but With the Counter Applied.
     
    Last edited: Dec 24, 2015
  6. Offline

    Mrs. bwfctower

    @SirMonkeyFood No need to check if a boolean '==' true. You can just use "if (someBoolean)" or "if (!someBoolean)"

    Also a substring is technically more efficient than a call to #replace.
     
    SirMonkeyFood likes this.
  7. Offline

    SirMonkeyFood

    @Mrs. bwfctower
    Oh, that makes sense.

    Also, as far as substrings go, theoretically, I could do this:

    Code:
    String bs = line.substring(15);
                                bs = ChatColor.stripColor(bs);
                                int n = Integer.parseInt(bs);
    Instead of this:

    Code:
    String bs = line.replace("Blocks Broken: ", "");
                                bs = ChatColor.stripColor(bs);
                                int n = Integer.parseInt(bs);
    Right?
     
  8. Offline

    mcdorli

    Theoretically, you don't need stripColor, if you only want one letter. ChatColors are visible in the string.
     
  9. Offline

    Mrs. bwfctower

    If you don't strip color, and there is a color there, a NumberFormatException will be thrown.
     
  10. Offline

    mcdorli

    If you pick only one letter, then it brings it's color too?
     
  11. Offline

    Mrs. bwfctower

    Where do you get the "only one letter" from?
     
  12. Offline

    teej107

    Of course. Don't be a silly dope and grab a String with a ChatColor. My explanation doesn't and is fine. Didn't need to strip the colors.
     
  13. Offline

    Mrs. bwfctower

    Yeah yours works fine.

    For some reason though 15 didn't include the color, and hard coding that in loses the readability that could have prevented this confusion.
     
  14. Offline

    Coopah

    @SirMonkeyFood @Mrs. bwfctower @mcdorli @teej107
    Thanks mate!
    Anyone know why this isn't removing it from the string?
    Code:
    String toBeRemoved;
                                    toBeRemoved = ChatColor.GREEN + "Buy Price: " + ChatColor.GRAY + "$";
                                    String bs = line.replaceAll(toBeRemoved, "");
                                    bs = ChatColor.stripColor(bs);
                                    int n = Integer.parseInt(bs);
    
     
    Last edited: Dec 26, 2015
  15. Offline

    Areoace

    @Coopah it is the same concept as what you did before but in reverse, try and figure it out. Please stop requesting to be spoonfed on everything, it really isn't beneficial for your learning.
     
  16. It'd be better to do
    Code:
    line.substring(toBeRemoved.length());
    You shouldn't need to "stripColor" as a color is basically just some characters attached to the string. "&e" and such. They'd be stripped using the String#substring method, and if you just get the length of the string using String#length, all matches up.

    To support my statement: ChatColor.java Source

    Specifically
    Code:
        @Override
        public String toString() {
            return toString;
        }
    Code:
        private ChatColor(char code, int intCode, boolean isFormat) {
            this.code = code;
            this.intCode = intCode;
            this.isFormat = isFormat;
            this.toString = new String(new char[] {COLOR_CHAR, code});
        }
    Code:
        public static final char COLOR_CHAR = '\u00A7';
    \u00A7 is used in minecraft, it's also known as "ยง": Formatting codes

    stripColor is actually inefficient, and won't do anything for you :) When you concatenate a String and a class as you do in toBeRemoved, the class's toString method will be called, resulting in a String total. Then you can use the String#length() to get the full length, including color codes, etc.. If that String is the beginning of your String, as it is in this case. It's more efficient to simply cut it off, rather than run a pattern matcher.
     
  17. Offline

    Coopah

    @BreezerFly
    Ahh, thank you, but it's still not removing anything?
    Code:
                                   String toBeRemoved;
                                    toBeRemoved = ChatColor.GREEN + "Buy Price: " + ChatColor.GRAY + "$";
                                    line.substring(toBeRemoved.length());
                                    int n = Integer.parseInt(line);
     
  18. Offline

    Xerox262

    You can just do
    int n = Integer.parseInt(line.substring(line.lastIndexOf('$')));

    If it still doesn't work then show us more, odds are something is mistaken like another color after the $
     
  19. Well... You need to do line = line.substring......
     
    Coopah likes this.
Thread Status:
Not open for further replies.

Share This Page