Solved Getting coloured meta from config file.

Discussion in 'Plugin Development' started by hjackson140701, Aug 11, 2016.

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

    hjackson140701

    Hey guys, i'm trying to get a string from my config file.

    As you can see in other instances of this string they work:
    Code:
    if(player.hasPermission("infusion.command.give"))
            {
                ItemStack Infusion = new ItemStack(Material.DIAMOND_PICKAXE);
                ItemMeta InfusionMeta = Infusion.getItemMeta();
                ArrayList<String> lore = new ArrayList<String>();
                String itemLore = getConfig().getString("itemlore");
                String itemLore2 = itemLore.replaceAll("&", "§");
                lore.add(itemLore2);
                InfusionMeta.setLore(lore);
                Infusion.setItemMeta(InfusionMeta);
                playerInventory.addItem(Infusion);
    (its the itemLore2 you're looking at).

    I have a BlockBreak event below but its not working.
    Code:
    @EventHandler
    public void InfusionPickaxeUse(BlockBreakEvent event)
    {
        Player player = event.getPlayer();
      
        if(!event.isCancelled())
            {
        if(player.getItemInHand() == null)
            {
                return;
            }
                String itemLore = getConfig().getString("itemlore");
                String itemLore2 = itemLore.replaceAll("&", "§");
        if((player.getItemInHand().getType() != Material.AIR) &&
                (player.getItemInHand().getItemMeta().getLore().contains(itemLore2)))
            {
        Location loc = event.getBlock().getLocation().add(-1.0D, -1.0D, -1.0D);
            for (int x = 0; x < 3; x++) {
              for (int y = 0; y < 3; y++) {
                for (int z = 0; z < 3; z++)
            {
        Location blockLoc = new Location(loc.getWorld(), loc.getX() + x, loc.getY() + y,
                loc.getZ() + z);
        Block currentBlock = blockLoc.getBlock();
        if((currentBlock.getType() == Material.MOB_SPAWNER) ||
                (currentBlock.getType() == Material.BEDROCK))
            {
            return;
            }
        for(ItemStack drop : currentBlock.getDrops())
            {
            blockLoc.getWorld().dropItem(blockLoc, drop);
            }
        currentBlock.setType(Material.AIR);
            }
    }
    Full code ^

    Code:
                String itemLore = getConfig().getString("itemlore");
                String itemLore2 = itemLore.replaceAll("&", "§");
        if((player.getItemInHand().getType() != Material.AIR) &&
                (player.getItemInHand().getItemMeta().getLore().contains(itemLore2)))
    Not working ^^

    This code is supposed to break a 3x3x3 area around the original block mined, the only trouble i'm having is with the lore on the item check.

    Here is my config.yml file.
    Code:
    # Infusion plugin by HeriPutr // 4p3x // hjackson140701
    nopermsmessage: '&cInsufficient permission(s) &7&o(infusion.command.give)'
    itemlore: '&4Infusion pickaxe'
    infusiongivemessage: '&aHeres an infusion pickaxe, &2%playername%&a'
    Any ideas? No errors are displayed and this code does work as it worked perfectly before I did this check for config string.
     
  2. Offline

    Zombie_Striker

    If the item is "Air", then the method "getItemInHand" would return null. I.e it will never be air.

    Item meta can also be null. Also, the lore can be null. use "hasItemMeta" and "hatLore" before getting them.
    A) Don't use Allmans Style for Java. Use "K&R" instead. I.e
    Code:
    If(...) {
    }
    B) If the event has been canceled, then this method will not run. This if statement is useless.

    your problem: Most likely, the lore does not contain a line that is exactly the same as the string from the config. Debug and print out all the lines of lore.
     
  3. Offline

    hjackson140701

    I tried changing a few things to debug this problem;
    Code:
    if(player.getItemInHand().getType() != Material.AIR)
                {
                    player.sendMessage("material inhand is not air");
                    player.sendMessage(itemLore2);
    This code should send a message "material in hand is not air" and the item lore in the config. Now I know that the problem is somewhere earlier on in the code. Will update when I solve

    Code:
    @EventHandler
        public void InfusionUse(BlockBreakEvent event)
        {
            Player player = event.getPlayer();
    
            if(!event.isCancelled())
            {
                player.sendMessage("block break event");
    Not even this worked so i'm assuming the event isnt even being called on BlockBreak, i'm not sure why?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 12, 2016
  4. Offline

    Marti201

  5. Offline

    hjackson140701

    Alright I fixed it thanks you helped alot

    Yep I have fixed it now, that was it I think, i'm not used to working in one class so it was confusing me.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 12, 2016
Thread Status:
Not open for further replies.

Share This Page