Code:java @EventHandler public void chestOpen(PlayerInteractEvent e) { Player p = e.getPlayer(); Action action = e.getAction(); if(action == action.RIGHT_CLICK_BLOCK){ if(e.getClickedBlock() == plugin.savedBlock){ if(plugin.savedBlock.getLocation() == plugin.locs){ Bukkit.broadcastMessage(ChatColor.GREEN+"The chest has been found by "+p.getName()); } } } } } The exception is being caused by line 6.
Code:java package me.gerov.trea; import java.io.File;import java.util.HashMap;import java.util.logging.Logger; import me.gerov.listener.PListener; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Location;import org.bukkit.Material;import org.bukkit.World;import org.bukkit.block.Block;import org.bukkit.block.BlockState;import org.bukkit.block.Chest;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.inventory.Inventory;import org.bukkit.inventory.ItemStack;import org.bukkit.plugin.PluginDescriptionFile;import org.bukkit.plugin.PluginManager;import org.bukkit.plugin.java.JavaPlugin;import org.bukkit.scheduler.BukkitRunnable; public class main extends JavaPlugin { public final Logger logger = Logger.getLogger("Minecraft"); public PluginManager pm; public Location locs; public Block savedBlock; @Override public void onDisable(){ PluginDescriptionFile pdffile = this.getDescription(); this.logger.info(pdffile.getName()+ChatColor.RED+" has been disabled!"); this.getConfig(); } @Override public void onEnable(){ final main plugin = this; PluginDescriptionFile pdffile = this.getDescription(); this.logger.info(pdffile.getName()+ChatColor.RED+" has been enabled!"); File file = new File(getDataFolder()+File.separator+"config.yml"); if(!file.exists()){ this.getLogger().info("Making config!"); this.getConfig().addDefault("time_in_seconds_between_chest_spawn", 10000); this.getConfig().addDefault("max_x_distance", 500); this.getConfig().addDefault("max_z_distance", 500); this.getConfig().addDefault("world", "world"); this.getConfig().addDefault("chestsSpawnNaturally", false); this.getConfig().options().copyDefaults(true); this.saveConfig(); final int time = this.getConfig().getInt("time_in_seconds_between_chest_spawn"); final int xDis = this.getConfig().getInt("max_x_distance"); final int zDis = this.getConfig().getInt("max_z_distance"); final String worldS = this.getConfig().getString("world"); final boolean cn = this.getConfig().getBoolean("chestsSpawnNaturally"); final World world = Bukkit.getWorld(worldS); if(cn == true){ new BukkitRunnable() { int timer = 0; int nowtime = time*20; public void run() { if(timer == time*20){ timer = 0; Location loc = new Location(world, 0,Math.random()*10,0); loc.setX( world.getSpawnLocation().getX() + Math.random() * xDis * 2 - xDis); loc.setZ( world.getSpawnLocation().getZ() + Math.random() * zDis * 2 - zDis); loc.setY( world.getHighestBlockAt(loc.getBlockX(), loc.getBlockZ() ).getY() ); Location savedLocation = null; HashMap<Location, Location> locs = new HashMap<Location, Location>(); locs.put(loc, savedLocation); loc.getBlock().setType(Material.CHEST); Block chest = loc.getBlock(); savedBlock = null; HashMap<Block, Block> lchest = new HashMap<Block, Block>(); lchest.put(chest, savedBlock); BlockState state = chest.getState(); Inventory inv = ((Chest) state).getInventory(); for(int x = 0; x <= 5;x++){ int random = (int) (Math.random()*3); if(random <= 1){ ItemStack strings = new ItemStack(Material.STRING,5); inv.addItem(strings); }else if(random <= 2){ ItemStack diamond = new ItemStack(Material.DIAMOND,5); inv.addItem(diamond); }else if(random <= 3){ ItemStack diamond = new ItemStack(Material.APPLE,10); inv.addItem(diamond); } }Bukkit.getServer().broadcastMessage(ChatColor.DARK_RED +"A chest has spawned at "+ChatColor.AQUA+loc.getX()+" "+ChatColor.AQUA+loc.getY()+" "+ChatColor.AQUA+loc.getZ()+"!"); }else if(timer != nowtime){ timer++; } } }.runTaskTimer(this, 0, 1); }else if(file.exists()){ final int time1 = this.getConfig().getInt("time_in_seconds_between_chest_spawn"); final int xDis1 = this.getConfig().getInt("max_x_distance"); final int yDis = this.getConfig().getInt("max_y_distance"); final int zDis1 = this.getConfig().getInt("max_z_distance"); final String worldS1 = this.getConfig().getString("world"); final World world1 = Bukkit.getWorld(worldS1); new BukkitRunnable() { int timer = 0; int nowtime = time1*20; public void run() { if(timer == time1*20){ timer = 0; Location loc = new Location(world1, 0,Math.random()*10,0); loc.setX( world1.getSpawnLocation().getX() + Math.random() * xDis1 * 2 - xDis1); // This get a Random with a MaxRange loc.setZ( world1.getSpawnLocation().getZ() + Math.random() * zDis1 * 2 - zDis1); loc.setY( world1.getHighestBlockAt(loc.getBlockX(), loc.getBlockZ() ).getY() ); loc.getBlock().setType(Material.CHEST); Block chest = loc.getBlock(); pm = getServer().getPluginManager(); PListener plis = new PListener(); pm.registerEvents(plis, plugin); savedBlock = chest; locs = loc; BlockState state = chest.getState(); Inventory inv = ((Chest) state).getInventory(); for(int x = 0; x <= 5;x++){ int random = (int) (Math.random()*3); if(random <= 1){ ItemStack strings = new ItemStack(Material.STRING,5); inv.addItem(strings); }else if(random <= 2){ ItemStack diamond = new ItemStack(Material.DIAMOND,5); inv.addItem(diamond); }else if(random <= 3){ ItemStack diamond = new ItemStack(Material.APPLE,10); inv.addItem(diamond); } }Bukkit.getServer().broadcastMessage(ChatColor.DARK_RED +"A chest has spawned at "+ChatColor.AQUA+loc.getX()+" "+ChatColor.AQUA+loc.getY()+" "+ChatColor.AQUA+loc.getZ()+"!"); }else if(timer != nowtime){ timer++; } } }.runTaskTimer(this, 0, 1); } } } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){ final int xDis = this.getConfig().getInt("max_x_distance"); final int zDis = this.getConfig().getInt("max_z_distance"); if(sender instanceof Player){ Player player = (Player) sender; if(player.hasPermission("treasure.spawnchest")){ if(commandLabel.equalsIgnoreCase("spawnchest")){ Location loc = new Location(((Player) sender).getWorld(), 0,Math.random()*10,0); loc.setX( ((Player) sender).getWorld().getSpawnLocation().getX() + Math.random() * xDis * 2 - xDis); // This get a Random with a MaxRange loc.setZ( ((Player) sender).getWorld().getSpawnLocation().getZ() + Math.random() * zDis * 2 - zDis); loc.setY( ((Player) sender).getWorld().getHighestBlockAt(loc.getBlockX(), loc.getBlockZ() ).getY() ); loc.getBlock().setType(Material.CHEST); Block chest = loc.getBlock(); BlockState state = chest.getState(); Inventory inv = ((Chest) state).getInventory(); for(int x = 0; x <= 5; x++){ int random = (int)(Math.random() * 10) + 1; if(random == 1){ ItemStack strings = new ItemStack(Material.STRING,5); inv.addItem(strings); }else if(random == 2){ ItemStack diamond = new ItemStack(Material.DIAMOND,5); inv.addItem(diamond); }else if(random == 3){ ItemStack diamond = new ItemStack(Material.APPLE,10); inv.addItem(diamond); }else if(random == 4){ ItemStack gold = new ItemStack(Material.GOLD_INGOT,5); inv.addItem(gold); }else if(random == 5){ ItemStack dirt = new ItemStack(Material.DIRT,10); inv.addItem(dirt); }else if(random == 6){ ItemStack glowStone = new ItemStack(Material.GLOWSTONE,5); inv.addItem(glowStone); }else if(random == 7){ ItemStack gApple = new ItemStack(Material.GOLDEN_APPLE,1); inv.addItem(gApple); }else if(random == 8){ ItemStack ironPick = new ItemStack(Material.IRON_PICKAXE,1); inv.addItem(ironPick); }else if(random == 9){ ItemStack diaHelm = new ItemStack(Material.DIAMOND_HELMET); inv.addItem(diaHelm); }else if(random == 10){ ItemStack stone = new ItemStack(Material.STONE,32); inv.addItem(stone); } pm = getServer().getPluginManager(); PListener plis = new PListener(); final main plugin = this; pm.registerEvents(plis, plugin); savedBlock = chest; locs = loc;}Bukkit.getServer().broadcastMessage(ChatColor.DARK_RED +"A chest has spawned at "+ChatColor.AQUA+loc.getX()+" "+ChatColor.AQUA+loc.getY()+" "+ChatColor.AQUA+loc.getZ()+"! Spawned by "+player.getName()+"."); } } } return true;}} I am testing with the command, ignore the 2 under the onEnable thing. Ehm... EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Gerov None of us are going to guess what your issue is for you ... You have to give us your full listener class, explain when the exception occurs .. Have you tried debugging? If you can't input effort, you can't accept effort in our responses.
well, you get a nullpointer cause you are not checking if the item is not null. use in the begin if(e.getitem != null). Code:java @EventHandler public void chestOpen(PlayerInteractEvent e) { Player p = e.getPlayer();if(e.getitem != null){ Action action = e.getAction(); if(action == action.RIGHT_CLICK_BLOCK){ if(e.getClickedBlock() == plugin.savedBlock){ if(plugin.savedBlock.getLocation() == plugin.locs){ Bukkit.broadcastMessage(ChatColor.GREEN+"The chest has been found by "+p.getName()); }} } } } } i hope this hellped a little bit, else sorry.
No one has seemed to address it yet, so I'll just tell you that your "plugin" variable is null (I don't even see you passing it upon construction of PListener) .getItem() is irrelevant (not involved with the line throwing the npe at all The event isn't null Neither .getClickedBlock() nor .savedBlock is dereferenced here, so neither would throw an NPE. Also, you should note some of the basic java conventions (you don't need to compare a boolean value to true/false, classes should start with a capital, etc).