/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/SnitchingChest.java
ViewVC logotype

Diff of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/SnitchingChest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1769 by torben, Tue Apr 3 20:23:59 2012 UTC revision 1785 by torben, Fri Apr 6 09:51:01 2012 UTC
# Line 2  package dk.thoerup.bukkit.hoeruputils; Line 2  package dk.thoerup.bukkit.hoeruputils;
2    
3    
4  import java.util.HashMap;  import java.util.HashMap;
5    import java.util.List;
6  import java.util.Set;  import java.util.Set;
7  import java.util.TreeMap;  import java.util.TreeMap;
8  import java.util.TreeSet;  import java.util.TreeSet;
9    
10  import org.bukkit.Location;  import org.bukkit.Location;
11  import org.bukkit.Material;  import org.bukkit.Material;
12    import org.bukkit.OfflinePlayer;
13    import org.bukkit.Server;
14  import org.bukkit.World;  import org.bukkit.World;
15  import org.bukkit.block.Block;  import org.bukkit.block.Block;
16  import org.bukkit.block.Chest;  import org.bukkit.block.Chest;
# Line 19  import org.bukkit.entity.Player; Line 22  import org.bukkit.entity.Player;
22  import org.bukkit.event.EventHandler;  import org.bukkit.event.EventHandler;
23  import org.bukkit.event.Listener;  import org.bukkit.event.Listener;
24  import org.bukkit.event.block.BlockBreakEvent;  import org.bukkit.event.block.BlockBreakEvent;
25    import org.bukkit.event.block.BlockPlaceEvent;
26  import org.bukkit.event.inventory.InventoryCloseEvent;  import org.bukkit.event.inventory.InventoryCloseEvent;
27  import org.bukkit.event.inventory.InventoryOpenEvent;  import org.bukkit.event.inventory.InventoryOpenEvent;
28  import org.bukkit.inventory.InventoryHolder;  import org.bukkit.inventory.InventoryHolder;
29  import org.bukkit.inventory.ItemStack;  import org.bukkit.inventory.ItemStack;
30  import org.bukkit.plugin.Plugin;  
31    
32  public class SnitchingChest  implements Listener, CommandExecutor{  public class SnitchingChest  implements Listener, CommandExecutor{
33                    
# Line 33  public class SnitchingChest  implements Line 37  public class SnitchingChest  implements
37                    
38          HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>();          HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>();
39                    
40          HashMap<Location,String> chestMap = new HashMap<Location, String>();          HashMap<Location,SnitchingChestBean> chestMap = new HashMap<Location, SnitchingChestBean>();
41    
42                    
43          Plugin plugin;          HoerupUtilsPlugin plugin;
44            Server server;
45                    
46          public SnitchingChest(Plugin plugin) {          public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) {
47                  this.plugin = plugin;                  this.plugin = plugin;
48                                    server = plugin.getServer();
49                  loadChests();                  try {
50                            loadChests();
51                    } catch (Exception e) {
52                            e.printStackTrace();
53                            //r.run();
54                            loadChests();
55                    }
56          }          }
57                    
58    
# Line 65  public class SnitchingChest  implements Line 76  public class SnitchingChest  implements
76                                    
77                  Location loc2 = getNeighborChest(loc);                  Location loc2 = getNeighborChest(loc);
78                                    
79                  String owner = chestMap.get(loc);                  SnitchingChestBean chest = chestMap.get(loc);
80                  if (owner != null) {                  if (chest != null) {
81                          if (owner.equals(player.getName())) {                          if (chest.getOwner().equals(player.getName())) {
82                                  player.sendMessage("[SnitchingChest] Removing surveillance from chest");                                  player.sendMessage("[SnitchingChest] Removing surveillance from chest");
83                                  chestMap.remove(loc);                                  removeChest(loc);
                                 if (loc2 != null) {  
                                         chestMap.remove(loc2);                                    
                                 }  
                                 saveChests();  
84                          } else {                          } else {
85                                  player.sendMessage("[SnitchingChest] Chest is already under surveillance");                                  player.sendMessage("[SnitchingChest] Chest is already under surveillance");
86                          }                          }
# Line 81  public class SnitchingChest  implements Line 88  public class SnitchingChest  implements
88                          return true;                          return true;
89                  }                  }
90                                    
91                  chestMap.put(loc, player.getName() );                  chest = createChest(player.getName(), "", loc);        
92                  if (loc2 != null) {                  if (loc2 != null) {
93                          chestMap.put(loc2, player.getName() );                          chest.setDoublechest(true);
94                  }                  }
95                    addChest(loc, chest);
96                    
97                    
98                  player.sendMessage("[SnitchingChest] Chest is now under surveillance");                  player.sendMessage("[SnitchingChest] Chest is now under surveillance");
99                  saveChests();                  
100                                    
101                  return true;                  return true;
102          }          }
# Line 95  public class SnitchingChest  implements Line 104  public class SnitchingChest  implements
104          @EventHandler          @EventHandler
105          public void onBlockBreak(BlockBreakEvent event) {          public void onBlockBreak(BlockBreakEvent event) {
106                  Location loc = event.getBlock().getLocation();                  Location loc = event.getBlock().getLocation();
107                  String owner = chestMap.get(loc);                  SnitchingChestBean chest = chestMap.get(loc);
108                  if (owner != null) {                  if (chest != null) {
109                          if (owner.equals(event.getPlayer().getName())) {                          if (chest.getOwner().equals(event.getPlayer().getName())) {
110                                  chestMap.remove(loc);                                  removeChest(loc);
                                 saveChests();                            
111                                  event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance");                                  event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance");
112                          } else {                          } else {
113                                  event.setCancelled(true);                                  event.setCancelled(true);
# Line 107  public class SnitchingChest  implements Line 115  public class SnitchingChest  implements
115                          }                          }
116                  }                  }
117          }          }
118            public void addChest(Location loc, SnitchingChestBean chest) {
119                    chestMap.put(loc, chest);
120                    if (chest.isDoublechest()) {
121                            Location loc2 = getNeighborChest(loc);
122                            chestMap.put(loc2, chest);
123                    }              
124                    plugin.getDatabase().save(chest);
125                    
126                    reloadChests();
127                    
128            }
129            
130            void removeChest(Location loc) {
131                    SnitchingChestBean chest = chestMap.remove(loc);
132                    if (chest != null) {                                                                    
133                            if (chest.isDoublechest()){
134                                    Location loc2 = getNeighborChest(loc);
135                                    chestMap.remove(loc2);
136                            }
137                            plugin.getDatabase().delete(chest);
138                    }
139            }
140            
141            int loadChestsWorker() {
142                    List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList();
143                    for (SnitchingChestBean chest : chestlist) {
144                            Location loc = getChestLocation(server, chest);
145                            chestMap.put(loc, chest);      
146                            
147                            if (chest.isDoublechest()) {
148                                    Location loc2 = getNeighborChest(loc);
149                                    chestMap.put(loc2, chest);      
150                            }
151                    }
152    
153                    return chestlist.size();
154            }
155            
156            void reloadChests() {          
157                    chestMap.clear();
158                    loadChestsWorker();            
159            }
160                    
161          void loadChests() {          void loadChests() {            
162                    int count = loadChestsWorker();
163                    plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests");
164            }
165            
166            
167            public SnitchingChestBean createChest(String owner, String description, Location loc) {
168                                    
169                    SnitchingChestBean chest = new SnitchingChestBean();
170                    chest.setOwner(owner);
171                    chest.setDescription(description);
172                    setChestLocation(chest, loc);
173    
174                    return chest;
175            }
176            
177            
178            public void setChestLocation(SnitchingChestBean chest, Location loc) {
179                    chest.setWorld( loc.getWorld().getName() );
180                    chest.setX( loc.getBlockX() );
181                    chest.setY( loc.getBlockY() );
182                    chest.setZ( loc.getBlockZ() );          
183          }          }
184                    
185            public Location getChestLocation(Server server, SnitchingChestBean chest) {
186                    World wrld = server.getWorld(chest.getWorld());
187                    return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());              
188            }
189            
190    
191            /*
192          void saveChests() {          void saveChests() {
193                                    
194          }          }*/
195                    
196          Location getNeighborChest(Location loc) {          Location getNeighborChest(Location loc) {
197                  World world = loc.getWorld();                  World world = loc.getWorld();
# Line 146  public class SnitchingChest  implements Line 223  public class SnitchingChest  implements
223                  } else {                  } else {
224                          loc =  ( (DoubleChest)holder).getLocation();                          loc =  ( (DoubleChest)holder).getLocation();
225                  }                  }
226                    
227                    loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5
228                    loc.setZ( loc.getBlockZ() ); // -- // --
229                    
230                  return loc;                  return loc;
231          }          }
232                    
233          @EventHandler          @EventHandler
234            public void onChestPlaced(BlockPlaceEvent event) {
235                    Block block = event.getBlock();
236                    
237                    if (block.getType() != Material.CHEST) {
238                            return;
239                    }
240                    
241                    Location chestloc = getNeighborChest( block.getLocation() );
242                    if (chestloc != null) {
243                            SnitchingChestBean chest = chestMap.get(chestloc);
244                            
245                            if (chest != null) { //the neighbor is a snitching chest
246                                    //SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc);
247                                    //plugin.getDatabase().save(chest);
248                                    
249                                    chest.setDoublechest(true);
250                                    addChest(chestloc, chest);
251                                    
252                                    
253                                    event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" );
254                            }                      
255                            
256                    }
257            }
258            
259            @EventHandler
260          public void onChestOpen(InventoryOpenEvent event) {          public void onChestOpen(InventoryOpenEvent event) {
261                                    
262                  if (! (event.getPlayer() instanceof Player)) {                  if (! (event.getPlayer() instanceof Player)) {
# Line 161  public class SnitchingChest  implements Line 268  public class SnitchingChest  implements
268                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
269                          Location loc = getChestLocation(holder);                          Location loc = getChestLocation(holder);
270                                                    
271                          String owner = chestMap.get( loc );                          SnitchingChestBean chest = chestMap.get( loc );
272                          if (owner == null) {                                                      if (chest == null) {                            
273                                  return; //chest not surveyed by this plugin                                  return; //chest not surveyed by this plugin
274                          }                          }
275                                    
276                                                                                                    
277                          Player player = (Player) event.getPlayer();                          Player player = (Player) event.getPlayer();
278                          if (player.getName().equals(owner)) {                          if (player.getName().equals(chest.getOwner() )) {
279                                  return; //chest is owned by it's own player                                  return; //chest is owned by it's own player
280                          }                                                }                      
281                                                    
# Line 188  public class SnitchingChest  implements Line 295  public class SnitchingChest  implements
295                  InventoryHolder holder = event.getInventory().getHolder();                  InventoryHolder holder = event.getInventory().getHolder();
296                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
297                          Location loc = getChestLocation(holder);                          Location loc = getChestLocation(holder);
298                          String owner = chestMap.get(loc);                          SnitchingChestBean chest = chestMap.get(loc);
299                            
300                            if (chest == null) { //chest was not a snitching chest
301                                    return;
302                            }
303                            
304                            OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
305                                                    
306                                                    
307                          Player player = (Player) event.getPlayer();                          Player player = (Player) event.getPlayer();
# Line 218  public class SnitchingChest  implements Line 331  public class SnitchingChest  implements
331                                                                    
332                                  int diff = Math.abs( savedcount - count);                                  int diff = Math.abs( savedcount - count);
333                                                                    
334                                  if (count > savedcount) {                                  if (diff > 0) {
335                                          String msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest";                                          String material = Material.getMaterial(item).name();
336                                          plugin.getLogger().info(msg);                                          String msg = null;
337                                  }                                          
338                                  if (count < savedcount) {                                          if (count > savedcount) {
339                                          String msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest";                                                  msg = player.getName() + " added "   + diff + " units of " + material + "(" +item + ") to "   + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
340                                            } else { //(count < savedcount)
341                                                    msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
342                                            }
343                                    
344                                    
345                                          plugin.getLogger().info(msg);                                          plugin.getLogger().info(msg);
346                                            plugin.getMessageWrapper().sendMessage(owner, msg);
347                                  }                                  }
348                                                                    
349                          }                          }

Legend:
Removed from v.1769  
changed lines
  Added in v.1785

  ViewVC Help
Powered by ViewVC 1.1.20