--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java 2012/04/05 18:51:57 1780 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/SnitchingChest.java 2012/05/28 15:06:26 1804 @@ -1,4 +1,4 @@ -package dk.thoerup.bukkit.hoeruputils; +package dk.thoerup.bukkit.hoeruputils.chests; import java.util.HashMap; @@ -21,13 +21,17 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; 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.event.player.PlayerInteractEvent; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; +import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; + public class SnitchingChest implements Listener, CommandExecutor{ @@ -81,9 +85,6 @@ if (chest.getOwner().equals(player.getName())) { player.sendMessage("[SnitchingChest] Removing surveillance from chest"); removeChest(loc); - if (loc2 != null) { - removeChest(loc2); - } } else { player.sendMessage("[SnitchingChest] Chest is already under surveillance"); } @@ -91,13 +92,12 @@ return true; } - SnitchingChestBean chest1 = createChest(player.getName(), "", loc); - addChest(loc, chest1); + chest = createChest(player.getName(), "", loc); if (loc2 != null) { - - SnitchingChestBean chest2 = createChest (player.getName(), "", loc2); - addChest(loc, chest2); + chest.setDoublechest(true); } + addChest(loc, chest); + player.sendMessage("[SnitchingChest] Chest is now under surveillance"); @@ -121,24 +121,50 @@ } public void addChest(Location loc, SnitchingChestBean chest) { chestMap.put(loc, chest); + if (chest.isDoublechest()) { + Location loc2 = getNeighborChest(loc); + chestMap.put(loc2, chest); + } plugin.getDatabase().save(chest); + + reloadChests(); + } void removeChest(Location loc) { SnitchingChestBean chest = chestMap.remove(loc); - if (chest != null) { + if (chest != null) { + if (chest.isDoublechest()){ + Location loc2 = getNeighborChest(loc); + chestMap.remove(loc2); + } plugin.getDatabase().delete(chest); } } - void loadChests() { + int loadChestsWorker() { List chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList(); for (SnitchingChestBean chest : chestlist) { Location loc = getChestLocation(server, chest); chestMap.put(loc, chest); + + if (chest.isDoublechest()) { + Location loc2 = getNeighborChest(loc); + chestMap.put(loc2, chest); + } } - - plugin.getLogger().info("[SnitchingChest] loaded " + chestMap.size() + " chests"); + + return chestlist.size(); + } + + void reloadChests() { + chestMap.clear(); + loadChestsWorker(); + } + + void loadChests() { + int count = loadChestsWorker(); + plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests"); } @@ -221,14 +247,33 @@ SnitchingChestBean chest = chestMap.get(chestloc); if (chest != null) { //the neighbor is a snitching chest - SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc); - addChest(chestloc, newchest); + //SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc); + //plugin.getDatabase().save(chest); + + chest.setDoublechest(true); + addChest(chestloc, chest); + + event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" ); } } } + /* + * how to prevent a user from opening a chest - usefull if SnitchingChest should morph into a LockedChest + @EventHandler + public void onChestInteract(PlayerInteractEvent event) { + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + Block b = event.getClickedBlock(); + + if (b.getType() == Material.CHEST) { + event.setCancelled(true); + } + } + } + */ + @EventHandler public void onChestOpen(InventoryOpenEvent event) { @@ -236,6 +281,7 @@ return; } + InventoryHolder holder = event.getInventory().getHolder(); if (holder instanceof Chest || holder instanceof DoubleChest) { @@ -303,17 +349,20 @@ int diff = Math.abs( savedcount - count); - String msg = null; - if (count > savedcount) { - msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest"; - } - if (count < savedcount) { - msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest"; - } - if (msg != null) { + 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(owner, msg); + plugin.getMessageWrapper().sendMessage("system", owner, msg); } }