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

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

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

revision 1853 by torben, Tue Oct 2 16:40:56 2012 UTC revision 2432 by torben, Mon Mar 9 13:55:54 2015 UTC
# Line 2  package dk.thoerup.bukkit.hoeruputils.ch Line 2  package dk.thoerup.bukkit.hoeruputils.ch
2    
3    
4  import java.util.HashMap;  import java.util.HashMap;
5    import java.util.HashSet;
6  import java.util.List;  import java.util.List;
7  import java.util.Set;  import java.util.Set;
8  import java.util.TreeMap;  import java.util.TreeMap;
# Line 24  import org.bukkit.event.EventHandler; Line 25  import org.bukkit.event.EventHandler;
25  import org.bukkit.event.Listener;  import org.bukkit.event.Listener;
26  import org.bukkit.event.block.Action;  import org.bukkit.event.block.Action;
27  import org.bukkit.event.block.BlockBreakEvent;  import org.bukkit.event.block.BlockBreakEvent;
28    import org.bukkit.event.block.BlockBurnEvent;
29  import org.bukkit.event.block.BlockPlaceEvent;  import org.bukkit.event.block.BlockPlaceEvent;
30  import org.bukkit.event.entity.EntityExplodeEvent;  import org.bukkit.event.entity.EntityExplodeEvent;
31  import org.bukkit.event.inventory.InventoryCloseEvent;  import org.bukkit.event.inventory.InventoryCloseEvent;
# Line 42  public class AdvancedChest  implements L Line 44  public class AdvancedChest  implements L
44          class ItemCount extends TreeMap<Integer,Integer> {          class ItemCount extends TreeMap<Integer,Integer> {
45                  private static final long serialVersionUID = 1L;                  private static final long serialVersionUID = 1L;
46          };          };
47            
48          HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>();          HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>();
49            
50                    
51          HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>();          HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>();
52    
53            
54          HoerupUtilsPlugin plugin;          HoerupUtilsPlugin plugin;
55          Server server;          Server server;
56            
57          public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) {          public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) {
58                  this.plugin = plugin;                  this.plugin = plugin;
59                  server = plugin.getServer();                  server = plugin.getServer();
# Line 63  public class AdvancedChest  implements L Line 65  public class AdvancedChest  implements L
65                          loadChests();                          loadChests();
66                  }                  }
67          }          }
68            
69    
70          @Override          @Override
71          public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {          public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
# Line 71  public class AdvancedChest  implements L Line 73  public class AdvancedChest  implements L
73                          sender.sendMessage("this is not a console command!");                          sender.sendMessage("this is not a console command!");
74                          return true;                          return true;
75                  }                  }
76                    
77                  Player player = (Player) sender;                  Player player = (Player) sender;
78                    
79                  if (args.length == 0) {                  if (args.length == 0) {
80                          player.sendMessage("Usage:");                          player.sendMessage("Usage:");
81                          player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer) [player]");                          player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer|setowner|comment) [player]");
82                          return true;                          return true;
83                  }                  }
84    
85    
86                    Block b = player.getTargetBlock( (Set<Material>)null, 30);                      
87                    Material mat = b.getType();
88                                    
89                                                    if (mat != Material.CHEST && mat != Material.HOPPER) {
90                  Block b = player.getTargetBlock(null, 30);                                                player.sendMessage("[Chest] Please look at the chest/hopper you want to protect");
                   
                 if (b.getTypeId() != 54) {  
                         player.sendMessage("[Chest] Please look at the chest you want to protect");  
91                          return true;                          return true;
92                  }                  }
93                    
94                  Location loc = b.getLocation();                  Location loc = b.getLocation();
95                  Location loc2 = getNeighborChest(loc);                  Location loc2 = null;
96                                    if ( mat == Material.HOPPER) { //dont find neighbours for Hoppers
97                            loc2 = getNeighborChest(loc);
98                    }
99    
100                  ChestBean chest = chestMap.get(loc);                  ChestBean chest = chestMap.get(loc);
101                  String cmd = args[0].toLowerCase();                  String cmd = args[0].toLowerCase();
102                    
103                  if (cmd.equals("status")) {                  if (cmd.equals("status")) {
104                          if (chest != null) {                                                      if (chest != null) {                            
105                                  String mode = "";                                  String mode = "";
# Line 107  public class AdvancedChest  implements L Line 113  public class AdvancedChest  implements L
113                                  default:                                  default:
114                                          mode = "unknown ??";                                          mode = "unknown ??";
115                                  }                                                                }                              
116                                    
117                                  player.sendMessage(ChatColor.GREEN + "Chest is  a " + mode + " chest owned by " + chest.getOwner());                                  player.sendMessage(ChatColor.GREEN + "Chest is  a " + mode + " chest owned by " + chest.getOwner());
118                                  player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() );                                  player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() );
119                                    player.sendMessage(ChatColor.GREEN + "Comment: " + chest.getComment() );
120                          } else {                          } else {
121                                  player.sendMessage(ChatColor.GREEN + "The chest is not protected");                                  player.sendMessage(ChatColor.GREEN + "The chest is not protected");
122                          }                          }
123                          return true;                          return true;
124                  }                  }
125                    
126                  if (cmd.equals("lock") || cmd.equals("snitch")) {                  if (cmd.equals("lock") || cmd.equals("snitch")) {
127                          if (chest == null) {                          if (chest == null) {
128                                  chest = createChest(player.getName(), "", loc);                                          chest = createChest(player.getName(), "", loc);        
# Line 131  public class AdvancedChest  implements L Line 138  public class AdvancedChest  implements L
138                                          modeStr = "snitching";                                          modeStr = "snitching";
139                                  }                                  }
140                                  chest.setModifyPlayers("");                                  chest.setModifyPlayers("");
141                                    chest.setComment("");
142                                  addChest(loc, chest);                                  addChest(loc, chest);
143                                  player.sendMessage("Chest is now " + modeStr);                                  player.sendMessage("Chest is now " + modeStr);
144                          } else {                          } else {
145                                    server.getLogger().info( player.getName() + " tried to protect a chest owned by " + chest.getOwner() );
146                                  player.sendMessage("This chest is already protected");                                  player.sendMessage("This chest is already protected");
147                          }                          }
148                          return true;                          return true;
149                  }                  }
150                    
151                  if (cmd.equals("remove")) {                  if (cmd.equals("remove")) {
152                          if (chest == null) {                          if (chest == null) {
153                                  player.sendMessage("This chest is not protected");                                  player.sendMessage("This chest is not protected");
154                                  return true;                                  return true;
155                          }                          }
156                          if (! chest.getOwner().equals( player.getName() ) ) {                          if ( chest.getOwner().equals( player.getName() ) || player.isOp() ) {
157                                    //do nothing
158                            } else {
159                                  player.sendMessage("You can not remove lock from a chest you don't own");                                  player.sendMessage("You can not remove lock from a chest you don't own");
160                                    server.getLogger().info( player.getName() + " tried to remove protection froma chest owned by " + chest.getOwner() );
161                                  return true;                                  return true;
162                          }                          }
163    
# Line 154  public class AdvancedChest  implements L Line 166  public class AdvancedChest  implements L
166    
167                          return true;                          return true;
168                  }                  }
169                    
170                    if (cmd.equals("setowner")) {
171                            if (chest == null) {
172                                    player.sendMessage("This chest is not protected");
173                                    return true;
174                            }
175                            if ( chest.getOwner().equals( player.getName() ) || player.isOp() ) {
176                                    //do nothing
177                            } else {
178                                    player.sendMessage("You can not set new owner of a chest you dont own");
179                                    server.getLogger().info( player.getName() + " tried to set owner on a chest owned by " + chest.getOwner() );
180                                    return true;
181                            }
182                            if (args.length != 2) {
183                                    player.sendMessage("You need to specify which player should own this chest");
184                                    return true;
185                            }
186                            
187                            @SuppressWarnings("deprecation")//user by name is our only option here
188                            OfflinePlayer p2 = server.getOfflinePlayer(args[1]);
189                            
190                            if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) {
191                                    player.sendMessage("Unknown user: " + args[1] );
192                                    return true;
193                            }
194                            chest.setOwner( p2.getName() );
195                            plugin.getDatabase().save( chest );
196                            player.sendMessage("ok");
197                            return true;
198                    }
199    
200                  if (cmd.equals("addplayer") || cmd.equals("removeplayer")) {                  if (cmd.equals("addplayer") || cmd.equals("removeplayer")) {
201                          if (chest == null) {                          if (chest == null) {
202                                  player.sendMessage("This chest is not protected");                                  player.sendMessage("This chest is not protected");
# Line 162  public class AdvancedChest  implements L Line 204  public class AdvancedChest  implements L
204                          }                          }
205                          if (! chest.getOwner().equals( player.getName() ) ) {                          if (! chest.getOwner().equals( player.getName() ) ) {
206                                  player.sendMessage("You can not add/remove players from a chest you don't own");                                  player.sendMessage("You can not add/remove players from a chest you don't own");
207                                    server.getLogger().info( player.getName() + " tried to add/remove player on a chest owned by " + chest.getOwner() );
208                                  return true;                                  return true;
209                          }                          }
210                          if (args.length != 2) {                          if (args.length != 2) {
211                                  player.sendMessage("You need to specify which player to add or remove");                                  player.sendMessage("You need to specify which player to add or remove");
212                                  return true;                                  return true;
213                          }                          }
214                            
215                            @SuppressWarnings("deprecation")//user by name is our only option here
216                          OfflinePlayer p2 = server.getOfflinePlayer(args[1]);                          OfflinePlayer p2 = server.getOfflinePlayer(args[1]);
217                          if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) {                          if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) {
218                                  player.sendMessage("Unknown user: " + args[1] );                                  player.sendMessage("Unknown user: " + args[1] );
219                                  return true;                                  return true;
220                          }                          }
221                            
222                          Set<String> players = Util.stringToSet( chest.getModifyPlayers() );                          Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
223                          if (cmd.equals("addplayer")) {                          if (cmd.equals("addplayer")) {
224                                  players.add(p2.getName());                                  players.add(p2.getName());
225                          } else {                          } else {
226                                  players.remove(p2.getName());                                  players.remove(p2.getName());
227                          }                          }
228                            
229                          chest.setModifyPlayers( Util.setToString(players) );                          chest.setModifyPlayers( Util.setToString(players) );
230                          plugin.getDatabase().save( chest );                          plugin.getDatabase().save( chest );
231                          player.sendMessage("ok");                          player.sendMessage("ok");
232                          return true;                          return true;
233                  }                  }
234                                    if (cmd.equals("comment")) {    
235                            if (chest == null) {
236                                    player.sendMessage("This chest is not protected");
237                                    return true;
238                            }
239                            if (! chest.getOwner().equals( player.getName() ) ) {                          
240                                    player.sendMessage("You can not comment a chest you don't own");
241                                    server.getLogger().info( player.getName() + " tried to comment on a chest owned by " + chest.getOwner() );
242                                    return true;
243                            }
244    
245                            StringBuilder sb = new StringBuilder();
246                            for (int i=1; i<args.length; i++) {
247                                    sb.append(args[i]).append(" ");
248                            }
249                            String comment = sb.toString().trim();
250    
251                            chest.setComment( comment );
252                            plugin.getDatabase().save( chest );
253                            player.sendMessage("Comment set");
254    
255                            return true;
256                    }
257    
258                  /*                  /*
259                  if (chest != null) {                  if (chest != null) {
260                          if (chest.getOwner().equals(player.getName())) {                          if (chest.getOwner().equals(player.getName())) {
# Line 195  public class AdvancedChest  implements L Line 263  public class AdvancedChest  implements L
263                          } else {                          } else {
264                                  player.sendMessage("[LockedChest] Chest is already protected");                                  player.sendMessage("[LockedChest] Chest is already protected");
265                          }                          }
266                            
267                          return true;                          return true;
268                  }                  }
269                    
270                  chest = createChest(player.getName(), "", loc);                          chest = createChest(player.getName(), "", loc);        
271                  if (loc2 != null) {                  if (loc2 != null) {
272                          chest.setDoublechest(true);                          chest.setDoublechest(true);
273                  }                  }
274                    
275                  addChest(loc, chest);                  addChest(loc, chest);
276                    
277            
278                  player.sendMessage("[LockedChest] Chest is now locked");                  player.sendMessage("[LockedChest] Chest is now locked");
279                  */                   */
280                    
281                  player.sendMessage("Unknown argument, " + cmd);                  player.sendMessage("Unknown argument, " + cmd);
282                    
283                  return true;                  return true;
284          }          }
285            
286          @EventHandler          @EventHandler
287          public void onBlockBreak(BlockBreakEvent event) {          public void onBlockBreak(BlockBreakEvent event) {
288                  Location loc = event.getBlock().getLocation();                  Location loc = event.getBlock().getLocation();
# Line 226  public class AdvancedChest  implements L Line 294  public class AdvancedChest  implements L
294                          } else {                          } else {
295                                  event.setCancelled(true);                                  event.setCancelled(true);
296                                  event.getPlayer().sendMessage("You can't destroy that chest");                                  event.getPlayer().sendMessage("You can't destroy that chest");
297                                  server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried breaking a chest owned by " + chest.getOwner() );                                  server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried breaking a chest owned by " + chest.getOwner() + chest.getCommentString() );
298                          }                          }
299                  }                  }
300          }          }
# Line 237  public class AdvancedChest  implements L Line 305  public class AdvancedChest  implements L
305                          chestMap.put(loc2, chest);                          chestMap.put(loc2, chest);
306                  }                                }              
307                  plugin.getDatabase().save(chest);                  plugin.getDatabase().save(chest);
308                    
309                  reloadChests();                  reloadChests();
310                    
311          }          }
312            
313          void removeChest(Location loc) {          void removeChest(Location loc) {
314                  ChestBean chest = chestMap.remove(loc);                  ChestBean chest = chestMap.remove(loc);
315                  if (chest != null) {                                                                                      if (chest != null) {                                                                    
# Line 252  public class AdvancedChest  implements L Line 320  public class AdvancedChest  implements L
320                          plugin.getDatabase().delete(chest);                          plugin.getDatabase().delete(chest);
321                  }                  }
322          }          }
323            
324          int loadChestsWorker() {          int loadChestsWorker() {
325                  List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList();                  List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList();
326                  for (ChestBean chest : chestlist) {                  for (ChestBean chest : chestlist) {
327                          Location loc = getChestLocation(server, chest);                          Location loc = getChestLocation(server, chest);
328                          chestMap.put(loc, chest);                                chestMap.put(loc, chest);      
329                            
330                          if (chest.isDoublechest()) {                          if (chest.isDoublechest()) {
331                                  Location loc2 = getNeighborChest(loc);                                  Location loc2 = getNeighborChest(loc);
332                                  chestMap.put(loc2, chest);                                        chestMap.put(loc2, chest);      
# Line 267  public class AdvancedChest  implements L Line 335  public class AdvancedChest  implements L
335    
336                  return chestlist.size();                  return chestlist.size();
337          }          }
338            
339          void reloadChests() {                    void reloadChests() {          
340                  chestMap.clear();                  chestMap.clear();
341                  loadChestsWorker();                              loadChestsWorker();            
342          }          }
343            
344          void loadChests() {                      void loadChests() {            
345                  int count = loadChestsWorker();                  int count = loadChestsWorker();
346                  server.getLogger().info("[AdvancedChest] loaded " + count + " chests");                  server.getLogger().info("[AdvancedChest] loaded " + count + " chests");
347          }          }
348            
349            
350          public ChestBean createChest(String owner, String description, Location loc) {          public ChestBean createChest(String owner, String description, Location loc) {
351                    
352                  ChestBean chest = new ChestBean();                  ChestBean chest = new ChestBean();
353                  chest.setOwner(owner);                  chest.setOwner(owner);
354                  chest.setDescription(description);                  chest.setDescription(description);
# Line 288  public class AdvancedChest  implements L Line 356  public class AdvancedChest  implements L
356    
357                  return chest;                  return chest;
358          }          }
359            
360            
361          public void setChestLocation(ChestBean chest, Location loc) {          public void setChestLocation(ChestBean chest, Location loc) {
362                  chest.setWorld( loc.getWorld().getName() );                  chest.setWorld( loc.getWorld().getName() );
363                  chest.setX( loc.getBlockX() );                  chest.setX( loc.getBlockX() );
364                  chest.setY( loc.getBlockY() );                  chest.setY( loc.getBlockY() );
365                  chest.setZ( loc.getBlockZ() );                            chest.setZ( loc.getBlockZ() );          
366          }          }
367            
368          public Location getChestLocation(Server server, ChestBean chest) {          public Location getChestLocation(Server server, ChestBean chest) {
369                  World wrld = server.getWorld(chest.getWorld());                  World wrld = server.getWorld(chest.getWorld());
370                  return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());                                return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());              
371          }          }
372            
373    
374          /*          /*
375          void saveChests() {          void saveChests() {
376                    
377          }*/          }*/
378            
379          Location getNeighborChest(Location loc) {          Location getNeighborChest(Location loc) {
380                  World world = loc.getWorld();                  World world = loc.getWorld();
381                    
382                  Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() );                  Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() );
383                  if (world.getBlockAt(target).getType() == Material.CHEST )                  if (world.getBlockAt(target).getType() == Material.CHEST )
384                          return target;                          return target;
385                    
386                  target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() );                  target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() );
387                  if (world.getBlockAt(target).getType() == Material.CHEST )                  if (world.getBlockAt(target).getType() == Material.CHEST )
388                          return target;                                    return target;          
# Line 326  public class AdvancedChest  implements L Line 394  public class AdvancedChest  implements L
394                  target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1);                  target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1);
395                  if (world.getBlockAt(target).getType() == Material.CHEST )                  if (world.getBlockAt(target).getType() == Material.CHEST )
396                          return target;                                                                    return target;                                          
397                    
398                  return null;                  return null;
399          }          }
400            
401            
402          Location getChestLocation(InventoryHolder holder) {          Location getChestLocation(InventoryHolder holder) {
403                  Location loc;                  Location loc;
404                  if ( holder instanceof Chest) {                  if ( holder instanceof Chest) {
# Line 338  public class AdvancedChest  implements L Line 406  public class AdvancedChest  implements L
406                  } else {                  } else {
407                          loc =  ( (DoubleChest)holder).getLocation();                          loc =  ( (DoubleChest)holder).getLocation();
408                  }                  }
409                    
410                  loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5                  loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5
411                  loc.setZ( loc.getBlockZ() ); // -- // --                  loc.setZ( loc.getBlockZ() ); // -- // --
412                    
413                  return loc;                  return loc;
414          }          }
415            
416          @EventHandler          @EventHandler
417          public void onChestPlaced(BlockPlaceEvent event) {          public void onChestPlaced(BlockPlaceEvent event) {
418                  Block block = event.getBlock();                  Block block = event.getBlock();
419                    
420                  if (block.getType() != Material.CHEST) {                  if (block.getType() != Material.CHEST) {
421                          return;                          return;
422                  }                  }
423                    
424                  Location chestloc = getNeighborChest( block.getLocation() );                  Location chestloc = getNeighborChest( block.getLocation() );
425                  if (chestloc != null) {                  if (chestloc == null)
426                          ChestBean chest = chestMap.get(chestloc);                          return;
                           
                         if (chest != null) { //the neighbor is a locked chest  
427    
428                                                    ChestBean chest = chestMap.get(chestloc);
429                                  chest.setDoublechest(true);                  
430                                  addChest(chestloc, chest);                  if (chest == null)//the neighbor is not a locked chest
431                                                            return;
432                                                    
433                                  event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" );  
434                          }                                        chest.setDoublechest(true);
435                                            addChest(chestloc, chest);
436                  }  
437    
438                    event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" );
439          }          }
440    
441    
# Line 376  public class AdvancedChest  implements L Line 444  public class AdvancedChest  implements L
444                  for (Block b : event.blockList() ) {                  for (Block b : event.blockList() ) {
445                          ChestBean chest = chestMap.get( b.getLocation() );                          ChestBean chest = chestMap.get( b.getLocation() );
446                          if (chest != null) {                          if (chest != null) {
447                                  server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() );                                  server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() +  chest.getCommentString() );
448                                  event.setCancelled( true );                                  event.setCancelled( true );
449                                  return;                                  return;
450                          }                          }
451                  }                  }
452          }                }
453            
454            @EventHandler
455            public void onChestBurn(BlockBurnEvent event) {
456                    ChestBean chest = chestMap.get( event.getBlock().getLocation() );
457                    if (chest != null) {
458                            server.getLogger().info( "[AdvancedChest] prevented a fire from destrying chest owned by " + chest.getOwner() + chest.getCommentString()  );
459                            event.setCancelled( true);
460                    }
461            }
462    
463          // prevent a user from opening a chest          // prevent a user from opening a chest
464          @EventHandler          @EventHandler
465          public void onChestInteract(PlayerInteractEvent event) {          public void onChestInteract(PlayerInteractEvent event) {
466                  if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {                  if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
467                          Block b = event.getClickedBlock();                          Block b = event.getClickedBlock();
468                            
469                          if (b.getType() == Material.CHEST) {                          if (b.getType() == Material.CHEST) {
470                                    
471                                  Location loc = b.getLocation();                                  Location loc = b.getLocation();
472                                    
473                                  ChestBean chest = chestMap.get( loc );                                  ChestBean chest = chestMap.get( loc );
474                                  if (chest == null) {                                                              if (chest == null) {                            
475                                          return; //chest not surveyed by this plugin                                          return; //chest not surveyed by this plugin
476                                  }                                  }
477                                    
478                                  if (chest.getChestType() != ChestBean.LOCKED ) {                                  if (chest.getChestType() != ChestBean.LOCKED ) {
479                                          return; //this is not a locked chests                                          return; //this is not a locked chests
480                                  }                                  }
481                                    
482                                  Player player = (Player) event.getPlayer();                                  Player player = (Player) event.getPlayer();
483                                  if (player.getName().equals(chest.getOwner() )) {                                  if (player.getName().equals(chest.getOwner() )) {
484                                          return; //chest is opened by it's owner                                          return; //chest is opened by it's owner
485                                  }                                  }
486                                    
487                                  Set<String> players = Util.stringToSet( chest.getModifyPlayers() );                                  Set<String> players = chest.getModifyPlayersSet() ;
488                                  if ( players.contains(player.getName()) ) {                                  if ( players.contains(player.getName()) ) {
489                                          return; //this player is on the whitelist so he may open                                          return; //this player is on the whitelist so he may open
490                                  }                                  }
491    
492    
493                                  server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner() );                                  server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner()  + chest.getCommentString() );
494                                  player.sendMessage( ChatColor.BLUE +  "Sorry but this chest is locked !");                                  player.sendMessage( ChatColor.BLUE +  "Sorry but this chest is locked !");
495                                  event.setCancelled(true);                                  event.setCancelled(true);
496                          }                          }
# Line 422  public class AdvancedChest  implements L Line 499  public class AdvancedChest  implements L
499    
500          @EventHandler          @EventHandler
501          public void onChestOpen(InventoryOpenEvent event) {          public void onChestOpen(InventoryOpenEvent event) {
502                    
503                  if (! (event.getPlayer() instanceof Player)) {                  if (! (event.getPlayer() instanceof Player)) {
504                          return;                          return;
505                  }                  }
506    
507    
508                    
509                  InventoryHolder holder = event.getInventory().getHolder();                  InventoryHolder holder = event.getInventory().getHolder();
510                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
511                          Location loc = getChestLocation(holder);                          Location loc = getChestLocation(holder);
512                            
513                          ChestBean chest = chestMap.get( loc );                          ChestBean chest = chestMap.get( loc );
514                          if (chest == null) {                                                      if (chest == null) {                            
515                                  return; //chest not surveyed by this plugin                                  return; //chest not surveyed by this plugin
516                          }                          }
517                            
518                          if (chest.getChestType() != ChestBean.SNITCHING) {                          if (chest.getChestType() != ChestBean.SNITCHING) {
519                                  return; // not a snitching chest                                  return; // not a snitching chest
520                          }                          }
521                            
522                                                    
523                          Player player = (Player) event.getPlayer();                          Player player = (Player) event.getPlayer();
524                          if (player.getName().equals(chest.getOwner() )) {                          if (player.getName().equals(chest.getOwner() )) {
525                                  return; //chest is owned by it's own player                                  return; //chest is owned by it's own player
526                          }                          }
527                            
528                          Set<String> players = Util.stringToSet( chest.getModifyPlayers() );                          Set<String> players = chest.getModifyPlayersSet();
529                          if ( players.contains(player.getName()) ) {                          if ( players.contains(player.getName()) ) {
530                                  return; //this player is on the whitelist so he may open                                  return; //this player is on the whitelist so he may open
531                          }                          }
532                            
533                            
534                            server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " opened a snitching chest owned by " + chest.getOwner()  + chest.getCommentString()  );
535    
536                          ItemCount contents = countItems( event.getInventory().getContents() );                                            ItemCount contents = countItems( event.getInventory().getContents() );                  
537                            
538                          contentMap.put(player.getName(), contents );                          contentMap.put(player.getName(), contents );
539                  }                  }
540          }          }
541            
542          @EventHandler          @EventHandler
543          public void onChestClose(InventoryCloseEvent event) {          public void onChestClose(InventoryCloseEvent event) {
544                  if (! (event.getPlayer() instanceof Player)) {                  if (! (event.getPlayer() instanceof Player)) {
545                          return;                          return;
546                  }                  }
547                    
548                    
549                  InventoryHolder holder = event.getInventory().getHolder();                  InventoryHolder holder = event.getInventory().getHolder();
550                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
551                          Location loc = getChestLocation(holder);                          Location loc = getChestLocation(holder);
552                          ChestBean chest = chestMap.get(loc);                          ChestBean chest = chestMap.get(loc);
553                            
554                          if (chest == null) { //chest was not a snitching chest                          if (chest == null) { //chest was not a snitching chest
555                                  return;                                  return;
556                          }                          }
557                            
558                          if (chest.getChestType() != ChestBean.SNITCHING) {                          if (chest.getChestType() != ChestBean.SNITCHING) {
559                                  return; // not a snitching chest                                  return; // not a snitching chest
560                          }                          }
561                            
562                            
563                          OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );                          OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
564                            
565                            
566                          Player player = (Player) event.getPlayer();                          Player player = (Player) event.getPlayer();
567                            
568                          ItemCount savedContent = contentMap.get( player.getName() );                          ItemCount savedContent = contentMap.get( player.getName() );
569                            
570                          if (savedContent == null) {                          if (savedContent == null) {
571                                  return;                                  return;
572                          }                          }
573                            
574                          contentMap.remove( player.getName() );                          contentMap.remove( player.getName() );
575                            
576                          ItemCount content = countItems( event.getInventory().getContents() );                          ItemCount content = countItems( event.getInventory().getContents() );
577                                                    
578                          Set<Integer> combinedKeyset = new TreeSet<Integer>();                                            Set<Integer> combinedKeyset = new TreeSet<Integer>();                  
579                          combinedKeyset.addAll( savedContent.keySet() );                          combinedKeyset.addAll( savedContent.keySet() );
580                          combinedKeyset.addAll( content.keySet() );                          combinedKeyset.addAll( content.keySet() );
581                            
582                          for (Integer item : combinedKeyset ) {                          for (Integer item : combinedKeyset ) {
583                                  Integer savedcount = savedContent.get(item);                                                              Integer savedcount = savedContent.get(item);                            
584                                  Integer count = content.get(item);                                  Integer count = content.get(item);
585                                    
586                                  if (savedcount == null)                                  if (savedcount == null)
587                                          savedcount = 0;                                          savedcount = 0;
588                                  if (count == null)                                  if (count == null)
589                                          count = 0;                                          count = 0;
590                                    
591                                    
592                                  int diff = Math.abs( savedcount - count);                                  int diff = Math.abs( savedcount - count);
593                                    
594                                  if (diff > 0) {                                  if (diff > 0) {
595                                          String material = Material.getMaterial(item).name();                                          String material = Material.getMaterial(item).name();
596                                          String msg = null;                                          String msg = null;
597                                            
598                                          if (count > savedcount) {                                          if (count > savedcount) {
599                                                  msg = player.getName() + " added "   + diff + " units of " + material + "(" +item + ") to "   + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + 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() + chest.getCommentString();
600                                          } else { //(count < savedcount)                                          } else { //(count < savedcount)
601                                                  msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + 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() + chest.getCommentString();
602                                          }                                          }
603                                    
604                                    
605                                          server.getLogger().info( "[AdvancedChest]" + msg);                                          server.getLogger().info( "[AdvancedChest]" + msg);
606                                          plugin.getMessageWrapper().sendMessage("system", owner, msg);                                          plugin.getMessageWrapper().sendMessage("system", owner, msg);
607                                  }                                  }
608                                    
609                          }                          }
610                            
611                            
612                  }                  }
613          }          }
614            
615          ItemCount countItems(ItemStack[] input) {          ItemCount countItems(ItemStack[] input) {
616                  ItemCount output = new ItemCount();                  ItemCount output = new ItemCount();
617                  for (int i=0; i<input.length; i++) {                  for (int i=0; i<input.length; i++) {
618                          ItemStack current = input[i];                          ItemStack current = input[i];
619                          if (current == null)                          if (current == null)
620                                  continue;                                  continue;
621                            
622                          int type = current.getTypeId();                          int type = current.getTypeId();
623                            
624                          Integer amount = output.get(type);                          Integer amount = output.get(type);
625                          if (amount == null)                          if (amount == null)
626                                  amount = 0;                                  amount = 0;
627                            
628                          output.put(type, amount + current.getAmount() );                                                  output.put(type, amount + current.getAmount() );                        
629                  }                  }
630                  return output;                  return output;
# Line 562  public class AdvancedChest  implements L Line 641  public class AdvancedChest  implements L
641                  }                  }
642                  return output;                  return output;
643          }*/          }*/
644            
645            
646  }  }

Legend:
Removed from v.1853  
changed lines
  Added in v.2432

  ViewVC Help
Powered by ViewVC 1.1.20