/[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 1811 by torben, Tue Jun 5 09:30:32 2012 UTC revision 2429 by torben, Mon Mar 9 12:29:22 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(null, 30);                                        Block b = player.getTargetBlock( (Set<Material>)null, 30);                      
87                    
88                  if (b.getTypeId() != 54) {                  if (b.getTypeId() != 54 && b.getTypeId() != 154) {
89                          player.sendMessage("[Chest] Please look at the chest you want to protect");                          player.sendMessage("[Chest] Please look at the chest/hopper you want to protect");
90                          return true;                          return true;
91                  }                  }
92                    
93                  Location loc = b.getLocation();                  Location loc = b.getLocation();
94                  Location loc2 = getNeighborChest(loc);                  Location loc2 = null;
95                                    if ( b.getTypeId() == 54) { //dont find neighbours for Hoppers
96                            loc2 = getNeighborChest(loc);
97                    }
98    
99                  ChestBean chest = chestMap.get(loc);                  ChestBean chest = chestMap.get(loc);
100                  String cmd = args[0].toLowerCase();                  String cmd = args[0].toLowerCase();
101                    
102                  if (cmd.equals("status")) {                  if (cmd.equals("status")) {
103                          if (chest != null) {                                                      if (chest != null) {                            
104                                  String mode = "";                                  String mode = "";
# Line 107  public class AdvancedChest  implements L Line 112  public class AdvancedChest  implements L
112                                  default:                                  default:
113                                          mode = "unknown ??";                                          mode = "unknown ??";
114                                  }                                                                }                              
115                                    
116                                  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());
117                                  player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() );                                  player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() );
118                                    player.sendMessage(ChatColor.GREEN + "Comment: " + chest.getComment() );
119                          } else {                          } else {
120                                  player.sendMessage(ChatColor.GREEN + "The chest is not protected");                                  player.sendMessage(ChatColor.GREEN + "The chest is not protected");
121                          }                          }
122                          return true;                          return true;
123                  }                  }
124                    
125                  if (cmd.equals("lock") || cmd.equals("snitch")) {                  if (cmd.equals("lock") || cmd.equals("snitch")) {
126                          if (chest == null) {                          if (chest == null) {
127                                  chest = createChest(player.getName(), "", loc);                                          chest = createChest(player.getName(), "", loc);        
# Line 131  public class AdvancedChest  implements L Line 137  public class AdvancedChest  implements L
137                                          modeStr = "snitching";                                          modeStr = "snitching";
138                                  }                                  }
139                                  chest.setModifyPlayers("");                                  chest.setModifyPlayers("");
140                                    chest.setComment("");
141                                  addChest(loc, chest);                                  addChest(loc, chest);
142                                  player.sendMessage("Chest is now " + modeStr);                                  player.sendMessage("Chest is now " + modeStr);
143                          } else {                          } else {
144                                    server.getLogger().info( player.getName() + " tried to protect a chest owned by " + chest.getOwner() );
145                                  player.sendMessage("This chest is already protected");                                  player.sendMessage("This chest is already protected");
146                          }                          }
147                          return true;                          return true;
148                  }                  }
149                    
150                  if (cmd.equals("remove")) {                  if (cmd.equals("remove")) {
151                          if (chest != null) {                          if (chest == null) {
152                                  player.sendMessage("[LockedChest] Removing protection from chest");                                  player.sendMessage("This chest is not protected");
153                                  removeChest(loc);                                                                return true;
154                            }
155                            if ( chest.getOwner().equals( player.getName() ) || player.isOp() ) {
156                                    //do nothing
157                          } else {                          } else {
158                                  player.sendMessage("This chest is not protected");                                        player.sendMessage("You can not remove lock from a chest you don't own");
159                                    server.getLogger().info( player.getName() + " tried to remove protection froma chest owned by " + chest.getOwner() );
160                                    return true;
161                          }                          }
162    
163                            player.sendMessage("[LockedChest] Removing protection from chest");
164                            removeChest(loc);                              
165    
166                          return true;                          return true;
167                  }                  }
168                    
169                    if (cmd.equals("setowner")) {
170                            if (chest == null) {
171                                    player.sendMessage("This chest is not protected");
172                                    return true;
173                            }
174                            if ( chest.getOwner().equals( player.getName() ) || player.isOp() ) {
175                                    //do nothing
176                            } else {
177                                    player.sendMessage("You can not set new owner of a chest you dont own");
178                                    server.getLogger().info( player.getName() + " tried to set owner on a chest owned by " + chest.getOwner() );
179                                    return true;
180                            }
181                            if (args.length != 2) {
182                                    player.sendMessage("You need to specify which player should own this chest");
183                                    return true;
184                            }
185                            OfflinePlayer p2 = server.getOfflinePlayer(args[1]);
186                            if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) {
187                                    player.sendMessage("Unknown user: " + args[1] );
188                                    return true;
189                            }
190                            chest.setOwner( p2.getName() );
191                            plugin.getDatabase().save( chest );
192                            player.sendMessage("ok");
193                            return true;
194                    }
195    
196                  if (cmd.equals("addplayer") || cmd.equals("removeplayer")) {                  if (cmd.equals("addplayer") || cmd.equals("removeplayer")) {
197                          if (chest == null) {                          if (chest == null) {
198                                  player.sendMessage("This chest is not protected");                                  player.sendMessage("This chest is not protected");
199                                  return true;                                  return true;
200                          }                          }
201                            if (! chest.getOwner().equals( player.getName() ) ) {
202                                    player.sendMessage("You can not add/remove players from a chest you don't own");
203                                    server.getLogger().info( player.getName() + " tried to add/remove player on a chest owned by " + chest.getOwner() );
204                                    return true;
205                            }
206                          if (args.length != 2) {                          if (args.length != 2) {
207                                  player.sendMessage("You need to specify which player to add or remove");                                  player.sendMessage("You need to specify which player to add or remove");
208                                  return true;                                  return true;
# Line 163  public class AdvancedChest  implements L Line 212  public class AdvancedChest  implements L
212                                  player.sendMessage("Unknown user: " + args[1] );                                  player.sendMessage("Unknown user: " + args[1] );
213                                  return true;                                  return true;
214                          }                          }
215                            
216                          Set<String> players = Util.stringToSet( chest.getModifyPlayers() );                          Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
217                          if (cmd.equals("addplayer")) {                          if (cmd.equals("addplayer")) {
218                                  players.add(p2.getName());                                  players.add(p2.getName());
219                          } else {                          } else {
220                                  players.remove(p2.getName());                                  players.remove(p2.getName());
221                          }                          }
222                            
223                          chest.setModifyPlayers( Util.setToString(players) );                          chest.setModifyPlayers( Util.setToString(players) );
224                          plugin.getDatabase().save( chest );                          plugin.getDatabase().save( chest );
225                          player.sendMessage("ok");                          player.sendMessage("ok");
226                          return true;                          return true;
227                  }                  }
228                                    if (cmd.equals("comment")) {    
229                            if (chest == null) {
230                                    player.sendMessage("This chest is not protected");
231                                    return true;
232                            }
233                            if (! chest.getOwner().equals( player.getName() ) ) {                          
234                                    player.sendMessage("You can not comment a chest you don't own");
235                                    server.getLogger().info( player.getName() + " tried to comment on a chest owned by " + chest.getOwner() );
236                                    return true;
237                            }
238    
239                            StringBuilder sb = new StringBuilder();
240                            for (int i=1; i<args.length; i++) {
241                                    sb.append(args[i]).append(" ");
242                            }
243                            String comment = sb.toString().trim();
244    
245                            chest.setComment( comment );
246                            plugin.getDatabase().save( chest );
247                            player.sendMessage("Comment set");
248    
249                            return true;
250                    }
251    
252                  /*                  /*
253                  if (chest != null) {                  if (chest != null) {
254                          if (chest.getOwner().equals(player.getName())) {                          if (chest.getOwner().equals(player.getName())) {
# Line 185  public class AdvancedChest  implements L Line 257  public class AdvancedChest  implements L
257                          } else {                          } else {
258                                  player.sendMessage("[LockedChest] Chest is already protected");                                  player.sendMessage("[LockedChest] Chest is already protected");
259                          }                          }
260                            
261                          return true;                          return true;
262                  }                  }
263                    
264                  chest = createChest(player.getName(), "", loc);                          chest = createChest(player.getName(), "", loc);        
265                  if (loc2 != null) {                  if (loc2 != null) {
266                          chest.setDoublechest(true);                          chest.setDoublechest(true);
267                  }                  }
268                    
269                  addChest(loc, chest);                  addChest(loc, chest);
270                    
271            
272                  player.sendMessage("[LockedChest] Chest is now locked");                  player.sendMessage("[LockedChest] Chest is now locked");
273                  */                   */
274                    
275                  player.sendMessage("Unknown argument, " + cmd);                  player.sendMessage("Unknown argument, " + cmd);
276                    
277                  return true;                  return true;
278          }          }
279            
280          @EventHandler          @EventHandler
281          public void onBlockBreak(BlockBreakEvent event) {          public void onBlockBreak(BlockBreakEvent event) {
282                  Location loc = event.getBlock().getLocation();                  Location loc = event.getBlock().getLocation();
# Line 216  public class AdvancedChest  implements L Line 288  public class AdvancedChest  implements L
288                          } else {                          } else {
289                                  event.setCancelled(true);                                  event.setCancelled(true);
290                                  event.getPlayer().sendMessage("You can't destroy that chest");                                  event.getPlayer().sendMessage("You can't destroy that chest");
291                                  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() );
292                          }                          }
293                  }                  }
294          }          }
# Line 227  public class AdvancedChest  implements L Line 299  public class AdvancedChest  implements L
299                          chestMap.put(loc2, chest);                          chestMap.put(loc2, chest);
300                  }                                }              
301                  plugin.getDatabase().save(chest);                  plugin.getDatabase().save(chest);
302                    
303                  reloadChests();                  reloadChests();
304                    
305          }          }
306            
307          void removeChest(Location loc) {          void removeChest(Location loc) {
308                  ChestBean chest = chestMap.remove(loc);                  ChestBean chest = chestMap.remove(loc);
309                  if (chest != null) {                                                                                      if (chest != null) {                                                                    
# Line 242  public class AdvancedChest  implements L Line 314  public class AdvancedChest  implements L
314                          plugin.getDatabase().delete(chest);                          plugin.getDatabase().delete(chest);
315                  }                  }
316          }          }
317            
318          int loadChestsWorker() {          int loadChestsWorker() {
319                  List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList();                  List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList();
320                  for (ChestBean chest : chestlist) {                  for (ChestBean chest : chestlist) {
321                          Location loc = getChestLocation(server, chest);                          Location loc = getChestLocation(server, chest);
322                          chestMap.put(loc, chest);                                chestMap.put(loc, chest);      
323                            
324                          if (chest.isDoublechest()) {                          if (chest.isDoublechest()) {
325                                  Location loc2 = getNeighborChest(loc);                                  Location loc2 = getNeighborChest(loc);
326                                  chestMap.put(loc2, chest);                                        chestMap.put(loc2, chest);      
# Line 257  public class AdvancedChest  implements L Line 329  public class AdvancedChest  implements L
329    
330                  return chestlist.size();                  return chestlist.size();
331          }          }
332            
333          void reloadChests() {                    void reloadChests() {          
334                  chestMap.clear();                  chestMap.clear();
335                  loadChestsWorker();                              loadChestsWorker();            
336          }          }
337            
338          void loadChests() {                      void loadChests() {            
339                  int count = loadChestsWorker();                  int count = loadChestsWorker();
340                  plugin.getLogger().info("[AdvancedChest] loaded " + count + " chests");                  server.getLogger().info("[AdvancedChest] loaded " + count + " chests");
341          }          }
342            
343            
344          public ChestBean createChest(String owner, String description, Location loc) {          public ChestBean createChest(String owner, String description, Location loc) {
345                    
346                  ChestBean chest = new ChestBean();                  ChestBean chest = new ChestBean();
347                  chest.setOwner(owner);                  chest.setOwner(owner);
348                  chest.setDescription(description);                  chest.setDescription(description);
# Line 278  public class AdvancedChest  implements L Line 350  public class AdvancedChest  implements L
350    
351                  return chest;                  return chest;
352          }          }
353            
354            
355          public void setChestLocation(ChestBean chest, Location loc) {          public void setChestLocation(ChestBean chest, Location loc) {
356                  chest.setWorld( loc.getWorld().getName() );                  chest.setWorld( loc.getWorld().getName() );
357                  chest.setX( loc.getBlockX() );                  chest.setX( loc.getBlockX() );
358                  chest.setY( loc.getBlockY() );                  chest.setY( loc.getBlockY() );
359                  chest.setZ( loc.getBlockZ() );                            chest.setZ( loc.getBlockZ() );          
360          }          }
361            
362          public Location getChestLocation(Server server, ChestBean chest) {          public Location getChestLocation(Server server, ChestBean chest) {
363                  World wrld = server.getWorld(chest.getWorld());                  World wrld = server.getWorld(chest.getWorld());
364                  return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());                                return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());              
365          }          }
366            
367    
368          /*          /*
369          void saveChests() {          void saveChests() {
370                    
371          }*/          }*/
372            
373          Location getNeighborChest(Location loc) {          Location getNeighborChest(Location loc) {
374                  World world = loc.getWorld();                  World world = loc.getWorld();
375                    
376                  Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() );                  Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() );
377                  if (world.getBlockAt(target).getType() == Material.CHEST )                  if (world.getBlockAt(target).getType() == Material.CHEST )
378                          return target;                          return target;
379                    
380                  target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() );                  target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() );
381                  if (world.getBlockAt(target).getType() == Material.CHEST )                  if (world.getBlockAt(target).getType() == Material.CHEST )
382                          return target;                                    return target;          
# Line 316  public class AdvancedChest  implements L Line 388  public class AdvancedChest  implements L
388                  target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1);                  target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1);
389                  if (world.getBlockAt(target).getType() == Material.CHEST )                  if (world.getBlockAt(target).getType() == Material.CHEST )
390                          return target;                                                                    return target;                                          
391                    
392                  return null;                  return null;
393          }          }
394            
395            
396          Location getChestLocation(InventoryHolder holder) {          Location getChestLocation(InventoryHolder holder) {
397                  Location loc;                  Location loc;
398                  if ( holder instanceof Chest) {                  if ( holder instanceof Chest) {
# Line 328  public class AdvancedChest  implements L Line 400  public class AdvancedChest  implements L
400                  } else {                  } else {
401                          loc =  ( (DoubleChest)holder).getLocation();                          loc =  ( (DoubleChest)holder).getLocation();
402                  }                  }
403                    
404                  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
405                  loc.setZ( loc.getBlockZ() ); // -- // --                  loc.setZ( loc.getBlockZ() ); // -- // --
406                    
407                  return loc;                  return loc;
408          }          }
409            
410          @EventHandler          @EventHandler
411          public void onChestPlaced(BlockPlaceEvent event) {          public void onChestPlaced(BlockPlaceEvent event) {
412                  Block block = event.getBlock();                  Block block = event.getBlock();
413                    
414                  if (block.getType() != Material.CHEST) {                  if (block.getType() != Material.CHEST) {
415                          return;                          return;
416                  }                  }
417                    
418                  Location chestloc = getNeighborChest( block.getLocation() );                  Location chestloc = getNeighborChest( block.getLocation() );
419                  if (chestloc != null) {                  if (chestloc == null)
420                          ChestBean chest = chestMap.get(chestloc);                          return;
421                            
422                          if (chest != null) { //the neighbor is a locked chest                  ChestBean chest = chestMap.get(chestloc);
423                    
424                                                    if (chest == null)//the neighbor is not a locked chest
425                                  chest.setDoublechest(true);                          return;
426                                  addChest(chestloc, chest);                  
427                                    
428                                                    chest.setDoublechest(true);
429                                  event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" );                  addChest(chestloc, chest);
430                          }                        
431                            
432                  }                  event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" );
433          }          }
434    
435    
# Line 366  public class AdvancedChest  implements L Line 438  public class AdvancedChest  implements L
438                  for (Block b : event.blockList() ) {                  for (Block b : event.blockList() ) {
439                          ChestBean chest = chestMap.get( b.getLocation() );                          ChestBean chest = chestMap.get( b.getLocation() );
440                          if (chest != null) {                          if (chest != null) {
441                                  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() );
442                                  event.setCancelled( true );                                  event.setCancelled( true );
443                                  return;                                  return;
444                          }                          }
445                  }                  }
446          }                }
447            
448            @EventHandler
449            public void onChestBurn(BlockBurnEvent event) {
450                    ChestBean chest = chestMap.get( event.getBlock().getLocation() );
451                    if (chest != null) {
452                            server.getLogger().info( "[AdvancedChest] prevented a fire from destrying chest owned by " + chest.getOwner() + chest.getCommentString()  );
453                            event.setCancelled( true);
454                    }
455            }
456    
457          // prevent a user from opening a chest          // prevent a user from opening a chest
458          @EventHandler          @EventHandler
459          public void onChestInteract(PlayerInteractEvent event) {          public void onChestInteract(PlayerInteractEvent event) {
460                  if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {                  if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
461                          Block b = event.getClickedBlock();                          Block b = event.getClickedBlock();
462                            
463                          if (b.getType() == Material.CHEST) {                          if (b.getType() == Material.CHEST) {
464                                    
465                                  Location loc = b.getLocation();                                  Location loc = b.getLocation();
466                                    
467                                  ChestBean chest = chestMap.get( loc );                                  ChestBean chest = chestMap.get( loc );
468                                  if (chest == null) {                                                              if (chest == null) {                            
469                                          return; //chest not surveyed by this plugin                                          return; //chest not surveyed by this plugin
470                                  }                                  }
471                                    
472                                  if (chest.getChestType() != ChestBean.LOCKED ) {                                  if (chest.getChestType() != ChestBean.LOCKED ) {
473                                          return; //this is not a locked chests                                          return; //this is not a locked chests
474                                  }                                  }
475                                    
476                                  Player player = (Player) event.getPlayer();                                  Player player = (Player) event.getPlayer();
477                                  if (player.getName().equals(chest.getOwner() )) {                                  if (player.getName().equals(chest.getOwner() )) {
478                                          return; //chest is opened by it's owner                                          return; //chest is opened by it's owner
479                                  }                                  }
480                                    
481                                  Set<String> players = Util.stringToSet( chest.getModifyPlayers() );                                  Set<String> players = chest.getModifyPlayersSet() ;
482                                  if ( players.contains(player.getName()) ) {                                  if ( players.contains(player.getName()) ) {
483                                          return; //this player is on the whitelist so he may open                                          return; //this player is on the whitelist so he may open
484                                  }                                  }
485    
486    
487                                  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() );
488                                  player.sendMessage( ChatColor.BLUE +  "Sorry but this chest is locked !");                                  player.sendMessage( ChatColor.BLUE +  "Sorry but this chest is locked !");
489                                  event.setCancelled(true);                                  event.setCancelled(true);
490                          }                          }
# Line 412  public class AdvancedChest  implements L Line 493  public class AdvancedChest  implements L
493    
494          @EventHandler          @EventHandler
495          public void onChestOpen(InventoryOpenEvent event) {          public void onChestOpen(InventoryOpenEvent event) {
496                    
497                  if (! (event.getPlayer() instanceof Player)) {                  if (! (event.getPlayer() instanceof Player)) {
498                          return;                          return;
499                  }                  }
500    
501    
502                    
503                  InventoryHolder holder = event.getInventory().getHolder();                  InventoryHolder holder = event.getInventory().getHolder();
504                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
505                          Location loc = getChestLocation(holder);                          Location loc = getChestLocation(holder);
506                            
507                          ChestBean chest = chestMap.get( loc );                          ChestBean chest = chestMap.get( loc );
508                          if (chest == null) {                                                      if (chest == null) {                            
509                                  return; //chest not surveyed by this plugin                                  return; //chest not surveyed by this plugin
510                          }                          }
511                            
512                          if (chest.getChestType() != ChestBean.SNITCHING) {                          if (chest.getChestType() != ChestBean.SNITCHING) {
513                                  return; // not a snitching chest                                  return; // not a snitching chest
514                          }                          }
515                            
516                                                    
517                          Player player = (Player) event.getPlayer();                          Player player = (Player) event.getPlayer();
518                          if (player.getName().equals(chest.getOwner() )) {                          if (player.getName().equals(chest.getOwner() )) {
519                                  return; //chest is owned by it's own player                                  return; //chest is owned by it's own player
520                          }                          }
521                            
522                          Set<String> players = Util.stringToSet( chest.getModifyPlayers() );                          Set<String> players = chest.getModifyPlayersSet();
523                          if ( players.contains(player.getName()) ) {                          if ( players.contains(player.getName()) ) {
524                                  return; //this player is on the whitelist so he may open                                  return; //this player is on the whitelist so he may open
525                          }                          }
526                            
527                            
528                            server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " opened a snitching chest owned by " + chest.getOwner()  + chest.getCommentString()  );
529    
530                          ItemCount contents = countItems( event.getInventory().getContents() );                                            ItemCount contents = countItems( event.getInventory().getContents() );                  
531                            
532                          contentMap.put(player.getName(), contents );                          contentMap.put(player.getName(), contents );
533                  }                  }
534          }          }
535            
536          @EventHandler          @EventHandler
537          public void onChestClose(InventoryCloseEvent event) {          public void onChestClose(InventoryCloseEvent event) {
538                  if (! (event.getPlayer() instanceof Player)) {                  if (! (event.getPlayer() instanceof Player)) {
539                          return;                          return;
540                  }                  }
541                    
542                    
543                  InventoryHolder holder = event.getInventory().getHolder();                  InventoryHolder holder = event.getInventory().getHolder();
544                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
545                          Location loc = getChestLocation(holder);                          Location loc = getChestLocation(holder);
546                          ChestBean chest = chestMap.get(loc);                          ChestBean chest = chestMap.get(loc);
547                            
548                          if (chest == null) { //chest was not a snitching chest                          if (chest == null) { //chest was not a snitching chest
549                                  return;                                  return;
550                          }                          }
551                            
552                          if (chest.getChestType() != ChestBean.SNITCHING) {                          if (chest.getChestType() != ChestBean.SNITCHING) {
553                                  return; // not a snitching chest                                  return; // not a snitching chest
554                          }                          }
555                            
556                            
557                          OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );                          OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
558                            
559                            
560                          Player player = (Player) event.getPlayer();                          Player player = (Player) event.getPlayer();
561                            
562                          ItemCount savedContent = contentMap.get( player.getName() );                          ItemCount savedContent = contentMap.get( player.getName() );
563                            
564                          if (savedContent == null) {                          if (savedContent == null) {
565                                  return;                                  return;
566                          }                          }
567                            
568                          contentMap.remove( player.getName() );                          contentMap.remove( player.getName() );
569                            
570                          ItemCount content = countItems( event.getInventory().getContents() );                          ItemCount content = countItems( event.getInventory().getContents() );
571                                                    
572                          Set<Integer> combinedKeyset = new TreeSet<Integer>();                                            Set<Integer> combinedKeyset = new TreeSet<Integer>();                  
573                          combinedKeyset.addAll( savedContent.keySet() );                          combinedKeyset.addAll( savedContent.keySet() );
574                          combinedKeyset.addAll( content.keySet() );                          combinedKeyset.addAll( content.keySet() );
575                            
576                          for (Integer item : combinedKeyset ) {                          for (Integer item : combinedKeyset ) {
577                                  Integer savedcount = savedContent.get(item);                                                              Integer savedcount = savedContent.get(item);                            
578                                  Integer count = content.get(item);                                  Integer count = content.get(item);
579                                    
580                                  if (savedcount == null)                                  if (savedcount == null)
581                                          savedcount = 0;                                          savedcount = 0;
582                                  if (count == null)                                  if (count == null)
583                                          count = 0;                                          count = 0;
584                                    
585                                    
586                                  int diff = Math.abs( savedcount - count);                                  int diff = Math.abs( savedcount - count);
587                                    
588                                  if (diff > 0) {                                  if (diff > 0) {
589                                          String material = Material.getMaterial(item).name();                                          String material = Material.getMaterial(item).name();
590                                          String msg = null;                                          String msg = null;
591                                            
592                                          if (count > savedcount) {                                          if (count > savedcount) {
593                                                  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();
594                                          } else { //(count < savedcount)                                          } else { //(count < savedcount)
595                                                  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();
596                                          }                                          }
597                                    
598                                    
599                                          plugin.getLogger().info(msg);                                          server.getLogger().info( "[AdvancedChest]" + msg);
600                                          plugin.getMessageWrapper().sendMessage("system", owner, msg);                                          plugin.getMessageWrapper().sendMessage("system", owner, msg);
601                                  }                                  }
602                                    
603                          }                          }
604                            
605                            
606                  }                  }
607          }          }
608            
609          ItemCount countItems(ItemStack[] input) {          ItemCount countItems(ItemStack[] input) {
610                  ItemCount output = new ItemCount();                  ItemCount output = new ItemCount();
611                  for (int i=0; i<input.length; i++) {                  for (int i=0; i<input.length; i++) {
612                          ItemStack current = input[i];                          ItemStack current = input[i];
613                          if (current == null)                          if (current == null)
614                                  continue;                                  continue;
615                            
616                          int type = current.getTypeId();                          int type = current.getTypeId();
617                            
618                          Integer amount = output.get(type);                          Integer amount = output.get(type);
619                          if (amount == null)                          if (amount == null)
620                                  amount = 0;                                  amount = 0;
621                            
622                          output.put(type, amount + current.getAmount() );                                                  output.put(type, amount + current.getAmount() );                        
623                  }                  }
624                  return output;                  return output;
# Line 552  public class AdvancedChest  implements L Line 635  public class AdvancedChest  implements L
635                  }                  }
636                  return output;                  return output;
637          }*/          }*/
638            
639            
640  }  }

Legend:
Removed from v.1811  
changed lines
  Added in v.2429

  ViewVC Help
Powered by ViewVC 1.1.20