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

  ViewVC Help
Powered by ViewVC 1.1.20