/[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

miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java revision 1773 by torben, Thu Apr 5 12:37:13 2012 UTC miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/SnitchingChest.java revision 1804 by torben, Mon May 28 15:06:26 2012 UTC
# Line 1  Line 1 
1  package dk.thoerup.bukkit.hoeruputils;  package dk.thoerup.bukkit.hoeruputils.chests;
2    
3    
4  import java.util.HashMap;  import java.util.HashMap;
# 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;
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    
33    import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin;
34    
35    
36  public class SnitchingChest  implements Listener, CommandExecutor{  public class SnitchingChest  implements Listener, CommandExecutor{
37                    
# Line 80  public class SnitchingChest  implements Line 85  public class SnitchingChest  implements
85                          if (chest.getOwner().equals(player.getName())) {                          if (chest.getOwner().equals(player.getName())) {
86                                  player.sendMessage("[SnitchingChest] Removing surveillance from chest");                                  player.sendMessage("[SnitchingChest] Removing surveillance from chest");
87                                  removeChest(loc);                                  removeChest(loc);
                                 if (loc2 != null) {  
                                         removeChest(loc2);                                        
                                 }  
88                          } else {                          } else {
89                                  player.sendMessage("[SnitchingChest] Chest is already under surveillance");                                  player.sendMessage("[SnitchingChest] Chest is already under surveillance");
90                          }                          }
# Line 90  public class SnitchingChest  implements Line 92  public class SnitchingChest  implements
92                          return true;                          return true;
93                  }                  }
94                                    
95                  SnitchingChestBean chest1 = createChest(player.getName(), "", loc);                  chest = createChest(player.getName(), "", loc);        
                 //chestMap.put(loc, chest1);  
                 addChest(loc, chest1);  
96                  if (loc2 != null) {                  if (loc2 != null) {
97                                                    chest.setDoublechest(true);
                         SnitchingChestBean chest2 = createChest (player.getName(), "", loc2);  
                         addChest(loc, chest2);  
                         //chestMap.put(loc2, chest2 );  
98                  }                  }
99                    addChest(loc, chest);
100                    
101                    
102                  player.sendMessage("[SnitchingChest] Chest is now under surveillance");                  player.sendMessage("[SnitchingChest] Chest is now under surveillance");
103                                    
# Line 122  public class SnitchingChest  implements Line 121  public class SnitchingChest  implements
121          }          }
122          public void addChest(Location loc, SnitchingChestBean chest) {          public void addChest(Location loc, SnitchingChestBean chest) {
123                  chestMap.put(loc, chest);                  chestMap.put(loc, chest);
124                    if (chest.isDoublechest()) {
125                            Location loc2 = getNeighborChest(loc);
126                            chestMap.put(loc2, chest);
127                    }              
128                  plugin.getDatabase().save(chest);                  plugin.getDatabase().save(chest);
129                    
130                    reloadChests();
131                    
132          }          }
133                    
134          void removeChest(Location loc) {          void removeChest(Location loc) {
135                  SnitchingChestBean chest = chestMap.remove(loc);                  SnitchingChestBean chest = chestMap.remove(loc);
136                  plugin.getDatabase().delete(chest);                  if (chest != null) {                                                                    
137                            if (chest.isDoublechest()){
138                                    Location loc2 = getNeighborChest(loc);
139                                    chestMap.remove(loc2);
140                            }
141                            plugin.getDatabase().delete(chest);
142                    }
143          }          }
144                    
145          void loadChests() {                      int loadChestsWorker() {
146                  List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList();                  List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList();
147                  for (SnitchingChestBean chest : chestlist) {                  for (SnitchingChestBean chest : chestlist) {
148                          Location loc = getChestLocation(server, chest);                          Location loc = getChestLocation(server, chest);
149                          chestMap.put(loc, chest);                                chestMap.put(loc, chest);      
150                            
151                            if (chest.isDoublechest()) {
152                                    Location loc2 = getNeighborChest(loc);
153                                    chestMap.put(loc2, chest);      
154                            }
155                  }                  }
156    
157                    return chestlist.size();
158            }
159            
160            void reloadChests() {          
161                    chestMap.clear();
162                    loadChestsWorker();            
163            }
164            
165            void loadChests() {            
166                    int count = loadChestsWorker();
167                    plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests");
168          }          }
169                    
170                    
# Line 198  public class SnitchingChest  implements Line 227  public class SnitchingChest  implements
227                  } else {                  } else {
228                          loc =  ( (DoubleChest)holder).getLocation();                          loc =  ( (DoubleChest)holder).getLocation();
229                  }                  }
230                    
231                    loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5
232                    loc.setZ( loc.getBlockZ() ); // -- // --
233                    
234                  return loc;                  return loc;
235          }          }
236                    
237          @EventHandler          @EventHandler
238            public void onChestPlaced(BlockPlaceEvent event) {
239                    Block block = event.getBlock();
240                    
241                    if (block.getType() != Material.CHEST) {
242                            return;
243                    }
244                    
245                    Location chestloc = getNeighborChest( block.getLocation() );
246                    if (chestloc != null) {
247                            SnitchingChestBean chest = chestMap.get(chestloc);
248                            
249                            if (chest != null) { //the neighbor is a snitching chest
250                                    //SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc);
251                                    //plugin.getDatabase().save(chest);
252                                    
253                                    chest.setDoublechest(true);
254                                    addChest(chestloc, chest);
255                                    
256                                    
257                                    event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" );
258                            }                      
259                            
260                    }
261            }
262            
263            /*
264             * how to prevent a user from opening a chest - usefull if SnitchingChest should morph into a LockedChest
265            @EventHandler
266            public void onChestInteract(PlayerInteractEvent event) {
267                    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
268                            Block b = event.getClickedBlock();
269                            
270                            if (b.getType() == Material.CHEST) {
271                                    event.setCancelled(true);
272                            }
273                    }              
274            }
275            */
276            
277            @EventHandler
278          public void onChestOpen(InventoryOpenEvent event) {          public void onChestOpen(InventoryOpenEvent event) {
279                                    
280                  if (! (event.getPlayer() instanceof Player)) {                  if (! (event.getPlayer() instanceof Player)) {
281                          return;                          return;
282                  }                  }
283    
284    
285                                    
286                  InventoryHolder holder = event.getInventory().getHolder();                  InventoryHolder holder = event.getInventory().getHolder();
287                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
# Line 241  public class SnitchingChest  implements Line 315  public class SnitchingChest  implements
315                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
316                          Location loc = getChestLocation(holder);                          Location loc = getChestLocation(holder);
317                          SnitchingChestBean chest = chestMap.get(loc);                          SnitchingChestBean chest = chestMap.get(loc);
318                            
319                            if (chest == null) { //chest was not a snitching chest
320                                    return;
321                            }
322                            
323                          OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );                          OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
324                                                    
325                                                    
# Line 270  public class SnitchingChest  implements Line 349  public class SnitchingChest  implements
349                                                                    
350                                                                    
351                                  int diff = Math.abs( savedcount - count);                                  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";  
                                 }  
352                                                                    
353                                  if (msg != null) {                                  if (diff > 0) {
354                                            String material = Material.getMaterial(item).name();
355                                            String msg = null;
356                                            
357                                            if (count > savedcount) {
358                                                    msg = player.getName() + " added "   + diff + " units of " + material + "(" +item + ") to "   + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
359                                            } else { //(count < savedcount)
360                                                    msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
361                                            }
362                                    
363                                    
364                                          plugin.getLogger().info(msg);                                          plugin.getLogger().info(msg);
365                                          plugin.getMessageWrapper().sendMessage(owner, msg);                                          plugin.getMessageWrapper().sendMessage("system", owner, msg);
366                                  }                                  }
367                                                                    
368                          }                          }

Legend:
Removed from v.1773  
changed lines
  Added in v.1804

  ViewVC Help
Powered by ViewVC 1.1.20