Splitting enchantments? (string.split())

Discussion in 'Plugin Development' started by GamerBah, Jul 19, 2015.

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

    GamerBah

    Hi there! I'm looking for a way to split up the message you get when you put an item's "getEnchantments()" into a string. By default, it comes out as "{Enchantment,[16, DAMAGE_ALL, 5], Enchantment[#, enchantment, level]}" I have split it down so that it separates the 2 enchantments, like this:

    16, DAMAGE_ALL, 5
    (id), KNOCKBACK, 2

    I'm trying to find a way to split that above strings into 2 different ones, so that I can take that data and use it. This is the code I have so far:


    Code:
    int amount = enchantments.size();
                String a = enchantments.toString().replace("{Enchantment[", "")
                        .replace("]=", ", ");
                String b = null;
                for (int i = 0; i <= amount - 1; i++) {
                    b = getModifiedString(a).replace(", Enchantment[", "|")
                            .replace("]=", ", ").replace("}", "");
                    String[] enchantment = b.split("\\|", amount);
                    String[] enchantmentSplit = enchantment[i].replace(" ", "").split(",");
                    p.sendMessage(enchantmentSplit[i]);
                }

    Split Numbers:
    0.........1...........2
    16, DAMAGE_ALL, 5

    3.........4...........5
    (id), KNOCKBACK, 2

    The "p.sendMessage(enchantmentSplit); " does not give the wanted results, as I am not sure how I would go about getting the 2nd split (1 and 4 in the "diagram" above) of each string. It is most likely a very easy solution, but with my code I have I can't see it :p
     
  2. Offline

    567legodude

    @GamerBah
    Code:
    public static String listEnchants(ItemStack item) {
        if (item.getEnchantments().size() < 1) return null;
        String f = "";
        for (Map.Entry<Enchantment, Integer> entry : item.getEnchantments().entrySet()) f += entry.getKey().getName() + ", ";
        return f.substring(0, f.length()-2);
    }
     
    Last edited: Jul 19, 2015
  3. Offline

    _Filip

    Is there a specific reason you need the string??
     
  4. Offline

    GamerBah

    So I can put the enchantment name and level onto a book

    I've used @567legodude's method and I've come up with this code:
    Code:
    String[] enchantments = listEnchants(inHand).split("\\,");
                Inventory inv = Bukkit.getServer().createInventory(p, 27,
                        "Item Upgrade Altar");
                for (int i = 0; i <= enchantments.length - 1; i++) {
                    p.sendMessage(ChatColor.RED + enchantments[i]);
                    String[] split = enchantments[i].split("\\-");
                    for (int i2 = 0; i <= split.length; i++) {
                    inv.setItem(i, createBook(ChatColor.YELLOW + "Upgrade your " + split[i2] + " " + split[i2 + 1] + " to ..."));
                    }
                }
                p.openInventory(inv);
    Now, when the event runs, it's only returning 1 of the enchantments.
    Note that there's also 3 books being created (? wat)

    [​IMG]
    I am really not sure what I'm doing wrong, or how to fix it... Help is very greatly appreciated!


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  5. Offline

    567legodude

    @GamerBah I didn't know that you wanted the level too. Just use .getEnchantments() on the itemstack
    which returns a HashMap<Enchantment, Integer>
    to get the enchantment name use map.getEntry().getName()
    to get the level use map.getValue()
     
    Shortninja66 and seanliam2000 like this.
Thread Status:
Not open for further replies.

Share This Page