Solved Getting the amount of blocks mined with an item and putting it onto a lore

Discussion in 'Spigot Plugin Development' started by Nullinq, Feb 17, 2020.

  1. Hello,
    I'm fairly new to Java/Spigot development, but I can make some basic plugins.
    I'm currently making a plugin where I need to get the amount of blocks mined by a Diamond Pickaxe, and put the amount of blocks the item has mined onto it's lore.

    This is my current code:
    Code (Text):
            final Player player = e.getPlayer();
            final ItemStack item = player.getItemInHand();
            final ItemMeta meta = item.getItemMeta();
            final List<String> minedLore = new ArrayList<>();
            int blocksMined = 0;
            if (player.getItemInHand().getType().equals(Material.DIAMOND_PICKAXE)) {
                try {
                    final String line = minedLore.get(1);
                    final String[] split = line.split(" ");
                    blocksMined = Integer.parseInt(split[2]);
                } catch (final NumberFormatException ex) {}

                minedLore.add("Mined: " + (blocksMined + 1));
                meta.setLore(minedLore);
                item.setItemMeta(meta);
            }
    When mining a block, it's supposed to add 1 block to the lore every time a block has been mined.

    I also get this error when mining any block:
    Code (Text):
    Could not pass event BlockBreakEvent to Tokens v1.0.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.breakBlock(PlayerInteractManager.java:286) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.a(PlayerInteractManager.java:121) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:623) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockDig.a(SourceFile:40) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockDig.a(SourceFile:10) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_241]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
            at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_241]
            at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_241]
            at me.stress.tokens.events.Events.onBlockBreak(Events.java:101) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[paper-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 17 more
    Any help is appreciated,
    Thank you.
     
  2. Okay, so:
    1) You are assigning a new ArrayList to your minedLore-list so it naturally is empty (and does not contain any lines
    2) You have an empty catch-block
    3) minedLore.add(...) will add a new line every time this command is being executed. The first time you have a zero, naturally you will have a one every time a block is being mined.
     
  3. So, will something like this work?
    Code (Text):
            final Player player = e.getPlayer();
            final ItemStack item = player.getItemInHand();
            final ItemMeta meta = item.getItemMeta();
            final List<String> minedLore = new ArrayList<>();
            int blocksMined = 0;
            if (player.getItemInHand().getType().equals(Material.DIAMOND_PICKAXE)) {
                minedLore.add("Mined: " + (blocksMined + 1));
                try {
                    final String line = minedLore.get(1);
                    final String[] split = line.split(" ");
                    blocksMined = Integer.parseInt(split[2]);
                } catch (final NumberFormatException ex) {}

                meta.setLore(minedLore);
                item.setItemMeta(meta);
            }
     
  4. This time you are, again, creating a new list (empty), adding one line (the contents will be “Mined: 1”). Now the ‘complicated’ part of your code simply takes that line and assigns blocksMined to 1, which (if that part of the code worked) will do... exactly nothing. You are getting split[2], however in Java arrays start at index 0, thus this will throw an IndexOutOfBoundsException. The same will be true for minedLore.get(1), again, Lists are like arrays, they start at index 0.
     
  5. Okay, thank you.