Solved Item metadata causes plugin to stop working.

Discussion in 'Plugin Development' started by Nickname97, Aug 12, 2015.

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

    Nickname97

    I have two event handlers. One to add metadata to block places, and one to drop double ores on block break. The block metadata should tell me if the block is natural or placed by the player. I only want the ores to drop double if the block is natural. If I leave the commented section in, the plugin runs, but the ores do not drop. If I remove it, the ores drop fine, but even if it is a player placed block.

    Code:
    @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Block block = event.getBlock();
            Location loc = block.getLocation();
            /*if (block.getMetadata("PLACED") != null) {
               return;
            }*/
            if ((block.getType() == Material.STONE)) {
                block.getWorld().dropItem(loc, new ItemStack(Material.STONE, 1));
            }
            if ((block.getType() == Material.COAL_ORE)) {
                block.getWorld().dropItem(loc, new ItemStack(Material.COAL, 1));
            }
            if ((block.getType() == Material.DIAMOND_ORE)) {
                block.getWorld().dropItem(loc, new ItemStack(Material.DIAMOND, 1));
            }
            if ((block.getType() == Material.EMERALD_ORE)) {
                block.getWorld().dropItem(loc, new ItemStack(Material.EMERALD, 1));
            }
            if ((block.getType() == Material.GOLD_ORE)) {
                block.getWorld().dropItem(loc, new ItemStack(Material.GOLD_ORE, 1));
            }
            if ((block.getType() == Material.IRON_ORE)) {
                block.getWorld().dropItem(loc, new ItemStack(Material.IRON_ORE, 1));
            }
            if ((block.getType() == Material.LAPIS_ORE)) {
                block.getWorld().dropItem(loc, new ItemStack(Material.INK_SACK, 6, (byte)4));
            }
            if ((block.getType() == Material.QUARTZ_ORE)) {
                block.getWorld().dropItem(loc, new ItemStack(Material.QUARTZ, 1));
            }
            if ((block.getType() == Material.GLOWING_REDSTONE_ORE)) {
                block.getWorld().dropItem(loc, new ItemStack(Material.REDSTONE, 4));
            }
        }
       
        @EventHandler
        public void onBlockPlace(BlockBreakEvent event) {
            Block b = event.getBlock();
            b.setMetadata("PLACED", new FixedMetadataValue(this.plugin, "something"));
        }
    Thanks in advance!
     
  2. Offline

    Tecno_Wizard

    @Nickname97, the metadata wouldn't be null. It would be an empty list.
     
  3. Offline

    Nickname97

    So this code should run as intended?

    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onBlockPlace(BlockBreakEvent event) {
            block = event.getBlock();
            block.setMetadata("PLACED", new FixedMetadataValue(this.plugin, "something"));
        }
    
        @EventHandler(priority = EventPriority.HIGH)
        public void onBlockBreak(BlockBreakEvent event) {
            block = event.getBlock();
            Location loc = block.getLocation();
            if (!(block.hasMetadata("PLACED"))) {
                if ((block.getType() == Material.STONE)) {
                    block.getWorld().dropItem(loc, new ItemStack(Material.STONE, 1));
                }
                if ((block.getType() == Material.COAL_ORE)) {
                    block.getWorld().dropItem(loc, new ItemStack(Material.COAL, 1));
                }
                if ((block.getType() == Material.DIAMOND_ORE)) {
                    block.getWorld().dropItem(loc, new ItemStack(Material.DIAMOND, 1));
                }
                if ((block.getType() == Material.EMERALD_ORE)) {
                    block.getWorld().dropItem(loc, new ItemStack(Material.EMERALD, 1));
                }
                if ((block.getType() == Material.GOLD_ORE)) {
                    block.getWorld().dropItem(loc, new ItemStack(Material.GOLD_ORE, 1));
                }
                if ((block.getType() == Material.IRON_ORE)) {
                    block.getWorld().dropItem(loc, new ItemStack(Material.IRON_ORE, 1));
                }
                if ((block.getType() == Material.LAPIS_ORE)) {
                    block.getWorld().dropItem(loc, new ItemStack(Material.INK_SACK, 6, (byte)4));
                }
                if ((block.getType() == Material.QUARTZ_ORE)) {
                    block.getWorld().dropItem(loc, new ItemStack(Material.QUARTZ, 1));
                }
                if ((block.getType() == Material.GLOWING_REDSTONE_ORE)) {
                    block.getWorld().dropItem(loc, new ItemStack(Material.REDSTONE, 4));
                }
            }
        }
    Wow I figured it out.

    Line 2:
    Code:
    public void onBlockPlace(BlockBreakEvent event) {
    Should be:
    Code:
    public void onBlockPlace(BlockPlaceEvent event) {
    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Aug 13, 2015
Thread Status:
Not open for further replies.

Share This Page