Solved NullPointerException on BlockPlace

Discussion in 'Plugin Development' started by AngryCupcake274, Mar 26, 2015.

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

    AngryCupcake274

    Hello all,
    I am creating a plugin that checks if the player places a block and logs the X, Y, and Z coordinates. The problem is that sometimes it throws a NullPointerException. I have no clue why, and that's why I've called upon the Bukkit forum people to help!

    Here is the code:
    Code:
    package me.BioBen.SpecialTools.Listeners;
    
    import me.BioBen.SpecialTools.Main;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    public class BlockPlaceListener implements Listener {
       
        // variables for getting information from other classes
        private final Main plugin;
       
        // variables for class testing
        public int test = 1;
       
       
        public BlockPlaceListener(Main plugin) {
           
            this.plugin = plugin;
           
        }
       
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
           
            /*
             * top of cuboid
             * 1 2 3
             * 4 5 6
             * 7 8 9
             */
            Location block1 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() + 1, e.getBlockPlaced().getY() + 1, e.getBlockPlaced().getZ() - 1);
            Location block2 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() + 1, e.getBlockPlaced().getY() + 1, e.getBlockPlaced().getZ());
            Location block3 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() + 1, e.getBlockPlaced().getY() + 1, e.getBlockPlaced().getZ() + 1);
            Location block4 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX(), e.getBlockPlaced().getY() + 1, e.getBlockPlaced().getZ() - 1);
            Location block5 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX(), e.getBlockPlaced().getY() + 1, e.getBlockPlaced().getZ());
            Location block6 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX(), e.getBlockPlaced().getY() + 1, e.getBlockPlaced().getZ() + 1);
            Location block7 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() - 1, e.getBlockPlaced().getY() + 1, e.getBlockPlaced().getZ() - 1);
            Location block8 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() - 1, e.getBlockPlaced().getY() + 1, e.getBlockPlaced().getZ());
            Location block9 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() - 1, e.getBlockPlaced().getY() + 1, e.getBlockPlaced().getZ() + 1);
    
            /*
             * bottom of cuboid
             * 10 11 12
             * 13 14 15
             * 16 17 18
             */
            Location block10 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() + 1, e.getBlockPlaced().getY() - 1, e.getBlockPlaced().getZ() - 1);
            Location block11 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() + 1, e.getBlockPlaced().getY() - 1, e.getBlockPlaced().getZ());
            Location block12 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() + 1, e.getBlockPlaced().getY() - 1, e.getBlockPlaced().getZ() + 1);
            Location block13 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX(), e.getBlockPlaced().getY() - 1, e.getBlockPlaced().getZ() - 1);
            Location block14 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX(), e.getBlockPlaced().getY() - 1, e.getBlockPlaced().getZ());
            Location block15 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX(), e.getBlockPlaced().getY() - 1, e.getBlockPlaced().getZ() + 1);
            Location block16 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() - 1, e.getBlockPlaced().getY() - 1, e.getBlockPlaced().getZ() - 1);
            Location block17 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() - 1, e.getBlockPlaced().getY() - 1, e.getBlockPlaced().getZ());
            Location block18 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() - 1, e.getBlockPlaced().getY() - 1, e.getBlockPlaced().getZ() + 1);
    
            /*
             * side 1 of cuboid
             * XX XX XX
             * 19 20 21
             * XX XX XX
             */
            Location block19 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() - 1, e.getBlockPlaced().getY(), e.getBlockPlaced().getZ() - 1);
            Location block20 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() - 1, e.getBlockPlaced().getY(), e.getBlockPlaced().getZ());
            Location block21 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() - 1, e.getBlockPlaced().getY(), e.getBlockPlaced().getZ() + 1);
    
            /*
             * side 2 of cuboid
             * XX XX XX
             * XX 22 23
             * XX XX XX
             */
            Location block22 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX(), e.getBlockPlaced().getY(), e.getBlockPlaced().getZ() + 1);
            Location block23 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() + 1, e.getBlockPlaced().getY(), e.getBlockPlaced().getZ() + 1);
    
            /*
             * side 3 of cuboid
             * XX XX XX
             * XX 24 25
             * XX XX XX
             */
            Location block24 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() + 1, e.getBlockPlaced().getY(), e.getBlockPlaced().getZ());
            Location block25 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX() + 1, e.getBlockPlaced().getY(), e.getBlockPlaced().getZ() - 1);
    
            /*
             * side 4 of cuboid
             * XX XX XX
             * XX 26 XX
             * XX XX XX
             */
            Location block26 = new Location(e.getBlockPlaced().getWorld(), e.getBlockPlaced().getX(), e.getBlockPlaced().getY(), e.getBlockPlaced().getZ() - 1);
           
            Block b1 = block1.getBlock();
            Block b2 = block2.getBlock();
            Block b3 = block3.getBlock();
            Block b4 = block4.getBlock();
            Block b5 = block5.getBlock();
            Block b6 = block6.getBlock();
            Block b7 = block7.getBlock();
            Block b8 = block8.getBlock();
            Block b9 = block9.getBlock();
            Block b10 = block10.getBlock();
            Block b11 = block11.getBlock();
            Block b12 = block12.getBlock();
            Block b13 = block13.getBlock();
            Block b14 = block14.getBlock();
            Block b15 = block15.getBlock();
            Block b16 = block16.getBlock();
            Block b17 = block17.getBlock();
            Block b18 = block18.getBlock();
            Block b19 = block19.getBlock();
            Block b20 = block20.getBlock();
            Block b21 = block21.getBlock();
            Block b22 = block22.getBlock();
            Block b23 = block23.getBlock();
            Block b24 = block24.getBlock();
            Block b25 = block25.getBlock();
            Block b26 = block26.getBlock();
           
           
            Block[] blocklist = new Block[26];
            blocklist[0] = b1;
            blocklist[1] = b2;
            blocklist[2] = b3;
            blocklist[3] = b4;
            blocklist[4] = b5;
            blocklist[5] = b6;
            blocklist[6] = b7;
            blocklist[7] = b8;
            blocklist[8] = b9;
            blocklist[9] = b10;
            blocklist[10] = b11;
            blocklist[11] = b12;
            blocklist[12] = b13;
            blocklist[13] = b14;
            blocklist[14] = b15;
            blocklist[15] = b16;
            blocklist[16] = b17;
            blocklist[17] = b18;
            blocklist[18] = b19;
            blocklist[19] = b20;
            blocklist[20] = b21;
            blocklist[21] = b22;
            blocklist[22] = b23;
            blocklist[23] = b24;
            blocklist[24] = b25;
            blocklist[25] = b26;
           
            for (Block block : blocklist) {
               
                if (block.getType().equals(Material.COAL_ORE) ||
                        block.getType().equals(Material.IRON_ORE) ||
                        block.getType().equals(Material.GOLD_ORE) ||
                        block.getType().equals(Material.DIAMOND_ORE) ||
                        block.getType().equals(Material.EMERALD_ORE) ||
                        block.getType().equals(Material.REDSTONE_ORE) ||
                        block.getType().equals(Material.GLOWING_REDSTONE_ORE) ||
                        block.getType().equals(Material.LAPIS_ORE) ||
                        block.getType().equals(Material.QUARTZ_ORE) ||
                        block.getType().equals(Material.COAL_BLOCK) ||
                        block.getType().equals(Material.IRON_BLOCK) ||
                        block.getType().equals(Material.GOLD_BLOCK) ||
                        block.getType().equals(Material.DIAMOND_BLOCK) ||
                        block.getType().equals(Material.EMERALD_BLOCK) ||
                        block.getType().equals(Material.REDSTONE_BLOCK) ||
                        block.getType().equals(Material.LAPIS_BLOCK) ||
                        block.getType().equals(Material.QUARTZ_BLOCK)) {
                   
                    plugin.blocklistx.add(e.getBlockPlaced().getX());
                    plugin.blocklisty.add(e.getBlockPlaced().getY());
                    plugin.blocklistz.add(e.getBlockPlaced().getZ());
                   
                    plugin.console.sendMessage(ChatColor.GREEN + Integer.toString(plugin.blocklistx.size()));
                    plugin.console.sendMessage(ChatColor.GREEN + Integer.toString(plugin.blocklisty.size()));
                    plugin.console.sendMessage(ChatColor.GREEN + Integer.toString(plugin.blocklistz.size()));
                   
                }
               
            }
           
            plugin.console.sendMessage(ChatColor.RED + Integer.toString(e.getBlockPlaced().getX()));
            plugin.console.sendMessage(ChatColor.RED + Integer.toString(e.getBlockPlaced().getY()));
            plugin.console.sendMessage(ChatColor.RED + Integer.toString(e.getBlockPlaced().getZ()));
           
        }
    }
    
     
  2. Offline

    teej107

Thread Status:
Not open for further replies.

Share This Page