/[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 1809 by torben, Sun Jun 3 20:34:42 2012 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 22  import org.bukkit.event.block.Action; Line 26  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.BlockPlaceEvent;  import org.bukkit.event.block.BlockPlaceEvent;
28  import org.bukkit.event.entity.EntityExplodeEvent;  import org.bukkit.event.entity.EntityExplodeEvent;
29    import org.bukkit.event.inventory.InventoryCloseEvent;
30    import org.bukkit.event.inventory.InventoryOpenEvent;
31  import org.bukkit.event.player.PlayerInteractEvent;  import org.bukkit.event.player.PlayerInteractEvent;
32  import org.bukkit.inventory.InventoryHolder;  import org.bukkit.inventory.InventoryHolder;
33    import org.bukkit.inventory.ItemStack;
34    
35  import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin;  import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin;
36    import dk.thoerup.bukkit.hoeruputils.Util;
37    
38    
39  public class LockedChest  implements Listener, CommandExecutor{  
40    public class AdvancedChest  implements Listener, CommandExecutor{
41    
42            class ItemCount extends TreeMap<Integer,Integer> {
43                    private static final long serialVersionUID = 1L;
44            };
45            
46            HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>();
47                    
48                                    
49          HashMap<Location,LockedChestBean> chestMap = new HashMap<Location, LockedChestBean>();          HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>();
50    
51                    
52          HoerupUtilsPlugin plugin;          HoerupUtilsPlugin plugin;
53          Server server;          Server server;
54                    
55          public LockedChest(HoerupUtilsPlugin plugin, Runnable r) {          public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) {
56                  this.plugin = plugin;                  this.plugin = plugin;
57                  server = plugin.getServer();                  server = plugin.getServer();
58                  try {                  try {
# Line 59  public class LockedChest  implements Lis Line 74  public class LockedChest  implements Lis
74                                    
75                  Player player = (Player) sender;                  Player player = (Player) sender;
76                                    
77                    if (args.length == 0) {
78                            player.sendMessage("Usage:");
79                            player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer) [player]");
80                            return true;
81                    }
82                                    
83                  Block b = player.getTargetBlock(null, 30);                                  
84                  Location loc = b.getLocation();                  Block b = player.getTargetBlock(null, 30);                      
85                                    
86                  if (b.getTypeId() != 54) {                  if (b.getTypeId() != 54) {
87                          player.sendMessage("[LockedChest] Please look at the chest you want to lock");                          player.sendMessage("[Chest] Please look at the chest you want to protect");
88                          return true;                          return true;
89                  }                  }
90                                    
91                    Location loc = b.getLocation();
92                  Location loc2 = getNeighborChest(loc);                  Location loc2 = getNeighborChest(loc);
93                                    
94                  LockedChestBean chest = chestMap.get(loc);                  ChestBean chest = chestMap.get(loc);
95                    String cmd = args[0].toLowerCase();
96                    
97                    if (cmd.equals("status")) {
98                            if (chest != null) {                            
99                                    String mode = "";
100                                    switch (chest.getChestType()) {
101                                    case ChestBean.LOCKED:
102                                            mode = "locked";
103                                            break;
104                                    case ChestBean.SNITCHING:
105                                            mode = "snitching";
106                                            break;
107                                    default:
108                                            mode = "unknown ??";
109                                    }                              
110                                    
111                                    player.sendMessage(ChatColor.GREEN + "Chest is  a " + mode + " chest owned by " + chest.getOwner());
112                                    player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() );
113                            } else {
114                                    player.sendMessage(ChatColor.GREEN + "The chest is not protected");
115                            }
116                            return true;
117                    }
118                    
119                    if (cmd.equals("lock") || cmd.equals("snitch")) {
120                            if (chest == null) {
121                                    chest = createChest(player.getName(), "", loc);        
122                                    if (loc2 != null) {
123                                            chest.setDoublechest(true);
124                                    }
125                                    String modeStr = "";
126                                    if (cmd.equals("lock")) {
127                                            chest.setChestType( ChestBean.LOCKED);
128                                            modeStr = "locked";
129                                    } else {
130                                            chest.setChestType( ChestBean.SNITCHING);
131                                            modeStr = "snitching";
132                                    }
133                                    chest.setModifyPlayers("");
134                                    addChest(loc, chest);
135                                    player.sendMessage("Chest is now " + modeStr);
136                            } else {
137                                    player.sendMessage("This chest is already protected");
138                            }
139                            return true;
140                    }
141                    
142                    if (cmd.equals("remove")) {
143                            if (chest != null) {
144                                    player.sendMessage("[LockedChest] Removing protection from chest");
145                                    removeChest(loc);                              
146                            } else {
147                                    player.sendMessage("This chest is not protected");      
148                            }
149                            return true;
150                    }
151                    
152                    if (cmd.equals("addplayer") || cmd.equals("removeplayer")) {
153                            if (chest == null) {
154                                    player.sendMessage("This chest is not protected");
155                                    return true;
156                            }
157                            if (args.length != 2) {
158                                    player.sendMessage("You need to specify which player to add or remove");
159                                    return true;
160                            }
161                            OfflinePlayer p2 = server.getOfflinePlayer(args[1]);
162                            if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) {
163                                    player.sendMessage("Unknown user: " + args[1] );
164                                    return true;
165                            }
166                            
167                            Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
168                            if (cmd.equals("addplayer")) {
169                                    players.add(p2.getName());
170                            } else {
171                                    players.remove(p2.getName());
172                            }
173                            
174                            chest.setModifyPlayers( Util.setToString(players) );
175                            plugin.getDatabase().save( chest );
176                            player.sendMessage("ok");
177                            return true;
178                    }
179                    
180                    /*
181                  if (chest != null) {                  if (chest != null) {
182                          if (chest.getOwner().equals(player.getName())) {                          if (chest.getOwner().equals(player.getName())) {
183                                  player.sendMessage("[LockedChest] Removing lock from chest");                                  player.sendMessage("[LockedChest] Removing lock from chest");
184                                  removeChest(loc);                                  removeChest(loc);
185                          } else {                          } else {
186                                  player.sendMessage("[LockedChest] Chest is already locked");                                  player.sendMessage("[LockedChest] Chest is already protected");
187                          }                          }
188                                                    
189                          return true;                          return true;
# Line 91  public class LockedChest  implements Lis Line 198  public class LockedChest  implements Lis
198                                    
199                    
200                  player.sendMessage("[LockedChest] Chest is now locked");                  player.sendMessage("[LockedChest] Chest is now locked");
201                    */
202                                    
203                    player.sendMessage("Unknown argument, " + cmd);
204                                    
205                  return true;                  return true;
206          }          }
# Line 99  public class LockedChest  implements Lis Line 208  public class LockedChest  implements Lis
208          @EventHandler          @EventHandler
209          public void onBlockBreak(BlockBreakEvent event) {          public void onBlockBreak(BlockBreakEvent event) {
210                  Location loc = event.getBlock().getLocation();                  Location loc = event.getBlock().getLocation();
211                  LockedChestBean chest = chestMap.get(loc);                  ChestBean chest = chestMap.get(loc);
212                  if (chest != null) {                  if (chest != null) {
213                          if (chest.getOwner().equals(event.getPlayer().getName())) {                          if (chest.getOwner().equals(event.getPlayer().getName())) {
214                                  removeChest(loc);                                  removeChest(loc);
215                                  event.getPlayer().sendMessage("[LockedChest] The destroyed chest was locked");                                  event.getPlayer().sendMessage("[AdvancedChest] The destroyed chest was locked or snitching");
216                          } else {                          } else {
217                                  event.setCancelled(true);                                  event.setCancelled(true);
218                                  event.getPlayer().sendMessage("You can't destroy that chest");                                  event.getPlayer().sendMessage("You can't destroy that chest");
219                          }                          }
220                  }                  }
221          }          }
222          public void addChest(Location loc, LockedChestBean chest) {          public void addChest(Location loc, ChestBean chest) {
223                  chestMap.put(loc, chest);                  chestMap.put(loc, chest);
224                  if (chest.isDoublechest()) {                  if (chest.isDoublechest()) {
225                          Location loc2 = getNeighborChest(loc);                          Location loc2 = getNeighborChest(loc);
# Line 123  public class LockedChest  implements Lis Line 232  public class LockedChest  implements Lis
232          }          }
233                    
234          void removeChest(Location loc) {          void removeChest(Location loc) {
235                  LockedChestBean chest = chestMap.remove(loc);                  ChestBean chest = chestMap.remove(loc);
236                  if (chest != null) {                                                                                      if (chest != null) {                                                                    
237                          if (chest.isDoublechest()){                          if (chest.isDoublechest()){
238                                  Location loc2 = getNeighborChest(loc);                                  Location loc2 = getNeighborChest(loc);
# Line 134  public class LockedChest  implements Lis Line 243  public class LockedChest  implements Lis
243          }          }
244                    
245          int loadChestsWorker() {          int loadChestsWorker() {
246                  List<LockedChestBean> chestlist = plugin.getDatabase().find( LockedChestBean.class).findList();                  List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList();
247                  for (LockedChestBean chest : chestlist) {                  for (ChestBean chest : chestlist) {
248                          Location loc = getChestLocation(server, chest);                          Location loc = getChestLocation(server, chest);
249                          chestMap.put(loc, chest);                                chestMap.put(loc, chest);      
250                                                    
# Line 155  public class LockedChest  implements Lis Line 264  public class LockedChest  implements Lis
264                    
265          void loadChests() {                      void loadChests() {            
266                  int count = loadChestsWorker();                  int count = loadChestsWorker();
267                  plugin.getLogger().info("[LockedChest] loaded " + count + " chests");                  plugin.getLogger().info("[AdvancedChest] loaded " + count + " chests");
268          }          }
269                    
270                    
271          public LockedChestBean createChest(String owner, String description, Location loc) {          public ChestBean createChest(String owner, String description, Location loc) {
272                                    
273                  LockedChestBean chest = new LockedChestBean();                  ChestBean chest = new ChestBean();
274                  chest.setOwner(owner);                  chest.setOwner(owner);
275                  chest.setDescription(description);                  chest.setDescription(description);
276                  setChestLocation(chest, loc);                  setChestLocation(chest, loc);
# Line 170  public class LockedChest  implements Lis Line 279  public class LockedChest  implements Lis
279          }          }
280                    
281                    
282          public void setChestLocation(LockedChestBean chest, Location loc) {          public void setChestLocation(ChestBean chest, Location loc) {
283                  chest.setWorld( loc.getWorld().getName() );                  chest.setWorld( loc.getWorld().getName() );
284                  chest.setX( loc.getBlockX() );                  chest.setX( loc.getBlockX() );
285                  chest.setY( loc.getBlockY() );                  chest.setY( loc.getBlockY() );
286                  chest.setZ( loc.getBlockZ() );                            chest.setZ( loc.getBlockZ() );          
287          }          }
288                    
289          public Location getChestLocation(Server server, LockedChestBean chest) {          public Location getChestLocation(Server server, ChestBean chest) {
290                  World wrld = server.getWorld(chest.getWorld());                  World wrld = server.getWorld(chest.getWorld());
291                  return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());                                return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());              
292          }          }
# Line 235  public class LockedChest  implements Lis Line 344  public class LockedChest  implements Lis
344                                    
345                  Location chestloc = getNeighborChest( block.getLocation() );                  Location chestloc = getNeighborChest( block.getLocation() );
346                  if (chestloc != null) {                  if (chestloc != null) {
347                          LockedChestBean chest = chestMap.get(chestloc);                          ChestBean chest = chestMap.get(chestloc);
348                                                    
349                          if (chest != null) { //the neighbor is a locked chest                          if (chest != null) { //the neighbor is a locked chest
350    
# Line 244  public class LockedChest  implements Lis Line 353  public class LockedChest  implements Lis
353                                  addChest(chestloc, chest);                                  addChest(chestloc, chest);
354                                                                    
355                                                                    
356                                  event.getPlayer().sendMessage( "[LockedChest] Chest has been expanded" );                                  event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" );
357                          }                                                }                      
358                                                    
359                  }                  }
# Line 254  public class LockedChest  implements Lis Line 363  public class LockedChest  implements Lis
363          @EventHandler          @EventHandler
364          public void onChestExplode(EntityExplodeEvent event) {          public void onChestExplode(EntityExplodeEvent event) {
365                  for (Block b : event.blockList() ) {                  for (Block b : event.blockList() ) {
366                          LockedChestBean chest = chestMap.get( b.getLocation() );                          ChestBean chest = chestMap.get( b.getLocation() );
367                          if (chest != null) {                          if (chest != null) {
368                                  event.setCancelled( true );                                  event.setCancelled( true );
369                                  return;                                  return;
# Line 272  public class LockedChest  implements Lis Line 381  public class LockedChest  implements Lis
381                                                                    
382                                  Location loc = b.getLocation();                                  Location loc = b.getLocation();
383                                                                    
384                                  LockedChestBean chest = chestMap.get( loc );                                  ChestBean chest = chestMap.get( loc );
385                                  if (chest == null) {                                                              if (chest == null) {                            
386                                          return; //chest not surveyed by this plugin                                          return; //chest not surveyed by this plugin
387                                  }                                  }
388                                                                    
389                                    if (chest.getChestType() != ChestBean.LOCKED ) {
390                                            return; //this is not a locked chests
391                                    }
392                                    
393                                  Player player = (Player) event.getPlayer();                                  Player player = (Player) event.getPlayer();
394                                  if (player.getName().equals(chest.getOwner() )) {                                  if (player.getName().equals(chest.getOwner() )) {
395                                          return; //chest is opened by it's owner                                          return; //chest is opened by it's owner
396                                  }                                  }
397                                                                    
398                                  if (chest.getModifyPlayers() != null && chest.getModifyPlayers().length()>0) {                                  Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
399                                          String modplayers[] = chest.getModifyPlayers().split(",");                                  if ( players.contains(player.getName()) ) {
400                                          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  
                                                 }  
                                         }  
401                                  }                                  }
402    
403    
404                                                    
405                                  player.sendMessage( ChatColor.BLUE +  "Sorry but this chest is locked !");                                  player.sendMessage( ChatColor.BLUE +  "Sorry but this chest is locked !");
406                                  event.setCancelled(true);                                  event.setCancelled(true);
407                          }                          }
408                  }                                }              
409          }          }
410    
411            @EventHandler
412            public void onChestOpen(InventoryOpenEvent event) {
413                    
414                    if (! (event.getPlayer() instanceof Player)) {
415                            return;
416                    }
417    
418    
419                    
420                    InventoryHolder holder = event.getInventory().getHolder();
421                    if (holder instanceof Chest || holder instanceof DoubleChest) {
422                            Location loc = getChestLocation(holder);
423                            
424                            ChestBean chest = chestMap.get( loc );
425                            if (chest == null) {                            
426                                    return; //chest not surveyed by this plugin
427                            }
428                            
429                            if (chest.getChestType() != ChestBean.SNITCHING) {
430                                    return; // not a snitching chest
431                            }
432                            
433                                                    
434                            Player player = (Player) event.getPlayer();
435                            if (player.getName().equals(chest.getOwner() )) {
436                                    return; //chest is owned by it's own player
437                            }
438                            
439                            Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
440                            if ( players.contains(player.getName()) ) {
441                                    return; //this player is on the whitelist so he may open
442                            }
443                            
444                            
445                            ItemCount contents = countItems( event.getInventory().getContents() );                  
446                            
447                            contentMap.put(player.getName(), contents );
448                    }
449            }
450            
451            @EventHandler
452            public void onChestClose(InventoryCloseEvent event) {
453                    if (! (event.getPlayer() instanceof Player)) {
454                            return;
455                    }
456                    
457                    
458                    InventoryHolder holder = event.getInventory().getHolder();
459                    if (holder instanceof Chest || holder instanceof DoubleChest) {
460                            Location loc = getChestLocation(holder);
461                            ChestBean chest = chestMap.get(loc);
462                            
463                            if (chest == null) { //chest was not a snitching chest
464                                    return;
465                            }
466                            
467                            if (chest.getChestType() != ChestBean.SNITCHING) {
468                                    return; // not a snitching chest
469                            }
470                            
471                            
472                            OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
473                            
474                            
475                            Player player = (Player) event.getPlayer();
476                            
477                            ItemCount savedContent = contentMap.get( player.getName() );
478                            
479                            if (savedContent == null) {
480                                    return;
481                            }
482                            
483                            contentMap.remove( player.getName() );
484                            
485                            ItemCount content = countItems( event.getInventory().getContents() );
486                                                    
487                            Set<Integer> combinedKeyset = new TreeSet<Integer>();                  
488                            combinedKeyset.addAll( savedContent.keySet() );
489                            combinedKeyset.addAll( content.keySet() );
490                            
491                            for (Integer item : combinedKeyset ) {
492                                    Integer savedcount = savedContent.get(item);                            
493                                    Integer count = content.get(item);
494                                    
495                                    if (savedcount == null)
496                                            savedcount = 0;
497                                    if (count == null)
498                                            count = 0;
499                                    
500                                    
501                                    int diff = Math.abs( savedcount - count);
502                                    
503                                    if (diff > 0) {
504                                            String material = Material.getMaterial(item).name();
505                                            String msg = null;
506                                            
507                                            if (count > savedcount) {
508                                                    msg = player.getName() + " added "   + diff + " units of " + material + "(" +item + ") to "   + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
509                                            } else { //(count < savedcount)
510                                                    msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
511                                            }
512                                    
513                                    
514                                            plugin.getLogger().info(msg);
515                                            plugin.getMessageWrapper().sendMessage("system", owner, msg);
516                                    }
517                                    
518                            }
519                            
520                            
521                    }
522            }
523            
524            ItemCount countItems(ItemStack[] input) {
525                    ItemCount output = new ItemCount();
526                    for (int i=0; i<input.length; i++) {
527                            ItemStack current = input[i];
528                            if (current == null)
529                                    continue;
530                            
531                            int type = current.getTypeId();
532                            
533                            Integer amount = output.get(type);
534                            if (amount == null)
535                                    amount = 0;
536                            
537                            output.put(type, amount + current.getAmount() );                        
538                    }
539                    return output;
540            }
541            /*
542            ItemStack[] cloneItemStacks(ItemStack[] input) {
543                    ItemStack[] output = new ItemStack[ input.length ];
544                    for (int i=0; i<input.length; i++) {
545                            if (input[i] != null) {
546                                    output[i] = input[i].clone();
547                            } else {
548                                    output[i] = new ItemStack(0, 0);
549                            }
550                    }
551                    return output;
552            }*/
553            
554                    
555  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20