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

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

  ViewVC Help
Powered by ViewVC 1.1.20