--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java 2012/04/03 20:49:42 1772 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java 2012/04/05 12:37:13 1773 @@ -2,6 +2,7 @@ import java.util.HashMap; +import java.util.List; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; @@ -35,17 +36,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 +75,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 +90,18 @@ return true; } - chestMap.put(loc, player.getName() ); + SnitchingChestBean chest1 = createChest(player.getName(), "", loc); + //chestMap.put(loc, chest1); + addChest(loc, chest1); if (loc2 != null) { - chestMap.put(loc2, player.getName() ); + + SnitchingChestBean chest2 = createChest (player.getName(), "", loc2); + addChest(loc, chest2); + //chestMap.put(loc2, chest2 ); } player.sendMessage("[SnitchingChest] Chest is now under surveillance"); - saveChests(); + return true; } @@ -99,11 +109,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,14 +120,53 @@ } } } + public void addChest(Location loc, SnitchingChestBean chest) { + chestMap.put(loc, chest); + plugin.getDatabase().save(chest); + } + + void removeChest(Location loc) { + SnitchingChestBean chest = chestMap.remove(loc); + 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); + } + } - void loadChests() { + + 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(); @@ -165,14 +213,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 +240,8 @@ 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); + OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); Player player = (Player) event.getPlayer();