/[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 1783 by torben, Thu Apr 5 20:53:59 2012 UTC revision 1803 by torben, Mon May 28 14:32:53 2012 UTC
# Line 21  import org.bukkit.command.CommandSender; Line 21  import org.bukkit.command.CommandSender;
21  import org.bukkit.entity.Player;  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.Action;
25  import org.bukkit.event.block.BlockBreakEvent;  import org.bukkit.event.block.BlockBreakEvent;
26  import org.bukkit.event.block.BlockPlaceEvent;  import org.bukkit.event.block.BlockPlaceEvent;
27  import org.bukkit.event.inventory.InventoryCloseEvent;  import org.bukkit.event.inventory.InventoryCloseEvent;
28  import org.bukkit.event.inventory.InventoryOpenEvent;  import org.bukkit.event.inventory.InventoryOpenEvent;
29    import org.bukkit.event.player.PlayerInteractEvent;
30  import org.bukkit.inventory.InventoryHolder;  import org.bukkit.inventory.InventoryHolder;
31  import org.bukkit.inventory.ItemStack;  import org.bukkit.inventory.ItemStack;
32    
# Line 81  public class SnitchingChest  implements Line 83  public class SnitchingChest  implements
83                          if (chest.getOwner().equals(player.getName())) {                          if (chest.getOwner().equals(player.getName())) {
84                                  player.sendMessage("[SnitchingChest] Removing surveillance from chest");                                  player.sendMessage("[SnitchingChest] Removing surveillance from chest");
85                                  removeChest(loc);                                  removeChest(loc);
                                 if (loc2 != null) {  
                                         removeChest(loc2);                                        
                                 }  
86                          } else {                          } else {
87                                  player.sendMessage("[SnitchingChest] Chest is already under surveillance");                                  player.sendMessage("[SnitchingChest] Chest is already under surveillance");
88                          }                          }
# Line 91  public class SnitchingChest  implements Line 90  public class SnitchingChest  implements
90                          return true;                          return true;
91                  }                  }
92                                    
93                  SnitchingChestBean chest1 = createChest(player.getName(), "", loc);                  chest = createChest(player.getName(), "", loc);        
                 addChest(loc, chest1);  
94                  if (loc2 != null) {                  if (loc2 != null) {
95                                                    chest.setDoublechest(true);
                         SnitchingChestBean chest2 = createChest (player.getName(), "", loc2);  
                         addChest(loc, chest2);  
96                  }                  }
97                    addChest(loc, chest);
98                    
99                    
100                  player.sendMessage("[SnitchingChest] Chest is now under surveillance");                  player.sendMessage("[SnitchingChest] Chest is now under surveillance");
101                                    
# Line 121  public class SnitchingChest  implements Line 119  public class SnitchingChest  implements
119          }          }
120          public void addChest(Location loc, SnitchingChestBean chest) {          public void addChest(Location loc, SnitchingChestBean chest) {
121                  chestMap.put(loc, chest);                  chestMap.put(loc, chest);
122                    if (chest.isDoublechest()) {
123                            Location loc2 = getNeighborChest(loc);
124                            chestMap.put(loc2, chest);
125                    }              
126                  plugin.getDatabase().save(chest);                  plugin.getDatabase().save(chest);
127                    
128                    reloadChests();
129                    
130          }          }
131                    
132          void removeChest(Location loc) {          void removeChest(Location loc) {
133                  SnitchingChestBean chest = chestMap.remove(loc);                  SnitchingChestBean chest = chestMap.remove(loc);
134                  if (chest != null) {                  if (chest != null) {                                                                    
135                            if (chest.isDoublechest()){
136                                    Location loc2 = getNeighborChest(loc);
137                                    chestMap.remove(loc2);
138                            }
139                          plugin.getDatabase().delete(chest);                          plugin.getDatabase().delete(chest);
140                  }                  }
141          }          }
142                    
143          void loadChests() {                      int loadChestsWorker() {
144                  List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList();                  List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList();
145                  for (SnitchingChestBean chest : chestlist) {                  for (SnitchingChestBean chest : chestlist) {
146                          Location loc = getChestLocation(server, chest);                          Location loc = getChestLocation(server, chest);
147                          chestMap.put(loc, chest);                                chestMap.put(loc, chest);      
148                            
149                            if (chest.isDoublechest()) {
150                                    Location loc2 = getNeighborChest(loc);
151                                    chestMap.put(loc2, chest);      
152                            }
153                  }                  }
154                    
155                  plugin.getLogger().info("[SnitchingChest] loaded " + chestMap.size() + " chests");                  return chestlist.size();
156            }
157            
158            void reloadChests() {          
159                    chestMap.clear();
160                    loadChestsWorker();            
161            }
162            
163            void loadChests() {            
164                    int count = loadChestsWorker();
165                    plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests");
166          }          }
167                    
168                    
# Line 221  public class SnitchingChest  implements Line 245  public class SnitchingChest  implements
245                          SnitchingChestBean chest = chestMap.get(chestloc);                          SnitchingChestBean chest = chestMap.get(chestloc);
246                                                    
247                          if (chest != null) { //the neighbor is a snitching chest                          if (chest != null) { //the neighbor is a snitching chest
248                                  SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc);                                  //SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc);
249                                  addChest(chestloc, newchest);                                  //plugin.getDatabase().save(chest);
250                                    
251                                    chest.setDoublechest(true);
252                                    addChest(chestloc, chest);
253                                    
254                                    
255                                  event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" );                                  event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" );
256                          }                                                }                      
257                                                    
258                  }                  }
259          }          }
260                    
261            /*
262             * how to prevent a user from opening a chest - usefull if SnitchingChest should morph into a LockedChest
263            @EventHandler
264            public void onChestInteract(PlayerInteractEvent event) {
265                    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
266                            Block b = event.getClickedBlock();
267                            
268                            if (b.getType() == Material.CHEST) {
269                                    event.setCancelled(true);
270                            }
271                    }              
272            }
273            */
274            
275          @EventHandler          @EventHandler
276          public void onChestOpen(InventoryOpenEvent event) {          public void onChestOpen(InventoryOpenEvent event) {
277                                    
# Line 236  public class SnitchingChest  implements Line 279  public class SnitchingChest  implements
279                          return;                          return;
280                  }                  }
281    
282    
283                                    
284                  InventoryHolder holder = event.getInventory().getHolder();                  InventoryHolder holder = event.getInventory().getHolder();
285                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
# Line 309  public class SnitchingChest  implements Line 353  public class SnitchingChest  implements
353                                          String msg = null;                                          String msg = null;
354                                                                                    
355                                          if (count > savedcount) {                                          if (count > savedcount) {
356                                                  msg = player.getName() + " added "   + diff + " units of " + material + "(" +item + ") to "   + owner.getName() + "'s chest at " + loc.getWorld() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();                                                  msg = player.getName() + " added "   + diff + " units of " + material + "(" +item + ") to "   + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
357                                          } else { //(count < savedcount)                                          } else { //(count < savedcount)
358                                                  msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();                                                  msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
359                                          }                                          }
360                                                                    
361                                                                    
362                                          plugin.getLogger().info(msg);                                          plugin.getLogger().info(msg);
363                                          plugin.getMessageWrapper().sendMessage(owner, msg);                                          plugin.getMessageWrapper().sendMessage("system", owner, msg);
364                                  }                                  }
365                                                                    
366                          }                          }

Legend:
Removed from v.1783  
changed lines
  Added in v.1803

  ViewVC Help
Powered by ViewVC 1.1.20