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

miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/LockedChest.java revision 1806 by torben, Mon May 28 20:15:38 2012 UTC miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/AdvancedChest.java revision 2426 by torben, Sat Mar 7 20:54:11 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;
8    import java.util.TreeMap;
9    import java.util.TreeSet;
10    
11  import org.bukkit.ChatColor;  import org.bukkit.ChatColor;
12  import org.bukkit.Location;  import org.bukkit.Location;
13  import org.bukkit.Material;  import org.bukkit.Material;
14    import org.bukkit.OfflinePlayer;
15  import org.bukkit.Server;  import org.bukkit.Server;
16  import org.bukkit.World;  import org.bukkit.World;
17  import org.bukkit.block.Block;  import org.bukkit.block.Block;
# Line 20  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;
32    import org.bukkit.event.inventory.InventoryOpenEvent;
33  import org.bukkit.event.player.PlayerInteractEvent;  import org.bukkit.event.player.PlayerInteractEvent;
34  import org.bukkit.inventory.InventoryHolder;  import org.bukkit.inventory.InventoryHolder;
35    import org.bukkit.inventory.ItemStack;
36    
37  import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin;  import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin;
38    import dk.thoerup.bukkit.hoeruputils.Util;
39    
40    
 public class LockedChest  implements Listener, CommandExecutor{  
           
                   
         HashMap<Location,LockedChestBean> chestMap = new HashMap<Location, LockedChestBean>();  
41    
42            public class AdvancedChest  implements Listener, CommandExecutor{
43    
44            class ItemCount extends TreeMap<Integer,Integer> {
45                    private static final long serialVersionUID = 1L;
46            };
47    
48            HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>();
49    
50    
51            HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>();
52    
53    
54          HoerupUtilsPlugin plugin;          HoerupUtilsPlugin plugin;
55          Server server;          Server server;
56            
57          public LockedChest(HoerupUtilsPlugin plugin, Runnable r) {          public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) {
58                  this.plugin = plugin;                  this.plugin = plugin;
59                  server = plugin.getServer();                  server = plugin.getServer();
60                  try {                  try {
# Line 48  public class LockedChest  implements Lis Line 65  public class LockedChest  implements Lis
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 56  public class LockedChest  implements Lis Line 73  public class LockedChest  implements Lis
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) {
80                  Block b = player.getTargetBlock(null, 30);                          player.sendMessage("Usage:");
81                            player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer|setowner|comment) [player]");
82                            return true;
83                    }
84    
85    
86                    Block b = player.getTargetBlock( (HashSet<Byte>)null, 30);                      
87    
88                    if (b.getTypeId() != 54 && b.getTypeId() != 154) {
89                            player.sendMessage("[Chest] Please look at the chest/hopper you want to protect");
90                            return true;
91                    }
92    
93                  Location loc = b.getLocation();                  Location loc = b.getLocation();
94                                    Location loc2 = null;
95                  if (b.getTypeId() != 54) {                  if ( b.getTypeId() == 54) { //dont find neighbours for Hoppers
96                          player.sendMessage("[LockedChest] Please look at the chest you want to lock");                          loc2 = getNeighborChest(loc);
97                    }
98    
99                    ChestBean chest = chestMap.get(loc);
100                    String cmd = args[0].toLowerCase();
101    
102                    if (cmd.equals("status")) {
103                            if (chest != null) {                            
104                                    String mode = "";
105                                    switch (chest.getChestType()) {
106                                    case ChestBean.LOCKED:
107                                            mode = "locked";
108                                            break;
109                                    case ChestBean.SNITCHING:
110                                            mode = "snitching";
111                                            break;
112                                    default:
113                                            mode = "unknown ??";
114                                    }                              
115    
116                                    player.sendMessage(ChatColor.GREEN + "Chest is  a " + mode + " chest owned by " + chest.getOwner());
117                                    player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() );
118                                    player.sendMessage(ChatColor.GREEN + "Comment: " + chest.getComment() );
119                            } else {
120                                    player.sendMessage(ChatColor.GREEN + "The chest is not protected");
121                            }
122                          return true;                          return true;
123                  }                  }
124                    
125                  Location loc2 = getNeighborChest(loc);                  if (cmd.equals("lock") || cmd.equals("snitch")) {
126                                            if (chest == null) {
127                  LockedChestBean chest = chestMap.get(loc);                                  chest = createChest(player.getName(), "", loc);        
128                                    if (loc2 != null) {
129                                            chest.setDoublechest(true);
130                                    }
131                                    String modeStr = "";
132                                    if (cmd.equals("lock")) {
133                                            chest.setChestType( ChestBean.LOCKED);
134                                            modeStr = "locked";
135                                    } else {
136                                            chest.setChestType( ChestBean.SNITCHING);
137                                            modeStr = "snitching";
138                                    }
139                                    chest.setModifyPlayers("");
140                                    chest.setComment("");
141                                    addChest(loc, chest);
142                                    player.sendMessage("Chest is now " + modeStr);
143                            } else {
144                                    server.getLogger().info( player.getName() + " tried to protect a chest owned by " + chest.getOwner() );
145                                    player.sendMessage("This chest is already protected");
146                            }
147                            return true;
148                    }
149    
150                    if (cmd.equals("remove")) {
151                            if (chest == null) {
152                                    player.sendMessage("This chest is not protected");
153                                    return true;
154                            }
155                            if ( chest.getOwner().equals( player.getName() ) || player.isOp() ) {
156                                    //do nothing
157                            } else {
158                                    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;
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")) {
197                            if (chest == null) {
198                                    player.sendMessage("This chest is not protected");
199                                    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) {
207                                    player.sendMessage("You need to specify which player to add or remove");
208                                    return true;
209                            }
210                            OfflinePlayer p2 = server.getOfflinePlayer(args[1]);
211                            if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) {
212                                    player.sendMessage("Unknown user: " + args[1] );
213                                    return true;
214                            }
215    
216                            Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
217                            if (cmd.equals("addplayer")) {
218                                    players.add(p2.getName());
219                            } else {
220                                    players.remove(p2.getName());
221                            }
222    
223                            chest.setModifyPlayers( Util.setToString(players) );
224                            plugin.getDatabase().save( chest );
225                            player.sendMessage("ok");
226                            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())) {
255                                  player.sendMessage("[LockedChest] Removing lock from chest");                                  player.sendMessage("[LockedChest] Removing lock from chest");
256                                  removeChest(loc);                                  removeChest(loc);
257                          } else {                          } else {
258                                  player.sendMessage("[LockedChest] Chest is already locked");                                  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);
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();
283                  LockedChestBean chest = chestMap.get(loc);                  ChestBean chest = chestMap.get(loc);
284                  if (chest != null) {                  if (chest != null) {
285                          if (chest.getOwner().equals(event.getPlayer().getName())) {                          if (chest.getOwner().equals(event.getPlayer().getName())) {
286                                  removeChest(loc);                                  removeChest(loc);
287                                  event.getPlayer().sendMessage("[LockedChest] The destroyed chest was locked");                                  event.getPlayer().sendMessage("[AdvancedChest] The destroyed chest was locked or snitching");
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() + chest.getCommentString() );
292                          }                          }
293                  }                  }
294          }          }
295          public void addChest(Location loc, LockedChestBean chest) {          public void addChest(Location loc, ChestBean chest) {
296                  chestMap.put(loc, chest);                  chestMap.put(loc, chest);
297                  if (chest.isDoublechest()) {                  if (chest.isDoublechest()) {
298                          Location loc2 = getNeighborChest(loc);                          Location loc2 = getNeighborChest(loc);
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                  LockedChestBean chest = chestMap.remove(loc);                  ChestBean chest = chestMap.remove(loc);
309                  if (chest != null) {                                                                                      if (chest != null) {                                                                    
310                          if (chest.isDoublechest()){                          if (chest.isDoublechest()){
311                                  Location loc2 = getNeighborChest(loc);                                  Location loc2 = getNeighborChest(loc);
# Line 132  public class LockedChest  implements Lis Line 314  public class LockedChest  implements Lis
314                          plugin.getDatabase().delete(chest);                          plugin.getDatabase().delete(chest);
315                  }                  }
316          }          }
317            
318          int loadChestsWorker() {          int loadChestsWorker() {
319                  List<LockedChestBean> chestlist = plugin.getDatabase().find( LockedChestBean.class).findList();                  List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList();
320                  for (LockedChestBean 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 147  public class LockedChest  implements Lis Line 329  public class LockedChest  implements Lis
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("[LockedChest] loaded " + count + " chests");                  server.getLogger().info("[AdvancedChest] loaded " + count + " chests");
341          }          }
342            
343            
344          public LockedChestBean createChest(String owner, String description, Location loc) {          public ChestBean createChest(String owner, String description, Location loc) {
345                    
346                  LockedChestBean chest = new LockedChestBean();                  ChestBean chest = new ChestBean();
347                  chest.setOwner(owner);                  chest.setOwner(owner);
348                  chest.setDescription(description);                  chest.setDescription(description);
349                  setChestLocation(chest, loc);                  setChestLocation(chest, loc);
350    
351                  return chest;                  return chest;
352          }          }
353            
354            
355          public void setChestLocation(LockedChestBean 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, LockedChestBean 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 206  public class LockedChest  implements Lis Line 388  public class LockedChest  implements Lis
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 218  public class LockedChest  implements Lis Line 400  public class LockedChest  implements Lis
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                          LockedChestBean 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( "[LockedChest] Chest has been expanded" );                  addChest(chestloc, chest);
430                          }                        
431                            
432                  }                  event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" );
433          }          }
434    
435    
436          @EventHandler          @EventHandler
437          public void onChestExplode(EntityExplodeEvent event) {          public void onChestExplode(EntityExplodeEvent event) {
438                  for (Block b : event.blockList() ) {                  for (Block b : event.blockList() ) {
439                          LockedChestBean 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() +  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                                  LockedChestBean 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 ) {
473                                            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                                  if (chest.getModifyPlayers() != null && chest.getModifyPlayers().length()>0) {                                  Set<String> players = chest.getModifyPlayersSet() ;
482                                          String modplayers[] = chest.getModifyPlayers().split(",");                                  if ( players.contains(player.getName()) ) {
483                                          for (String p : modplayers) {                                          return; //this player is on the whitelist so he may open
                                                 if ( player.getName().equals(p) ) {  
                                                         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()  + 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                          }                          }
491                  }                                }              
492          }          }
493            
494            @EventHandler
495            public void onChestOpen(InventoryOpenEvent event) {
496    
497                    if (! (event.getPlayer() instanceof Player)) {
498                            return;
499                    }
500    
501    
502    
503                    InventoryHolder holder = event.getInventory().getHolder();
504                    if (holder instanceof Chest || holder instanceof DoubleChest) {
505                            Location loc = getChestLocation(holder);
506    
507                            ChestBean chest = chestMap.get( loc );
508                            if (chest == null) {                            
509                                    return; //chest not surveyed by this plugin
510                            }
511    
512                            if (chest.getChestType() != ChestBean.SNITCHING) {
513                                    return; // not a snitching chest
514                            }
515    
516    
517                            Player player = (Player) event.getPlayer();
518                            if (player.getName().equals(chest.getOwner() )) {
519                                    return; //chest is owned by it's own player
520                            }
521    
522                            Set<String> players = chest.getModifyPlayersSet();
523                            if ( players.contains(player.getName()) ) {
524                                    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() );                  
531    
532                            contentMap.put(player.getName(), contents );
533                    }
534            }
535    
536            @EventHandler
537            public void onChestClose(InventoryCloseEvent event) {
538                    if (! (event.getPlayer() instanceof Player)) {
539                            return;
540                    }
541    
542    
543                    InventoryHolder holder = event.getInventory().getHolder();
544                    if (holder instanceof Chest || holder instanceof DoubleChest) {
545                            Location loc = getChestLocation(holder);
546                            ChestBean chest = chestMap.get(loc);
547    
548                            if (chest == null) { //chest was not a snitching chest
549                                    return;
550                            }
551    
552                            if (chest.getChestType() != ChestBean.SNITCHING) {
553                                    return; // not a snitching chest
554                            }
555    
556    
557                            OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
558    
559    
560                            Player player = (Player) event.getPlayer();
561    
562                            ItemCount savedContent = contentMap.get( player.getName() );
563    
564                            if (savedContent == null) {
565                                    return;
566                            }
567    
568                            contentMap.remove( player.getName() );
569    
570                            ItemCount content = countItems( event.getInventory().getContents() );
571    
572                            Set<Integer> combinedKeyset = new TreeSet<Integer>();                  
573                            combinedKeyset.addAll( savedContent.keySet() );
574                            combinedKeyset.addAll( content.keySet() );
575    
576                            for (Integer item : combinedKeyset ) {
577                                    Integer savedcount = savedContent.get(item);                            
578                                    Integer count = content.get(item);
579    
580                                    if (savedcount == null)
581                                            savedcount = 0;
582                                    if (count == null)
583                                            count = 0;
584    
585    
586                                    int diff = Math.abs( savedcount - count);
587    
588                                    if (diff > 0) {
589                                            String material = Material.getMaterial(item).name();
590                                            String msg = null;
591    
592                                            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() + chest.getCommentString();
594                                            } 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() + chest.getCommentString();
596                                            }
597    
598    
599                                            server.getLogger().info( "[AdvancedChest]" + msg);
600                                            plugin.getMessageWrapper().sendMessage("system", owner, msg);
601                                    }
602    
603                            }
604    
605    
606                    }
607            }
608    
609            ItemCount countItems(ItemStack[] input) {
610                    ItemCount output = new ItemCount();
611                    for (int i=0; i<input.length; i++) {
612                            ItemStack current = input[i];
613                            if (current == null)
614                                    continue;
615    
616                            int type = current.getTypeId();
617    
618                            Integer amount = output.get(type);
619                            if (amount == null)
620                                    amount = 0;
621    
622                            output.put(type, amount + current.getAmount() );                        
623                    }
624                    return output;
625            }
626            /*
627            ItemStack[] cloneItemStacks(ItemStack[] input) {
628                    ItemStack[] output = new ItemStack[ input.length ];
629                    for (int i=0; i<input.length; i++) {
630                            if (input[i] != null) {
631                                    output[i] = input[i].clone();
632                            } else {
633                                    output[i] = new ItemStack(0, 0);
634                            }
635                    }
636                    return output;
637            }*/
638    
639    
640  }  }

Legend:
Removed from v.1806  
changed lines
  Added in v.2426

  ViewVC Help
Powered by ViewVC 1.1.20