--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/LockedChest.java 2012/05/28 15:36:28 1805 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/AdvancedChest.java 2012/11/06 16:49:06 1869 @@ -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; @@ -20,23 +24,36 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockBurnEvent; 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 { @@ -58,24 +75,174 @@ 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); - 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() ); + player.sendMessage(ChatColor.GREEN + "Comment: " + chest.getComment() ); + } 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(""); + chest.setComment(""); + 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("This chest is not protected"); + return true; + } + if (! chest.getOwner().equals( player.getName() ) ) { + player.sendMessage("You can not remove lock from a chest you don't own"); + return true; + } + + player.sendMessage("[LockedChest] Removing protection from chest"); + removeChest(loc); + + return true; + } + + if (cmd.equals("setowner")) { + if (chest == null) { + player.sendMessage("This chest is not protected"); + return true; + } + if (! chest.getOwner().equals( player.getName() ) ) { + player.sendMessage("You can not set new owner of a chest you dont own"); + return true; + } + if (args.length != 2) { + player.sendMessage("You need to specify which player should own this chest"); + return true; + } + OfflinePlayer p2 = server.getOfflinePlayer(args[1]); + if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { + player.sendMessage("Unknown user: " + args[1] ); + return true; + } + chest.setOwner( p2.getName() ); + plugin.getDatabase().save( chest ); + player.sendMessage("ok"); + return true; + } + + if (cmd.equals("addplayer") || cmd.equals("removeplayer")) { + if (chest == null) { + player.sendMessage("This chest is not protected"); + return true; + } + if (! chest.getOwner().equals( player.getName() ) ) { + player.sendMessage("You can not add/remove players from a chest you don't own"); + 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(p2.getName()); + } else { + players.remove(p2.getName()); + } + + chest.setModifyPlayers( Util.setToString(players) ); + plugin.getDatabase().save( chest ); + player.sendMessage("ok"); + return true; + } + if (cmd.equals("comment")) { + if (chest == null) { + player.sendMessage("This chest is not protected"); + return true; + } + if (! chest.getOwner().equals( player.getName() ) ) { + player.sendMessage("You can not comment a chest you don't own"); + return true; + } + + StringBuilder sb = new StringBuilder(); + for (int i=1; i 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); @@ -154,13 +324,13 @@ void loadChests() { int count = loadChestsWorker(); - plugin.getLogger().info("[LockedChest] loaded " + count + " chests"); + server.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); @@ -169,14 +339,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()); } @@ -234,7 +404,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 @@ -243,12 +413,33 @@ addChest(chestloc, chest); - event.getPlayer().sendMessage( "[LockedChest] Chest has been expanded" ); + event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); } } } - + + + @EventHandler + public void onChestExplode(EntityExplodeEvent event) { + for (Block b : event.blockList() ) { + ChestBean chest = chestMap.get( b.getLocation() ); + if (chest != null) { + server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() ); + event.setCancelled( true ); + return; + } + } + } + + @EventHandler + public void onChestBurn(BlockBurnEvent event) { + ChestBean chest = chestMap.get( event.getBlock().getLocation() ); + if (chest != null) { + server.getLogger().info( "[AdvancedChest] prevented a fire from destrying chest owned by " + chest.getOwner() ); + event.setCancelled( true); + } + } // prevent a user from opening a chest @EventHandler @@ -260,30 +451,177 @@ 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 } - 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 - } - } + Set players = Util.stringToSet( chest.getModifyPlayers() ); + if ( players.contains(player.getName()) ) { + return; //this player is on the whitelist so he may open } - + + server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner() ); player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); event.setCancelled(true); } } } + + @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 = Util.stringToSet( chest.getModifyPlayers() ); + 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() ); + + 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(); + } + + + server.getLogger().info( "[AdvancedChest]" + msg); + plugin.getMessageWrapper().sendMessage("system", owner, msg); + } + + } + + + } + } + + ItemCount countItems(ItemStack[] input) { + ItemCount output = new ItemCount(); + for (int i=0; i