/[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 1854 by torben, Tue Oct 2 18:39:29 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|setowner) [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("This chest is not protected");
145                                    return true;
146                            }
147                            if (! chest.getOwner().equals( player.getName() ) ) {
148                                    player.sendMessage("You can not remove lock from a chest you don't own");
149                                    return true;
150                            }
151    
152                            player.sendMessage("[LockedChest] Removing protection from chest");
153                            removeChest(loc);                              
154    
155                            return true;
156                    }
157    
158                    if (cmd.equals("setowner")) {
159                            if (chest == null) {
160                                    player.sendMessage("This chest is not protected");
161                                    return true;
162                            }
163                            if (! chest.getOwner().equals( player.getName() ) ) {
164                                    player.sendMessage("You can not set new owner of a chest you dont own");
165                                    return true;
166                            }
167                            if (args.length != 2) {
168                                    player.sendMessage("You need to specify which player should own this chest");
169                                    return true;
170                            }
171                            OfflinePlayer p2 = server.getOfflinePlayer(args[1]);
172                            if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) {
173                                    player.sendMessage("Unknown user: " + args[1] );
174                                    return true;
175                            }
176                            chest.setOwner( p2.getName() );
177                            plugin.getDatabase().save( chest );
178                            player.sendMessage("ok");
179                            return true;
180                    }
181                    
182                    if (cmd.equals("addplayer") || cmd.equals("removeplayer")) {
183                            if (chest == null) {
184                                    player.sendMessage("This chest is not protected");
185                                    return true;
186                            }
187                            if (! chest.getOwner().equals( player.getName() ) ) {
188                                    player.sendMessage("You can not add/remove players from a chest you don't own");
189                                    return true;
190                            }
191                            if (args.length != 2) {
192                                    player.sendMessage("You need to specify which player to add or remove");
193                                    return true;
194                            }
195                            OfflinePlayer p2 = server.getOfflinePlayer(args[1]);
196                            if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) {
197                                    player.sendMessage("Unknown user: " + args[1] );
198                                    return true;
199                            }
200                            
201                            Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
202                            if (cmd.equals("addplayer")) {
203                                    players.add(p2.getName());
204                            } else {
205                                    players.remove(p2.getName());
206                            }
207                            
208                            chest.setModifyPlayers( Util.setToString(players) );
209                            plugin.getDatabase().save( chest );
210                            player.sendMessage("ok");
211                            return true;
212                    }
213                    
214                    /*
215                  if (chest != null) {                  if (chest != null) {
216                          if (chest.getOwner().equals(player.getName())) {                          if (chest.getOwner().equals(player.getName())) {
217                                  player.sendMessage("[LockedChest] Removing lock from chest");                                  player.sendMessage("[LockedChest] Removing lock from chest");
218                                  removeChest(loc);                                  removeChest(loc);
219                          } else {                          } else {
220                                  player.sendMessage("[LockedChest] Chest is already locked");                                  player.sendMessage("[LockedChest] Chest is already protected");
221                          }                          }
222                                                    
223                          return true;                          return true;
# Line 91  public class LockedChest  implements Lis Line 232  public class LockedChest  implements Lis
232                                    
233                    
234                  player.sendMessage("[LockedChest] Chest is now locked");                  player.sendMessage("[LockedChest] Chest is now locked");
235                    */
236                                    
237                    player.sendMessage("Unknown argument, " + cmd);
238                                    
239                  return true;                  return true;
240          }          }
# Line 99  public class LockedChest  implements Lis Line 242  public class LockedChest  implements Lis
242          @EventHandler          @EventHandler
243          public void onBlockBreak(BlockBreakEvent event) {          public void onBlockBreak(BlockBreakEvent event) {
244                  Location loc = event.getBlock().getLocation();                  Location loc = event.getBlock().getLocation();
245                  LockedChestBean chest = chestMap.get(loc);                  ChestBean chest = chestMap.get(loc);
246                  if (chest != null) {                  if (chest != null) {
247                          if (chest.getOwner().equals(event.getPlayer().getName())) {                          if (chest.getOwner().equals(event.getPlayer().getName())) {
248                                  removeChest(loc);                                  removeChest(loc);
249                                  event.getPlayer().sendMessage("[LockedChest] The destroyed chest was locked");                                  event.getPlayer().sendMessage("[AdvancedChest] The destroyed chest was locked or snitching");
250                          } else {                          } else {
251                                  event.setCancelled(true);                                  event.setCancelled(true);
252                                  event.getPlayer().sendMessage("You can't destroy that chest");                                  event.getPlayer().sendMessage("You can't destroy that chest");
253                                    server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried breaking a chest owned by " + chest.getOwner() );
254                          }                          }
255                  }                  }
256          }          }
257          public void addChest(Location loc, LockedChestBean chest) {          public void addChest(Location loc, ChestBean chest) {
258                  chestMap.put(loc, chest);                  chestMap.put(loc, chest);
259                  if (chest.isDoublechest()) {                  if (chest.isDoublechest()) {
260                          Location loc2 = getNeighborChest(loc);                          Location loc2 = getNeighborChest(loc);
# Line 123  public class LockedChest  implements Lis Line 267  public class LockedChest  implements Lis
267          }          }
268                    
269          void removeChest(Location loc) {          void removeChest(Location loc) {
270                  LockedChestBean chest = chestMap.remove(loc);                  ChestBean chest = chestMap.remove(loc);
271                  if (chest != null) {                                                                                      if (chest != null) {                                                                    
272                          if (chest.isDoublechest()){                          if (chest.isDoublechest()){
273                                  Location loc2 = getNeighborChest(loc);                                  Location loc2 = getNeighborChest(loc);
# Line 134  public class LockedChest  implements Lis Line 278  public class LockedChest  implements Lis
278          }          }
279                    
280          int loadChestsWorker() {          int loadChestsWorker() {
281                  List<LockedChestBean> chestlist = plugin.getDatabase().find( LockedChestBean.class).findList();                  List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList();
282                  for (LockedChestBean chest : chestlist) {                  for (ChestBean chest : chestlist) {
283                          Location loc = getChestLocation(server, chest);                          Location loc = getChestLocation(server, chest);
284                          chestMap.put(loc, chest);                                chestMap.put(loc, chest);      
285                                                    
# Line 155  public class LockedChest  implements Lis Line 299  public class LockedChest  implements Lis
299                    
300          void loadChests() {                      void loadChests() {            
301                  int count = loadChestsWorker();                  int count = loadChestsWorker();
302                  plugin.getLogger().info("[LockedChest] loaded " + count + " chests");                  server.getLogger().info("[AdvancedChest] loaded " + count + " chests");
303          }          }
304                    
305                    
306          public LockedChestBean createChest(String owner, String description, Location loc) {          public ChestBean createChest(String owner, String description, Location loc) {
307                                    
308                  LockedChestBean chest = new LockedChestBean();                  ChestBean chest = new ChestBean();
309                  chest.setOwner(owner);                  chest.setOwner(owner);
310                  chest.setDescription(description);                  chest.setDescription(description);
311                  setChestLocation(chest, loc);                  setChestLocation(chest, loc);
# Line 170  public class LockedChest  implements Lis Line 314  public class LockedChest  implements Lis
314          }          }
315                    
316                    
317          public void setChestLocation(LockedChestBean chest, Location loc) {          public void setChestLocation(ChestBean chest, Location loc) {
318                  chest.setWorld( loc.getWorld().getName() );                  chest.setWorld( loc.getWorld().getName() );
319                  chest.setX( loc.getBlockX() );                  chest.setX( loc.getBlockX() );
320                  chest.setY( loc.getBlockY() );                  chest.setY( loc.getBlockY() );
321                  chest.setZ( loc.getBlockZ() );                            chest.setZ( loc.getBlockZ() );          
322          }          }
323                    
324          public Location getChestLocation(Server server, LockedChestBean chest) {          public Location getChestLocation(Server server, ChestBean chest) {
325                  World wrld = server.getWorld(chest.getWorld());                  World wrld = server.getWorld(chest.getWorld());
326                  return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());                                return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());              
327          }          }
# Line 235  public class LockedChest  implements Lis Line 379  public class LockedChest  implements Lis
379                                    
380                  Location chestloc = getNeighborChest( block.getLocation() );                  Location chestloc = getNeighborChest( block.getLocation() );
381                  if (chestloc != null) {                  if (chestloc != null) {
382                          LockedChestBean chest = chestMap.get(chestloc);                          ChestBean chest = chestMap.get(chestloc);
383                                                    
384                          if (chest != null) { //the neighbor is a locked chest                          if (chest != null) { //the neighbor is a locked chest
385    
# Line 244  public class LockedChest  implements Lis Line 388  public class LockedChest  implements Lis
388                                  addChest(chestloc, chest);                                  addChest(chestloc, chest);
389                                                                    
390                                                                    
391                                  event.getPlayer().sendMessage( "[LockedChest] Chest has been expanded" );                                  event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" );
392                          }                                                }                      
393                                                    
394                  }                  }
# Line 254  public class LockedChest  implements Lis Line 398  public class LockedChest  implements Lis
398          @EventHandler          @EventHandler
399          public void onChestExplode(EntityExplodeEvent event) {          public void onChestExplode(EntityExplodeEvent event) {
400                  for (Block b : event.blockList() ) {                  for (Block b : event.blockList() ) {
401                          LockedChestBean chest = chestMap.get( b.getLocation() );                          ChestBean chest = chestMap.get( b.getLocation() );
402                          if (chest != null) {                          if (chest != null) {
403                                    server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() );
404                                  event.setCancelled( true );                                  event.setCancelled( true );
405                                  return;                                  return;
406                          }                          }
# Line 272  public class LockedChest  implements Lis Line 417  public class LockedChest  implements Lis
417                                                                    
418                                  Location loc = b.getLocation();                                  Location loc = b.getLocation();
419                                                                    
420                                  LockedChestBean chest = chestMap.get( loc );                                  ChestBean chest = chestMap.get( loc );
421                                  if (chest == null) {                                                              if (chest == null) {                            
422                                          return; //chest not surveyed by this plugin                                          return; //chest not surveyed by this plugin
423                                  }                                  }
424                                                                    
425                                    if (chest.getChestType() != ChestBean.LOCKED ) {
426                                            return; //this is not a locked chests
427                                    }
428                                    
429                                  Player player = (Player) event.getPlayer();                                  Player player = (Player) event.getPlayer();
430                                  if (player.getName().equals(chest.getOwner() )) {                                  if (player.getName().equals(chest.getOwner() )) {
431                                          return; //chest is opened by it's owner                                          return; //chest is opened by it's owner
432                                  }                                  }
433                                                                    
434                                  if (chest.getModifyPlayers() != null && chest.getModifyPlayers().length()>0) {                                  Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
435                                          String modplayers[] = chest.getModifyPlayers().split(",");                                  if ( players.contains(player.getName()) ) {
436                                          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  
                                                 }  
                                         }  
437                                  }                                  }
438    
439                            
440                                    server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner() );
441                                  player.sendMessage( ChatColor.BLUE +  "Sorry but this chest is locked !");                                  player.sendMessage( ChatColor.BLUE +  "Sorry but this chest is locked !");
442                                  event.setCancelled(true);                                  event.setCancelled(true);
443                          }                          }
444                  }                                }              
445          }          }
446    
447            @EventHandler
448            public void onChestOpen(InventoryOpenEvent event) {
449                    
450                    if (! (event.getPlayer() instanceof Player)) {
451                            return;
452                    }
453    
454    
455                    
456                    InventoryHolder holder = event.getInventory().getHolder();
457                    if (holder instanceof Chest || holder instanceof DoubleChest) {
458                            Location loc = getChestLocation(holder);
459                            
460                            ChestBean chest = chestMap.get( loc );
461                            if (chest == null) {                            
462                                    return; //chest not surveyed by this plugin
463                            }
464                            
465                            if (chest.getChestType() != ChestBean.SNITCHING) {
466                                    return; // not a snitching chest
467                            }
468                            
469                                                    
470                            Player player = (Player) event.getPlayer();
471                            if (player.getName().equals(chest.getOwner() )) {
472                                    return; //chest is owned by it's own player
473                            }
474                            
475                            Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
476                            if ( players.contains(player.getName()) ) {
477                                    return; //this player is on the whitelist so he may open
478                            }
479                            
480                            
481                            ItemCount contents = countItems( event.getInventory().getContents() );                  
482                            
483                            contentMap.put(player.getName(), contents );
484                    }
485            }
486            
487            @EventHandler
488            public void onChestClose(InventoryCloseEvent event) {
489                    if (! (event.getPlayer() instanceof Player)) {
490                            return;
491                    }
492                    
493                    
494                    InventoryHolder holder = event.getInventory().getHolder();
495                    if (holder instanceof Chest || holder instanceof DoubleChest) {
496                            Location loc = getChestLocation(holder);
497                            ChestBean chest = chestMap.get(loc);
498                            
499                            if (chest == null) { //chest was not a snitching chest
500                                    return;
501                            }
502                            
503                            if (chest.getChestType() != ChestBean.SNITCHING) {
504                                    return; // not a snitching chest
505                            }
506                            
507                            
508                            OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
509                            
510                            
511                            Player player = (Player) event.getPlayer();
512                            
513                            ItemCount savedContent = contentMap.get( player.getName() );
514                            
515                            if (savedContent == null) {
516                                    return;
517                            }
518                            
519                            contentMap.remove( player.getName() );
520                            
521                            ItemCount content = countItems( event.getInventory().getContents() );
522                                                    
523                            Set<Integer> combinedKeyset = new TreeSet<Integer>();                  
524                            combinedKeyset.addAll( savedContent.keySet() );
525                            combinedKeyset.addAll( content.keySet() );
526                            
527                            for (Integer item : combinedKeyset ) {
528                                    Integer savedcount = savedContent.get(item);                            
529                                    Integer count = content.get(item);
530                                    
531                                    if (savedcount == null)
532                                            savedcount = 0;
533                                    if (count == null)
534                                            count = 0;
535                                    
536                                    
537                                    int diff = Math.abs( savedcount - count);
538                                    
539                                    if (diff > 0) {
540                                            String material = Material.getMaterial(item).name();
541                                            String msg = null;
542                                            
543                                            if (count > savedcount) {
544                                                    msg = player.getName() + " added "   + diff + " units of " + material + "(" +item + ") to "   + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
545                                            } else { //(count < savedcount)
546                                                    msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
547                                            }
548                                    
549                                    
550                                            server.getLogger().info( "[AdvancedChest]" + msg);
551                                            plugin.getMessageWrapper().sendMessage("system", owner, msg);
552                                    }
553                                    
554                            }
555                            
556                            
557                    }
558            }
559            
560            ItemCount countItems(ItemStack[] input) {
561                    ItemCount output = new ItemCount();
562                    for (int i=0; i<input.length; i++) {
563                            ItemStack current = input[i];
564                            if (current == null)
565                                    continue;
566                            
567                            int type = current.getTypeId();
568                            
569                            Integer amount = output.get(type);
570                            if (amount == null)
571                                    amount = 0;
572                            
573                            output.put(type, amount + current.getAmount() );                        
574                    }
575                    return output;
576            }
577            /*
578            ItemStack[] cloneItemStacks(ItemStack[] input) {
579                    ItemStack[] output = new ItemStack[ input.length ];
580                    for (int i=0; i<input.length; i++) {
581                            if (input[i] != null) {
582                                    output[i] = input[i].clone();
583                            } else {
584                                    output[i] = new ItemStack(0, 0);
585                            }
586                    }
587                    return output;
588            }*/
589            
590                    
591  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20