Solved Pickaxe and lore.

Discussion in 'Plugin Development' started by SirMonkeyFood, Apr 14, 2015.

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

    SirMonkeyFood

    Alright, so I'm making this plugin, and it needs to count every time a pick breaks a block, and store it as part of the lore on the pick. Here's my code: http://pastebin.com/ArwUwAi7

    When it gets to the for loop, I parsed the int, and tried to throw it into the lore. I'm pretty sure I messed up the renaming the item part though. Any thoughts? Thanks for any help <3

    Forgot to mention, the "else" part works to add the lore to the pick just fine, it's the counting part that's causing issues(where the lore is renamed).
     
    Last edited by a moderator: Apr 14, 2015
  2. Offline

    SuperOriginal

    You're checking if the lore contains a ChatColor, but I don't see you setting the lore to a ChatColor? Also, I would only use the for loop to retrieve the amount of blocks broken and then operate outside of the loop instead of doing everything inside of it. But, I assume the difference in result is negligible anyways.
     
    Last edited: Apr 14, 2015
  3. Offline

    SirMonkeyFood

    Ah, yea, I plan to add in some colors in a sec. I'll give it a go, thanks.

    Same result. I guess the problem lies in the code itself then. It worked when it's supposed to initially add the lore though. Here's the updated code btw.
    Code:
    package me.SirMonkeyFood.SMPickRankup;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerExpChangeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.SirMonkeyFood.SMPickRankup.Main;
    
    public class PlayerListener implements Listener {
    
        public PlayerListener(Main plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);  
        }
      
        @EventHandler
        public void onBlockBreak(BlockBreakEvent bb){
            Player p = bb.getPlayer();
            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 &&
                p.getInventory().getItemInHand().getItemMeta().getLore().contains(ChatColor.WHITE + "Blocks Broken: ")){
                    ItemStack pick  = p.getInventory().getItemInHand();
                    int last = 0;
                    for(String line : pick.getItemMeta().getLore()) {
                        if(line.startsWith("Blocks Broken: ")){
                            String bs = line.replace("Blocks Broken: ", "");
                            bs = ChatColor.stripColor(bs);
                            int n = Integer.parseInt(bs);
                            last = n + 1;
                        }else{
                            return;
                        }
                 
                    }
                        p.sendMessage("hi" + last);
                        ItemMeta meta = pick.getItemMeta();
                        ArrayList<String> lore = new ArrayList<String>();
                        lore.add("Blocks Broken: " + last);
                        meta.setLore(lore);
                        pick.setItemMeta(meta);  
                    }else{
                    ItemStack pick2 = p.getInventory().getItemInHand();
                    ItemMeta meta2 = pick2.getItemMeta();
                    ArrayList<String> lore2 = new ArrayList<String>();
                    lore2.add("Blocks Broken: 1");
                    meta2.setLore(lore2);
                    pick2.setItemMeta(meta2);
                    }
                }
                  
            }
        }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  4. Offline

    SuperOriginal

    Have you tried debugging? And also, what I meant by checking the ChatColors was these two lines:

    and
    Won't those never be equal?

    Edit: Also, look at line 37 as it does not agree with the check at 33
     
  5. Offline

    SirMonkeyFood

    Oh. *facepalm commences* I just messed that up a second ago xP. I fixed that and retested the code. Here's everything updated (Again).

    Code:
    package me.SirMonkeyFood.SMPickRankup;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerExpChangeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.SirMonkeyFood.SMPickRankup.Main;
    
    public class PlayerListener implements Listener {
    
        public PlayerListener(Main plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    
        @EventHandler
        public void onBlockBreak(BlockBreakEvent bb) {
            Player p = bb.getPlayer();
            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
                        && p.getInventory().getItemInHand().getItemMeta().getLore()
                                .contains("Blocks Broken: ")) {
                    ItemStack pick = p.getInventory().getItemInHand();
                    int last = 0;
                    for (String line : pick.getItemMeta().getLore()) {
                        if (line.startsWith("Blocks Broken: ")) {
                            String bs = line.replace("Blocks Broken: ", "");
                            bs = ChatColor.stripColor(bs);
                            int n = Integer.parseInt(bs);
                            last = n + 1;
                        } else {
                            return;
                        }
                        // moar stuffs here
                    }
                    p.sendMessage("hi" + last);
                    ItemMeta meta = pick.getItemMeta();
                    ArrayList<String> lore = new ArrayList<String>();
                    lore.add("Blocks Broken: " + last);
                    meta.setLore(lore);
                    pick.setItemMeta(meta);
                } else {
                    ItemStack pick2 = p.getInventory().getItemInHand();
                    ItemMeta meta2 = pick2.getItemMeta();
                    ArrayList<String> lore2 = new ArrayList<String>();
                    lore2.add("Blocks Broken: 1");
                    meta2.setLore(lore2);
                    pick2.setItemMeta(meta2);
                }
            }
    
        }
    }
    
    
     
    Last edited: Apr 14, 2015
  6. Offline

    mythbusterma

  7. Offline

    nverdier

    At first I was like meh...
    But then
     
    Last edited by a moderator: Apr 14, 2015
  8. Offline

    SirMonkeyFood

    @mythbusterma Better? Spaced it out a bit, and seperated groups of statements a little better.
     
  9. Offline

    nverdier

    Nope. Using Eclipse? control + shift + F
     
  10. Offline

    SirMonkeyFood

    Mind = blown
     
    SuperOriginal likes this.
  11. Offline

    SirMonkeyFood

    Alright, I tried some more troubleshooting, and found that not only does the exception part work, it is the only thing that works. I added in some messages in my code, and the only one that fired in the event of a broken block was the "initial replace", which happened every time, reguardless of the item's lore. Here's the code:

    Code:
        @EventHandler
        public void onBlockBreak(BlockBreakEvent bb) {
            Player p = bb.getPlayer();
            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
                        && p.getInventory().getItemInHand().getItemMeta().getLore()
                                .contains("Blocks Broken: ")) {
                    ItemStack pick = p.getInventory().getItemInHand();
                    int last = 0;
                    p.sendMessage("before for loop");
                    for (String line : pick.getItemMeta().getLore()) {
                        if (line.startsWith("Blocks Broken: ")) {
                            String bs = line.replace("Blocks Broken: ", "");
                            bs = ChatColor.stripColor(bs);
                            int n = Integer.parseInt(bs);
                            last = n + 1;
                            p.sendMessage("After Parsing int");
                        } else {
                            return;
                        }
                    p.sendMessage("End of For loop");
                    }
                    p.sendMessage("change");
                    ItemMeta meta = pick.getItemMeta();
                    ArrayList<String> lore = new ArrayList<String>();
                    lore.add("Blocks Broken: " + last);
                    meta.setLore(lore);
                    pick.setItemMeta(meta);
                } else {
                    ItemStack pick2 = p.getInventory().getItemInHand();
                    ItemMeta meta2 = pick2.getItemMeta();
                    ArrayList<String> lore2 = new ArrayList<String>();
                    lore2.add("Blocks Broken: 1");
                    meta2.setLore(lore2);
                    pick2.setItemMeta(meta2);
                    p.sendMessage("initial replace");
                }
            }
    
        }
    }
    
     
  12. Offline

    Zombie_Striker

    JavaNamingConventions may be the one to blame. Minecarft has a class called PlayerListener, so all you have to do is change the class name (e.g. PlayerListener2 or PlayerListenerAnything)

    Also, I don't see you doing any nullchecks,

    Instead of having a huge if statement, make an array full of pickaxes and just see if the array contains the ItemInHand (makes it look neater)
     
    SirMonkeyFood likes this.
  13. Offline

    SirMonkeyFood

    Alright, I tried renaming the class, and no dice. I'm not sure where I would add a nullcheck, I thought I had all my bases covered with the checks I did, but maybe not. If you could clarify, that'd be great. Thanks for the help!
     
  14. Offline

    Zombie_Striker

    You first have to make sure that the item in hand is not null.

    You should also try using the .sendMessage before and after the if statements.
     
  15. Offline

    SirMonkeyFood

    Ah. the null didn't change anything, but the problem seems to be narrowed to these lines:
    Code:
                if (p.getInventory().getItemInHand().hasItemMeta() == true
                        && p.getInventory().getItemInHand().getItemMeta().hasLore() == true
                        && p.getInventory().getItemInHand().getItemMeta().getLore()
                                .contains("Blocks Broken: ")) { 
     
  16. Offline

    I Al Istannen

    @SirMonkeyFood Correct me if im wrong, but doesnt getLore().contains(String) checks for the exact String. So if you dont have the exact String, and for example a changing number at the end that wont work. Iterate over the contens of the Lore instead.
     
    SirMonkeyFood likes this.
  17. Offline

    SuperOriginal

    @I Al Istannen [brainfart].contains() is not .equals()[/brainfart]

    Edit: Nevermind.... Lore is a list....... yeah.
     
    Last edited: Apr 16, 2015
    SirMonkeyFood likes this.
  18. Offline

    I Al Istannen

    @SuperOriginal In case of a string, yes. But in case of a StringList, i thought it would be. Otherwise it would have to loop through all Strings and call the String.contains method on them.
     
    SirMonkeyFood likes this.
  19. Offline

    SirMonkeyFood

    Heh, turns out i didn't even need to check for the specific lore, i did that later on down the code. Have I mentioned how much I love you bukkit people? <3

    @I Al Istannen Hey, so plot twist, everything works including enchantments, but if I add in some lore that isn't part of the code, it stops working. (No stack trace, it just gives up) How do I maintain the lore previously on the pick? Here's the new code:
    Code:
    package me.SirMonkeyFood.SMPickRankup;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerExpChangeEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import com.sun.corba.se.impl.javax.rmi.CORBA.Util;
    
    import me.SirMonkeyFood.SMPickRankup.Main;
    
    public class PlayerListener21 implements Listener {
    
        public PlayerListener21(Main plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    
        @EventHandler
        public void onBlockBreak(BlockBreakEvent bb) {
            Player p = bb.getPlayer();
            if (p.getInventory().getItemInHand() != null) {
                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) {
                        ItemStack pick = p.getInventory().getItemInHand();
                        int last = 0;
                        for (String line : pick.getItemMeta().getLore()) {
                            if (line.startsWith("Blocks Broken:")) {
                                String bs = line.replace("Blocks Broken: ", "");
                                bs = ChatColor.stripColor(bs);
                                int n = Integer.parseInt(bs);
                                last = n + 1;
                            } else {
                                return;
                            }
                        }
                        ItemMeta meta = pick.getItemMeta();
                        ArrayList<String> lore = new ArrayList<String>();
                        lore.add("Blocks Broken: " + last);
                        meta.setLore(lore);
                        int currente = pick.getEnchantmentLevel(Enchantment.DIG_SPEED);
                        int currentf = pick.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
                        int levelupe = currente + 1;
                        int levelupf = currentf + 1;
                        if(last % 1 == 0){
                            meta.addEnchant(Enchantment.DIG_SPEED, levelupe, true);
                            meta.addEnchant(Enchantment.LOOT_BONUS_BLOCKS, levelupf, true);
                        }
                        pick.setItemMeta(meta);
                    } else {
                        ItemStack pick2 = p.getInventory().getItemInHand();
                        ItemMeta meta2 = pick2.getItemMeta();
                        ArrayList<String> lore2 = new ArrayList<String>();
                        lore2.add("Blocks Broken: 1");
                        meta2.setLore(lore2);
                        meta2.addEnchant(Enchantment.DIG_SPEED, 1, true);
                        meta2.addEnchant(Enchantment.LOOT_BONUS_BLOCKS, 1, true);
                        pick2.setItemMeta(meta2);
       
                    }
                }
            } else {
                return;
            }
        }
    }
     
    Last edited by a moderator: Apr 17, 2015
  20. Offline

    I Al Istannen

    @SirMonkeyFood What is lore "that isnt part of the code" you mean adding a thing like "Super Pickaxe" to the lore? If so, the error is at line 47. You checked before if the line starts with "Blocks Broken:". That works. But in line 47 you return, that means cancel the whole code, if the line doesn't start with "Blocks Broken:". So imagine the Lores are "Blocks Broken: 1" and "Hi". It checks the first line: The if is true => Setting the things you want. Then it checks the 2nd line: The if is false => Running the else code => return => not working.
     
    Last edited: Apr 17, 2015
    SirMonkeyFood likes this.
  21. Offline

    SirMonkeyFood

    Ah, thanks man! I'll try it when I get home.

    Every thing works perfectly, Thanks to everyone who helped! :p Marking thread as solved.

    As for anyone referencing this thread from teh googlez, Here's my final code(Annotated!). Thanks again Everyone!

    Show Spoiler

    Code:
    package me.SirMonkeyFood.SMPickRankup;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.FireworkEffect;
    import org.bukkit.FireworkEffect.Type;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Firework;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.FireworkMeta;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.SirMonkeyFood.SMPickRankup.Main;
    
    public class PlayerListener21 implements Listener {
    
        public PlayerListener21(Main plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    
        @EventHandler
        public void onBlockBreak(BlockBreakEvent bb) {
            Player p = bb.getPlayer();
            if (p.getInventory().getItemInHand() != null) {
                //makes sure I have a pick in my hand
                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) {
                        ItemStack pick = p.getInventory().getItemInHand();
                        int last = 0;
                        //tests for the pick's current number of broken blocks
                        for (String line : pick.getItemMeta().getLore()) {
                            if (line.startsWith(ChatColor.WHITE + "Blocks Broken:")) {
                                String bs = line.replace("Blocks Broken: ", "");
                                bs = ChatColor.stripColor(bs);
                                int n = Integer.parseInt(bs);
                                last = n + 1;
                            }
                        }
                        //re-adds the pick's lore, to have the number of blocks broken
                        ItemMeta meta = pick.getItemMeta();
                        ArrayList<String> lore = new ArrayList<String>();
                        lore.add(ChatColor.WHITE + "Blocks Broken: " + last);
                        meta.setLore(lore);
                        //handles the lvl up
                        int currente = pick
                                .getEnchantmentLevel(Enchantment.DIG_SPEED);
                        int currentf = pick
                                .getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
                        int levelupe = currente + 1;
                        int levelupf = currentf + 1;
                        //test for if a number is divisible by 1k
                        //(Every thousand blocks, the pick ranks up)
                        if (last % 1000 == 0) {
                            //adds enchants
                            meta.addEnchant(Enchantment.DIG_SPEED, levelupe, true);
                            meta.addEnchant(Enchantment.LOOT_BONUS_BLOCKS,
                                    levelupf, true);
                            //Message on pickrankup
                            p.sendMessage(ChatColor.BLACK
                                    + ""
                                    + ChatColor.BOLD
                                    + "---------------------------------------------");
                            p.sendMessage("");
                            p.sendMessage("");
                            p.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD
                                    + "                Your Pickaxe Ranked Up!");
                            p.sendMessage("");
                            p.sendMessage("");
                            p.sendMessage(ChatColor.BLACK
                                    + ""
                                    + ChatColor.BOLD
                                    + "---------------------------------------------");
                            //Firework Stuff from here on out from here: https://bukkit.org/threads/spawn-firework.118019/
                            Firework fw = (Firework) p.getWorld().spawnEntity(
                                    p.getLocation(), EntityType.FIREWORK);
                            FireworkMeta fwm = fw.getFireworkMeta();
    
                            Random r = new Random();
    
                            int rt = r.nextInt(4) + 1;
                            Type type = Type.BALL;
                            if (rt == 1)
                                type = Type.BALL;
                            if (rt == 2)
                                type = Type.BALL_LARGE;
                            if (rt == 3)
                                type = Type.BURST;
                            if (rt == 4)
                                type = Type.CREEPER;
                            if (rt == 5)
                                type = Type.STAR;
    
                            int r1i = r.nextInt(17) + 1;
                            int r2i = r.nextInt(17) + 1;
                            Color c1 = getColor(r1i);
                            Color c2 = getColor(r2i);
    
                            FireworkEffect effect = FireworkEffect.builder()
                                    .flicker(r.nextBoolean()).withColor(c1)
                                    .withFade(c2).with(type).trail(r.nextBoolean())
                                    .build();
    
                            fwm.addEffect(effect);
    
                            int rp = r.nextInt(2) + 1;
                            fwm.setPower(rp);
    
                            fw.setFireworkMeta(fwm);
                        }
                        pick.setItemMeta(meta);
                    } else {
                        //If the pick doesn't start with "Blocks Broken" Then this gives it the setup, and enchantments lvl 1 if it doesn't have any.
                        ItemStack pick2 = p.getInventory().getItemInHand();
                        ItemMeta meta2 = pick2.getItemMeta();
                        ArrayList<String> lore2 = new ArrayList<String>();
                        lore2.add(ChatColor.WHITE + "Blocks Broken: 1");
                        meta2.setLore(lore2);
                        if ((pick2.getItemMeta().hasEnchant(Enchantment.DIG_SPEED))) {
                        } else {
                            meta2.addEnchant(Enchantment.DIG_SPEED, 1, true);
                        }
                        if ((pick2.getItemMeta()
                                .hasEnchant(Enchantment.LOOT_BONUS_BLOCKS))) {
                        } else {
                            meta2.addEnchant(Enchantment.LOOT_BONUS_BLOCKS, 1, true);
                        }
                        pick2.setItemMeta(meta2);
    
                    }
                }
            } else {
                return;
            }
        }
    //more firework stuff
        private Color getColor(int i) {
            Color c = null;
            if (i == 1) {
                c = Color.AQUA;
            }
            if (i == 2) {
                c = Color.BLACK;
            }
            if (i == 3) {
                c = Color.BLUE;
            }
            if (i == 4) {
                c = Color.FUCHSIA;
            }
            if (i == 5) {
                c = Color.GRAY;
            }
            if (i == 6) {
                c = Color.GREEN;
            }
            if (i == 7) {
                c = Color.LIME;
            }
            if (i == 8) {
                c = Color.MAROON;
            }
            if (i == 9) {
                c = Color.NAVY;
            }
            if (i == 10) {
                c = Color.OLIVE;
            }
            if (i == 11) {
                c = Color.ORANGE;
            }
            if (i == 12) {
                c = Color.PURPLE;
            }
            if (i == 13) {
                c = Color.RED;
            }
            if (i == 14) {
                c = Color.SILVER;
            }
            if (i == 15) {
                c = Color.TEAL;
            }
            if (i == 16) {
                c = Color.WHITE;
            }
            if (i == 17) {
                c = Color.YELLOW;
            }
    
            return c;
        }
    
    }


    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Apr 17, 2015
Thread Status:
Not open for further replies.

Share This Page