/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/AdvancedChest.java
ViewVC logotype

Annotation of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/AdvancedChest.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20