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

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

  ViewVC Help
Powered by ViewVC 1.1.20