--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java 2012/04/03 20:30:41 1770 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java 2012/04/05 20:40:38 1781 @@ -2,6 +2,7 @@ import java.util.HashMap; +import java.util.List; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; @@ -21,6 +22,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.inventory.InventoryHolder; @@ -35,17 +37,22 @@ HashMap contentMap = new HashMap(); - HashMap chestMap = new HashMap(); + HashMap chestMap = new HashMap(); HoerupUtilsPlugin plugin; Server server; - public SnitchingChest(HoerupUtilsPlugin plugin) { + public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) { this.plugin = plugin; server = plugin.getServer(); - - loadChests(); + try { + loadChests(); + } catch (Exception e) { + e.printStackTrace(); + //r.run(); + loadChests(); + } } @@ -69,15 +76,14 @@ Location loc2 = getNeighborChest(loc); - String owner = chestMap.get(loc); - if (owner != null) { - if (owner.equals(player.getName())) { + SnitchingChestBean chest = chestMap.get(loc); + if (chest != null) { + if (chest.getOwner().equals(player.getName())) { player.sendMessage("[SnitchingChest] Removing surveillance from chest"); - chestMap.remove(loc); + removeChest(loc); if (loc2 != null) { - chestMap.remove(loc2); + removeChest(loc2); } - saveChests(); } else { player.sendMessage("[SnitchingChest] Chest is already under surveillance"); } @@ -85,13 +91,16 @@ return true; } - chestMap.put(loc, player.getName() ); + SnitchingChestBean chest1 = createChest(player.getName(), "", loc); + addChest(loc, chest1); if (loc2 != null) { - chestMap.put(loc2, player.getName() ); + + SnitchingChestBean chest2 = createChest (player.getName(), "", loc2); + addChest(loc, chest2); } player.sendMessage("[SnitchingChest] Chest is now under surveillance"); - saveChests(); + return true; } @@ -99,11 +108,10 @@ @EventHandler public void onBlockBreak(BlockBreakEvent event) { Location loc = event.getBlock().getLocation(); - String owner = chestMap.get(loc); - if (owner != null) { - if (owner.equals(event.getPlayer().getName())) { - chestMap.remove(loc); - saveChests(); + SnitchingChestBean chest = chestMap.get(loc); + if (chest != null) { + if (chest.getOwner().equals(event.getPlayer().getName())) { + removeChest(loc); event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance"); } else { event.setCancelled(true); @@ -111,15 +119,58 @@ } } } + public void addChest(Location loc, SnitchingChestBean chest) { + chestMap.put(loc, chest); + plugin.getDatabase().save(chest); + } - void loadChests() { + void removeChest(Location loc) { + SnitchingChestBean chest = chestMap.remove(loc); + if (chest != null) { + plugin.getDatabase().delete(chest); + } + } + + void loadChests() { + List chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList(); + for (SnitchingChestBean chest : chestlist) { + Location loc = getChestLocation(server, chest); + chestMap.put(loc, chest); + } + plugin.getLogger().info("[SnitchingChest] loaded " + chestMap.size() + " chests"); } - void saveChests() { + + public SnitchingChestBean createChest(String owner, String description, Location loc) { + SnitchingChestBean chest = new SnitchingChestBean(); + chest.setOwner(owner); + chest.setDescription(description); + setChestLocation(chest, loc); + + return chest; + } + + + public void setChestLocation(SnitchingChestBean 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, SnitchingChestBean 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(); @@ -150,10 +201,35 @@ } 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) { + SnitchingChestBean chest = chestMap.get(chestloc); + + if (chest != null) { //the neighbor is a snitching chest + SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc); + addChest(chestloc, newchest); + event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" ); + } + + } + } + + @EventHandler public void onChestOpen(InventoryOpenEvent event) { if (! (event.getPlayer() instanceof Player)) { @@ -165,14 +241,14 @@ if (holder instanceof Chest || holder instanceof DoubleChest) { Location loc = getChestLocation(holder); - String owner = chestMap.get( loc ); - if (owner == null) { + SnitchingChestBean chest = chestMap.get( loc ); + if (chest == null) { return; //chest not surveyed by this plugin } Player player = (Player) event.getPlayer(); - if (player.getName().equals(owner)) { + if (player.getName().equals(chest.getOwner() )) { return; //chest is owned by it's own player } @@ -192,8 +268,13 @@ InventoryHolder holder = event.getInventory().getHolder(); if (holder instanceof Chest || holder instanceof DoubleChest) { Location loc = getChestLocation(holder); - String ownerName = chestMap.get(loc); - OfflinePlayer owner = server.getOfflinePlayer(ownerName); + SnitchingChestBean chest = chestMap.get(loc); + + if (chest == null) { //chest was not a snitching chest + return; + } + + OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); Player player = (Player) event.getPlayer(); @@ -222,14 +303,15 @@ int diff = Math.abs( savedcount - count); - + String msg = null; if (count > savedcount) { - String msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest"; - plugin.getLogger().info(msg); - plugin.getMessageWrapper().sendMessage(owner, msg); + msg = player.getName() + " added " + diff + " units of " + item + " to " + owner.getName() + "'s chest"; } if (count < savedcount) { - String msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest"; + msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner.getName() + "'s chest"; + } + + if (msg != null) { plugin.getLogger().info(msg); plugin.getMessageWrapper().sendMessage(owner, msg); }