--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java 2012/04/03 20:21:02 1768 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java 2012/04/05 14:27:24 1779 @@ -2,12 +2,15 @@ import java.util.HashMap; +import java.util.List; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; 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; import org.bukkit.block.Chest; @@ -23,7 +26,7 @@ import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; -import org.bukkit.plugin.Plugin; + public class SnitchingChest implements Listener, CommandExecutor{ @@ -33,15 +36,22 @@ HashMap contentMap = new HashMap(); - HashMap chestMap = new HashMap(); + HashMap chestMap = new HashMap(); - Plugin plugin; + HoerupUtilsPlugin plugin; + Server server; - public SnitchingChest(Plugin plugin) { + public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) { this.plugin = plugin; - - loadChests(); + server = plugin.getServer(); + try { + loadChests(); + } catch (Exception e) { + e.printStackTrace(); + //r.run(); + loadChests(); + } } @@ -65,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"); } @@ -81,13 +90,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; } @@ -95,11 +107,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); @@ -107,15 +118,58 @@ } } } + public void addChest(Location loc, SnitchingChestBean chest) { + chestMap.put(loc, chest); + plugin.getDatabase().save(chest); + } + + void removeChest(Location loc) { + SnitchingChestBean chest = chestMap.remove(loc); + if (chest != null) { + plugin.getDatabase().delete(chest); + } + } - void loadChests() { + 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(); @@ -146,6 +200,10 @@ } 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; } @@ -161,14 +219,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 } @@ -188,6 +246,14 @@ InventoryHolder holder = event.getInventory().getHolder(); if (holder instanceof Chest || holder instanceof DoubleChest) { Location loc = getChestLocation(holder); + 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(); @@ -215,14 +281,17 @@ int diff = Math.abs( savedcount - count); - + String msg = null; if (count > savedcount) { - String msg = player.getName() + " added " + diff + " units of " + item ; - plugin.getLogger().info(msg); + msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest"; } if (count < savedcount) { - String msg = player.getName() + " removed " + diff + " units of " + item " from "; + msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest"; + } + + if (msg != null) { plugin.getLogger().info(msg); + plugin.getMessageWrapper().sendMessage(owner, msg); } }