--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/LockedChest.java 2012/05/28 20:15:38 1806 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/AdvancedChest.java 2012/06/03 18:05:09 1807 @@ -3,10 +3,14 @@ import java.util.HashMap; import java.util.List; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.OfflinePlayer; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.block.Block; @@ -22,22 +26,33 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; +import dk.thoerup.bukkit.hoeruputils.Util; -public class LockedChest implements Listener, CommandExecutor{ + +public class AdvancedChest implements Listener, CommandExecutor{ + + class ItemCount extends TreeMap { + private static final long serialVersionUID = 1L; + }; + + HashMap contentMap = new HashMap(); - HashMap chestMap = new HashMap(); + HashMap chestMap = new HashMap(); HoerupUtilsPlugin plugin; Server server; - public LockedChest(HoerupUtilsPlugin plugin, Runnable r) { + public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) { this.plugin = plugin; server = plugin.getServer(); try { @@ -59,24 +74,116 @@ Player player = (Player) sender; + if (args.length == 0) { + player.sendMessage("Usage:"); + player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer) [player]"); + return true; + } - Block b = player.getTargetBlock(null, 30); - Location loc = b.getLocation(); + + Block b = player.getTargetBlock(null, 30); if (b.getTypeId() != 54) { - player.sendMessage("[LockedChest] Please look at the chest you want to lock"); + player.sendMessage("[Chest] Please look at the chest you want to protect"); return true; } + Location loc = b.getLocation(); Location loc2 = getNeighborChest(loc); - LockedChestBean chest = chestMap.get(loc); + ChestBean chest = chestMap.get(loc); + String cmd = args[0].toLowerCase(); + + if (cmd.equals("status")) { + if (chest != null) { + String mode = ""; + switch (chest.getChestType()) { + case ChestBean.LOCKED: + mode = "locked"; + break; + case ChestBean.SNITCHING: + mode = "snitching"; + break; + default: + mode = "unknown ??"; + } + + player.sendMessage(ChatColor.GREEN + "Chest is a " + mode + " chest owned by " + chest.getOwner()); + player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() ); + } else { + player.sendMessage(ChatColor.GREEN + "The chest is not protected"); + } + return true; + } + + if (cmd.equals("lock") || cmd.equals("snitch")) { + if (chest == null) { + chest = createChest(player.getName(), "", loc); + if (loc2 != null) { + chest.setDoublechest(true); + } + String modeStr = ""; + if (cmd.equals("lock")) { + chest.setChestType( ChestBean.LOCKED); + modeStr = "locked"; + } else { + chest.setChestType( ChestBean.SNITCHING); + modeStr = "snitching"; + } + chest.setModifyPlayers(""); + addChest(loc, chest); + player.sendMessage("Chest is now " + modeStr); + } else { + player.sendMessage("This chest is already protected"); + } + return true; + } + + if (cmd.equals("remove")) { + if (chest != null) { + player.sendMessage("[LockedChest] Removing protection from chest"); + removeChest(loc); + } else { + player.sendMessage("This chest is not protected"); + } + return true; + } + + if (cmd.equals("addplayer") || cmd.equals("removeplayer")) { + if (chest == null) { + player.sendMessage("This chest is not protected"); + return true; + } + if (args.length != 2) { + player.sendMessage("You need to specify which player to add or remove"); + return true; + } + OfflinePlayer p2 = server.getOfflinePlayer(args[1]); + if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { + player.sendMessage("Unknown user: " + args[1] ); + return true; + } + + Set players = Util.stringToSet( chest.getModifyPlayers() ); + if (cmd.equals("addplayer")) { + players.add(args[1]); + } else { + players.remove(args[1]); + } + + chest.setModifyPlayers( Util.setToString(players) ); + plugin.getDatabase().save( chest ); + player.sendMessage("ok"); + return true; + } + + /* 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"); + player.sendMessage("[LockedChest] Chest is already protected"); } return true; @@ -91,7 +198,9 @@ player.sendMessage("[LockedChest] Chest is now locked"); + */ + player.sendMessage("Unknown argument, " + cmd); return true; } @@ -99,18 +208,18 @@ @EventHandler public void onBlockBreak(BlockBreakEvent event) { Location loc = event.getBlock().getLocation(); - LockedChestBean chest = chestMap.get(loc); + ChestBean 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"); + event.getPlayer().sendMessage("[AdvancedChest] The destroyed chest was locked or snitching"); } else { event.setCancelled(true); event.getPlayer().sendMessage("You can't destroy that chest"); } } } - public void addChest(Location loc, LockedChestBean chest) { + public void addChest(Location loc, ChestBean chest) { chestMap.put(loc, chest); if (chest.isDoublechest()) { Location loc2 = getNeighborChest(loc); @@ -123,7 +232,7 @@ } void removeChest(Location loc) { - LockedChestBean chest = chestMap.remove(loc); + ChestBean chest = chestMap.remove(loc); if (chest != null) { if (chest.isDoublechest()){ Location loc2 = getNeighborChest(loc); @@ -134,8 +243,8 @@ } int loadChestsWorker() { - List chestlist = plugin.getDatabase().find( LockedChestBean.class).findList(); - for (LockedChestBean chest : chestlist) { + List chestlist = plugin.getDatabase().find( ChestBean.class).findList(); + for (ChestBean chest : chestlist) { Location loc = getChestLocation(server, chest); chestMap.put(loc, chest); @@ -155,13 +264,13 @@ void loadChests() { int count = loadChestsWorker(); - plugin.getLogger().info("[LockedChest] loaded " + count + " chests"); + plugin.getLogger().info("[AdvancedChest] loaded " + count + " chests"); } - public LockedChestBean createChest(String owner, String description, Location loc) { + public ChestBean createChest(String owner, String description, Location loc) { - LockedChestBean chest = new LockedChestBean(); + ChestBean chest = new ChestBean(); chest.setOwner(owner); chest.setDescription(description); setChestLocation(chest, loc); @@ -170,14 +279,14 @@ } - public void setChestLocation(LockedChestBean chest, Location loc) { + 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, LockedChestBean chest) { + public Location getChestLocation(Server server, ChestBean chest) { World wrld = server.getWorld(chest.getWorld()); return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); } @@ -235,7 +344,7 @@ Location chestloc = getNeighborChest( block.getLocation() ); if (chestloc != null) { - LockedChestBean chest = chestMap.get(chestloc); + ChestBean chest = chestMap.get(chestloc); if (chest != null) { //the neighbor is a locked chest @@ -244,7 +353,7 @@ addChest(chestloc, chest); - event.getPlayer().sendMessage( "[LockedChest] Chest has been expanded" ); + event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); } } @@ -254,7 +363,7 @@ @EventHandler public void onChestExplode(EntityExplodeEvent event) { for (Block b : event.blockList() ) { - LockedChestBean chest = chestMap.get( b.getLocation() ); + ChestBean chest = chestMap.get( b.getLocation() ); if (chest != null) { event.setCancelled( true ); return; @@ -272,11 +381,15 @@ Location loc = b.getLocation(); - LockedChestBean chest = chestMap.get( loc ); + 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 @@ -297,5 +410,144 @@ } } } + + @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 + } + + + 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(); + } 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(); + } + + + plugin.getLogger().info(msg); + plugin.getMessageWrapper().sendMessage("system", owner, msg); + } + + } + + + } + } + + ItemCount countItems(ItemStack[] input) { + ItemCount output = new ItemCount(); + for (int i=0; i