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

  ViewVC Help
Powered by ViewVC 1.1.20