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

  ViewVC Help
Powered by ViewVC 1.1.20