--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/AdvancedChest.java 2013/03/22 13:57:52 1948 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/AdvancedChest.java 2013/03/22 19:38:26 1949 @@ -43,16 +43,16 @@ class ItemCount extends TreeMap { private static final long serialVersionUID = 1L; }; - + HashMap contentMap = new HashMap(); - - + + HashMap chestMap = new HashMap(); - + HoerupUtilsPlugin plugin; Server server; - + public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) { this.plugin = plugin; server = plugin.getServer(); @@ -64,7 +64,7 @@ loadChests(); } } - + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -72,29 +72,32 @@ sender.sendMessage("this is not a console command!"); return true; } - + Player player = (Player) sender; - + if (args.length == 0) { player.sendMessage("Usage:"); player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer|setowner|comment) [player]"); return true; } - - + + Block b = player.getTargetBlock(null, 30); - - if (b.getTypeId() != 54) { - player.sendMessage("[Chest] Please look at the chest you want to protect"); + + if (b.getTypeId() != 54 && b.getTypeId() != 154) { + player.sendMessage("[Chest] Please look at the chest/hopper you want to protect"); return true; } - + Location loc = b.getLocation(); - Location loc2 = getNeighborChest(loc); - + Location loc2 = null; + if ( b.getTypeId() == 54) { //dont find neighbours for Hoppers + loc2 = getNeighborChest(loc); + } + ChestBean chest = chestMap.get(loc); String cmd = args[0].toLowerCase(); - + if (cmd.equals("status")) { if (chest != null) { String mode = ""; @@ -108,7 +111,7 @@ default: mode = "unknown ??"; } - + player.sendMessage(ChatColor.GREEN + "Chest is a " + mode + " chest owned by " + chest.getOwner()); player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() ); player.sendMessage(ChatColor.GREEN + "Comment: " + chest.getComment() ); @@ -117,7 +120,7 @@ } return true; } - + if (cmd.equals("lock") || cmd.equals("snitch")) { if (chest == null) { chest = createChest(player.getName(), "", loc); @@ -142,7 +145,7 @@ } return true; } - + if (cmd.equals("remove")) { if (chest == null) { player.sendMessage("This chest is not protected"); @@ -188,7 +191,7 @@ player.sendMessage("ok"); return true; } - + if (cmd.equals("addplayer") || cmd.equals("removeplayer")) { if (chest == null) { player.sendMessage("This chest is not protected"); @@ -208,14 +211,14 @@ player.sendMessage("Unknown user: " + args[1] ); return true; } - + Set players = Util.stringToSet( chest.getModifyPlayers() ); if (cmd.equals("addplayer")) { players.add(p2.getName()); } else { players.remove(p2.getName()); } - + chest.setModifyPlayers( Util.setToString(players) ); plugin.getDatabase().save( chest ); player.sendMessage("ok"); @@ -244,7 +247,7 @@ return true; } - + /* if (chest != null) { if (chest.getOwner().equals(player.getName())) { @@ -253,26 +256,26 @@ } else { player.sendMessage("[LockedChest] Chest is already protected"); } - + return true; } - + chest = createChest(player.getName(), "", loc); if (loc2 != null) { chest.setDoublechest(true); } - + addChest(loc, chest); - - + + player.sendMessage("[LockedChest] Chest is now locked"); - */ - + */ + player.sendMessage("Unknown argument, " + cmd); - + return true; } - + @EventHandler public void onBlockBreak(BlockBreakEvent event) { Location loc = event.getBlock().getLocation(); @@ -295,11 +298,11 @@ chestMap.put(loc2, chest); } plugin.getDatabase().save(chest); - + reloadChests(); - + } - + void removeChest(Location loc) { ChestBean chest = chestMap.remove(loc); if (chest != null) { @@ -310,13 +313,13 @@ plugin.getDatabase().delete(chest); } } - + int loadChestsWorker() { List chestlist = plugin.getDatabase().find( ChestBean.class).findList(); for (ChestBean chest : chestlist) { Location loc = getChestLocation(server, chest); chestMap.put(loc, chest); - + if (chest.isDoublechest()) { Location loc2 = getNeighborChest(loc); chestMap.put(loc2, chest); @@ -325,20 +328,20 @@ return chestlist.size(); } - + void reloadChests() { chestMap.clear(); loadChestsWorker(); } - + void loadChests() { int count = loadChestsWorker(); server.getLogger().info("[AdvancedChest] loaded " + count + " chests"); } - - + + public ChestBean createChest(String owner, String description, Location loc) { - + ChestBean chest = new ChestBean(); chest.setOwner(owner); chest.setDescription(description); @@ -346,33 +349,33 @@ return chest; } - - + + public void setChestLocation(ChestBean 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, ChestBean 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; @@ -384,11 +387,11 @@ 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) { @@ -396,36 +399,36 @@ } 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) { - ChestBean chest = chestMap.get(chestloc); - - if (chest != null) { //the neighbor is a locked chest - - - chest.setDoublechest(true); - addChest(chestloc, chest); - - - event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); - } - - } + if (chestloc == null) + return; + + ChestBean chest = chestMap.get(chestloc); + + if (chest == null)//the neighbor is not a locked chest + return; + + + chest.setDoublechest(true); + addChest(chestloc, chest); + + + event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); } @@ -449,31 +452,31 @@ event.setCancelled( true); } } - + // 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(); - + ChestBean chest = chestMap.get( loc ); if (chest == null) { return; //chest not surveyed by this plugin } - + if (chest.getChestType() != ChestBean.LOCKED ) { return; //this is not a locked chests } - + Player player = (Player) event.getPlayer(); if (player.getName().equals(chest.getOwner() )) { return; //chest is opened by it's owner } - + Set players = chest.getModifyPlayersSet() ; if ( players.contains(player.getName()) ) { return; //this player is on the whitelist so he may open @@ -489,132 +492,132 @@ @EventHandler public void onChestOpen(InventoryOpenEvent event) { - + if (! (event.getPlayer() instanceof Player)) { return; } - + InventoryHolder holder = event.getInventory().getHolder(); if (holder instanceof Chest || holder instanceof DoubleChest) { Location loc = getChestLocation(holder); - + ChestBean chest = chestMap.get( loc ); if (chest == null) { return; //chest not surveyed by this plugin } - + if (chest.getChestType() != ChestBean.SNITCHING) { return; // not a snitching chest } - - + + Player player = (Player) event.getPlayer(); if (player.getName().equals(chest.getOwner() )) { return; //chest is owned by it's own player } - + Set players = chest.getModifyPlayersSet(); if ( players.contains(player.getName()) ) { return; //this player is on the whitelist so he may open } - - + + server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " opened a snitching chest owned by " + chest.getOwner() + chest.getCommentString() ); ItemCount contents = countItems( event.getInventory().getContents() ); - + contentMap.put(player.getName(), contents ); } } - + @EventHandler public void onChestClose(InventoryCloseEvent event) { if (! (event.getPlayer() instanceof Player)) { return; } - - + + InventoryHolder holder = event.getInventory().getHolder(); if (holder instanceof Chest || holder instanceof DoubleChest) { Location loc = getChestLocation(holder); ChestBean chest = chestMap.get(loc); - + if (chest == null) { //chest was not a snitching chest return; } - + if (chest.getChestType() != ChestBean.SNITCHING) { return; // not a snitching chest } - - + + OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); - - + + Player player = (Player) event.getPlayer(); - + ItemCount savedContent = contentMap.get( player.getName() ); - + if (savedContent == null) { return; } - + contentMap.remove( player.getName() ); - + ItemCount content = countItems( event.getInventory().getContents() ); - + Set combinedKeyset = new TreeSet(); combinedKeyset.addAll( savedContent.keySet() ); combinedKeyset.addAll( content.keySet() ); - + for (Integer item : combinedKeyset ) { Integer savedcount = savedContent.get(item); Integer count = content.get(item); - + if (savedcount == null) savedcount = 0; if (count == null) count = 0; - - + + int diff = Math.abs( savedcount - count); - + if (diff > 0) { String material = Material.getMaterial(item).name(); String msg = null; - + if (count > savedcount) { msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ() + chest.getCommentString(); } else { //(count < savedcount) msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ() + chest.getCommentString(); } - - + + server.getLogger().info( "[AdvancedChest]" + msg); plugin.getMessageWrapper().sendMessage("system", owner, msg); } - + } - - + + } } - + ItemCount countItems(ItemStack[] input) { ItemCount output = new ItemCount(); for (int i=0; i