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

  ViewVC Help
Powered by ViewVC 1.1.20