package dk.thoerup.bukkit.hoeruputils.chests; import java.util.HashMap; import java.util.List; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.Chest; import org.bukkit.block.DoubleChest; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.InventoryHolder; import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; public class LockedChest implements Listener, CommandExecutor{ HashMap chestMap = new HashMap(); HoerupUtilsPlugin plugin; Server server; public LockedChest(HoerupUtilsPlugin plugin, Runnable r) { this.plugin = plugin; server = plugin.getServer(); try { loadChests(); } catch (Exception e) { e.printStackTrace(); //r.run(); loadChests(); } } @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (! (sender instanceof Player) ) { sender.sendMessage("this is not a console command!"); return true; } Player player = (Player) sender; Block b = player.getTargetBlock(null, 30); Location loc = b.getLocation(); if (b.getTypeId() != 54) { player.sendMessage("[LockedChest] Please look at the chest you want to lock"); return true; } Location loc2 = getNeighborChest(loc); LockedChestBean chest = chestMap.get(loc); if (chest != null) { if (chest.getOwner().equals(player.getName())) { player.sendMessage("[LockedChest] Removing lock from chest"); removeChest(loc); } else { player.sendMessage("[LockedChest] Chest is already locked"); } return true; } chest = createChest(player.getName(), "", loc); if (loc2 != null) { chest.setDoublechest(true); } addChest(loc, chest); player.sendMessage("[LockedChest] Chest is now locked"); return true; } @EventHandler public void onBlockBreak(BlockBreakEvent event) { Location loc = event.getBlock().getLocation(); LockedChestBean chest = chestMap.get(loc); if (chest != null) { if (chest.getOwner().equals(event.getPlayer().getName())) { removeChest(loc); event.getPlayer().sendMessage("[LockedChest] The destroyed chest was locked"); } else { event.setCancelled(true); event.getPlayer().sendMessage("You can't destroy that chest"); } } } public void addChest(Location loc, LockedChestBean chest) { chestMap.put(loc, chest); if (chest.isDoublechest()) { Location loc2 = getNeighborChest(loc); chestMap.put(loc2, chest); } plugin.getDatabase().save(chest); reloadChests(); } void removeChest(Location loc) { LockedChestBean chest = chestMap.remove(loc); if (chest != null) { if (chest.isDoublechest()){ Location loc2 = getNeighborChest(loc); chestMap.remove(loc2); } plugin.getDatabase().delete(chest); } } int loadChestsWorker() { List chestlist = plugin.getDatabase().find( LockedChestBean.class).findList(); for (LockedChestBean chest : chestlist) { Location loc = getChestLocation(server, chest); chestMap.put(loc, chest); if (chest.isDoublechest()) { Location loc2 = getNeighborChest(loc); chestMap.put(loc2, chest); } } return chestlist.size(); } void reloadChests() { chestMap.clear(); loadChestsWorker(); } void loadChests() { int count = loadChestsWorker(); plugin.getLogger().info("[LockedChest] loaded " + count + " chests"); } public LockedChestBean createChest(String owner, String description, Location loc) { LockedChestBean chest = new LockedChestBean(); chest.setOwner(owner); chest.setDescription(description); setChestLocation(chest, loc); return chest; } public void setChestLocation(LockedChestBean chest, Location loc) { chest.setWorld( loc.getWorld().getName() ); chest.setX( loc.getBlockX() ); chest.setY( loc.getBlockY() ); chest.setZ( loc.getBlockZ() ); } public Location getChestLocation(Server server, LockedChestBean chest) { World wrld = server.getWorld(chest.getWorld()); return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); } /* void saveChests() { }*/ Location getNeighborChest(Location loc) { World world = loc.getWorld(); Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() ); if (world.getBlockAt(target).getType() == Material.CHEST ) return target; target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() ); if (world.getBlockAt(target).getType() == Material.CHEST ) return target; target = new Location(world, loc.getX(), loc.getY(), loc.getZ() +1); if (world.getBlockAt(target).getType() == Material.CHEST ) return target; target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1); if (world.getBlockAt(target).getType() == Material.CHEST ) return target; return null; } Location getChestLocation(InventoryHolder holder) { Location loc; if ( holder instanceof Chest) { loc = ( (Chest)holder).getLocation(); } else { loc = ( (DoubleChest)holder).getLocation(); } loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5 loc.setZ( loc.getBlockZ() ); // -- // -- return loc; } @EventHandler public void onChestPlaced(BlockPlaceEvent event) { Block block = event.getBlock(); if (block.getType() != Material.CHEST) { return; } Location chestloc = getNeighborChest( block.getLocation() ); if (chestloc != null) { LockedChestBean chest = chestMap.get(chestloc); if (chest != null) { //the neighbor is a locked chest chest.setDoublechest(true); addChest(chestloc, chest); event.getPlayer().sendMessage( "[LockedChest] Chest has been expanded" ); } } } @EventHandler public void onChestExplode(EntityExplodeEvent event) { for (Block b : event.blockList() ) { LockedChestBean chest = chestMap.get( b.getLocation() ); if (chest != null) { event.setCancelled( true ); return; } } } // prevent a user from opening a chest @EventHandler public void onChestInteract(PlayerInteractEvent event) { if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { Block b = event.getClickedBlock(); if (b.getType() == Material.CHEST) { Location loc = b.getLocation(); LockedChestBean chest = chestMap.get( loc ); if (chest == null) { return; //chest not surveyed by this plugin } Player player = (Player) event.getPlayer(); if (player.getName().equals(chest.getOwner() )) { return; //chest is opened by it's owner } if (chest.getModifyPlayers() != null && chest.getModifyPlayers().length()>0) { String modplayers[] = chest.getModifyPlayers().split(","); for (String p : modplayers) { if ( player.getName().equals(p) ) { return; //this player is on the whitelist so he may open } } } player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); event.setCancelled(true); } } } }