/[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 1813 - (show annotations) (download)
Thu Jun 28 10:19:26 2012 UTC (11 years, 10 months ago) by torben
File size: 14944 byte(s)
Use AdvancedChest for all log messages - and built against latest release bukkit
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) [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("[LockedChest] Removing protection from chest");
145 removeChest(loc);
146 } else {
147 player.sendMessage("This chest is not protected");
148 }
149 return true;
150 }
151
152 if (cmd.equals("addplayer") || cmd.equals("removeplayer")) {
153 if (chest == null) {
154 player.sendMessage("This chest is not protected");
155 return true;
156 }
157 if (args.length != 2) {
158 player.sendMessage("You need to specify which player to add or remove");
159 return true;
160 }
161 OfflinePlayer p2 = server.getOfflinePlayer(args[1]);
162 if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) {
163 player.sendMessage("Unknown user: " + args[1] );
164 return true;
165 }
166
167 Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
168 if (cmd.equals("addplayer")) {
169 players.add(p2.getName());
170 } else {
171 players.remove(p2.getName());
172 }
173
174 chest.setModifyPlayers( Util.setToString(players) );
175 plugin.getDatabase().save( chest );
176 player.sendMessage("ok");
177 return true;
178 }
179
180 /*
181 if (chest != null) {
182 if (chest.getOwner().equals(player.getName())) {
183 player.sendMessage("[LockedChest] Removing lock from chest");
184 removeChest(loc);
185 } else {
186 player.sendMessage("[LockedChest] Chest is already protected");
187 }
188
189 return true;
190 }
191
192 chest = createChest(player.getName(), "", loc);
193 if (loc2 != null) {
194 chest.setDoublechest(true);
195 }
196
197 addChest(loc, chest);
198
199
200 player.sendMessage("[LockedChest] Chest is now locked");
201 */
202
203 player.sendMessage("Unknown argument, " + cmd);
204
205 return true;
206 }
207
208 @EventHandler
209 public void onBlockBreak(BlockBreakEvent event) {
210 Location loc = event.getBlock().getLocation();
211 ChestBean chest = chestMap.get(loc);
212 if (chest != null) {
213 if (chest.getOwner().equals(event.getPlayer().getName())) {
214 removeChest(loc);
215 event.getPlayer().sendMessage("[AdvancedChest] The destroyed chest was locked or snitching");
216 } else {
217 event.setCancelled(true);
218 event.getPlayer().sendMessage("You can't destroy that chest");
219 server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried breaking a chest owned by " + chest.getOwner() );
220 }
221 }
222 }
223 public void addChest(Location loc, ChestBean chest) {
224 chestMap.put(loc, chest);
225 if (chest.isDoublechest()) {
226 Location loc2 = getNeighborChest(loc);
227 chestMap.put(loc2, chest);
228 }
229 plugin.getDatabase().save(chest);
230
231 reloadChests();
232
233 }
234
235 void removeChest(Location loc) {
236 ChestBean chest = chestMap.remove(loc);
237 if (chest != null) {
238 if (chest.isDoublechest()){
239 Location loc2 = getNeighborChest(loc);
240 chestMap.remove(loc2);
241 }
242 plugin.getDatabase().delete(chest);
243 }
244 }
245
246 int loadChestsWorker() {
247 List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList();
248 for (ChestBean chest : chestlist) {
249 Location loc = getChestLocation(server, chest);
250 chestMap.put(loc, chest);
251
252 if (chest.isDoublechest()) {
253 Location loc2 = getNeighborChest(loc);
254 chestMap.put(loc2, chest);
255 }
256 }
257
258 return chestlist.size();
259 }
260
261 void reloadChests() {
262 chestMap.clear();
263 loadChestsWorker();
264 }
265
266 void loadChests() {
267 int count = loadChestsWorker();
268 server.getLogger().info("[AdvancedChest] loaded " + count + " chests");
269 }
270
271
272 public ChestBean createChest(String owner, String description, Location loc) {
273
274 ChestBean chest = new ChestBean();
275 chest.setOwner(owner);
276 chest.setDescription(description);
277 setChestLocation(chest, loc);
278
279 return chest;
280 }
281
282
283 public void setChestLocation(ChestBean chest, Location loc) {
284 chest.setWorld( loc.getWorld().getName() );
285 chest.setX( loc.getBlockX() );
286 chest.setY( loc.getBlockY() );
287 chest.setZ( loc.getBlockZ() );
288 }
289
290 public Location getChestLocation(Server server, ChestBean chest) {
291 World wrld = server.getWorld(chest.getWorld());
292 return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());
293 }
294
295
296 /*
297 void saveChests() {
298
299 }*/
300
301 Location getNeighborChest(Location loc) {
302 World world = loc.getWorld();
303
304 Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() );
305 if (world.getBlockAt(target).getType() == Material.CHEST )
306 return target;
307
308 target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() );
309 if (world.getBlockAt(target).getType() == Material.CHEST )
310 return target;
311
312 target = new Location(world, loc.getX(), loc.getY(), loc.getZ() +1);
313 if (world.getBlockAt(target).getType() == Material.CHEST )
314 return target;
315
316 target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1);
317 if (world.getBlockAt(target).getType() == Material.CHEST )
318 return target;
319
320 return null;
321 }
322
323
324 Location getChestLocation(InventoryHolder holder) {
325 Location loc;
326 if ( holder instanceof Chest) {
327 loc = ( (Chest)holder).getLocation();
328 } else {
329 loc = ( (DoubleChest)holder).getLocation();
330 }
331
332 loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5
333 loc.setZ( loc.getBlockZ() ); // -- // --
334
335 return loc;
336 }
337
338 @EventHandler
339 public void onChestPlaced(BlockPlaceEvent event) {
340 Block block = event.getBlock();
341
342 if (block.getType() != Material.CHEST) {
343 return;
344 }
345
346 Location chestloc = getNeighborChest( block.getLocation() );
347 if (chestloc != null) {
348 ChestBean chest = chestMap.get(chestloc);
349
350 if (chest != null) { //the neighbor is a locked chest
351
352
353 chest.setDoublechest(true);
354 addChest(chestloc, chest);
355
356
357 event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" );
358 }
359
360 }
361 }
362
363
364 @EventHandler
365 public void onChestExplode(EntityExplodeEvent event) {
366 for (Block b : event.blockList() ) {
367 ChestBean chest = chestMap.get( b.getLocation() );
368 if (chest != null) {
369 server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() );
370 event.setCancelled( true );
371 return;
372 }
373 }
374 }
375
376 // prevent a user from opening a chest
377 @EventHandler
378 public void onChestInteract(PlayerInteractEvent event) {
379 if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
380 Block b = event.getClickedBlock();
381
382 if (b.getType() == Material.CHEST) {
383
384 Location loc = b.getLocation();
385
386 ChestBean chest = chestMap.get( loc );
387 if (chest == null) {
388 return; //chest not surveyed by this plugin
389 }
390
391 if (chest.getChestType() != ChestBean.LOCKED ) {
392 return; //this is not a locked chests
393 }
394
395 Player player = (Player) event.getPlayer();
396 if (player.getName().equals(chest.getOwner() )) {
397 return; //chest is opened by it's owner
398 }
399
400 Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
401 if ( players.contains(player.getName()) ) {
402 return; //this player is on the whitelist so he may open
403 }
404
405
406 server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner() );
407 player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !");
408 event.setCancelled(true);
409 }
410 }
411 }
412
413 @EventHandler
414 public void onChestOpen(InventoryOpenEvent event) {
415
416 if (! (event.getPlayer() instanceof Player)) {
417 return;
418 }
419
420
421
422 InventoryHolder holder = event.getInventory().getHolder();
423 if (holder instanceof Chest || holder instanceof DoubleChest) {
424 Location loc = getChestLocation(holder);
425
426 ChestBean chest = chestMap.get( loc );
427 if (chest == null) {
428 return; //chest not surveyed by this plugin
429 }
430
431 if (chest.getChestType() != ChestBean.SNITCHING) {
432 return; // not a snitching chest
433 }
434
435
436 Player player = (Player) event.getPlayer();
437 if (player.getName().equals(chest.getOwner() )) {
438 return; //chest is owned by it's own player
439 }
440
441 Set<String> players = Util.stringToSet( chest.getModifyPlayers() );
442 if ( players.contains(player.getName()) ) {
443 return; //this player is on the whitelist so he may open
444 }
445
446
447 ItemCount contents = countItems( event.getInventory().getContents() );
448
449 contentMap.put(player.getName(), contents );
450 }
451 }
452
453 @EventHandler
454 public void onChestClose(InventoryCloseEvent event) {
455 if (! (event.getPlayer() instanceof Player)) {
456 return;
457 }
458
459
460 InventoryHolder holder = event.getInventory().getHolder();
461 if (holder instanceof Chest || holder instanceof DoubleChest) {
462 Location loc = getChestLocation(holder);
463 ChestBean chest = chestMap.get(loc);
464
465 if (chest == null) { //chest was not a snitching chest
466 return;
467 }
468
469 if (chest.getChestType() != ChestBean.SNITCHING) {
470 return; // not a snitching chest
471 }
472
473
474 OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
475
476
477 Player player = (Player) event.getPlayer();
478
479 ItemCount savedContent = contentMap.get( player.getName() );
480
481 if (savedContent == null) {
482 return;
483 }
484
485 contentMap.remove( player.getName() );
486
487 ItemCount content = countItems( event.getInventory().getContents() );
488
489 Set<Integer> combinedKeyset = new TreeSet<Integer>();
490 combinedKeyset.addAll( savedContent.keySet() );
491 combinedKeyset.addAll( content.keySet() );
492
493 for (Integer item : combinedKeyset ) {
494 Integer savedcount = savedContent.get(item);
495 Integer count = content.get(item);
496
497 if (savedcount == null)
498 savedcount = 0;
499 if (count == null)
500 count = 0;
501
502
503 int diff = Math.abs( savedcount - count);
504
505 if (diff > 0) {
506 String material = Material.getMaterial(item).name();
507 String msg = null;
508
509 if (count > savedcount) {
510 msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
511 } else { //(count < savedcount)
512 msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
513 }
514
515
516 server.getLogger().info( "[AdvancedChest]" + msg);
517 plugin.getMessageWrapper().sendMessage("system", owner, msg);
518 }
519
520 }
521
522
523 }
524 }
525
526 ItemCount countItems(ItemStack[] input) {
527 ItemCount output = new ItemCount();
528 for (int i=0; i<input.length; i++) {
529 ItemStack current = input[i];
530 if (current == null)
531 continue;
532
533 int type = current.getTypeId();
534
535 Integer amount = output.get(type);
536 if (amount == null)
537 amount = 0;
538
539 output.put(type, amount + current.getAmount() );
540 }
541 return output;
542 }
543 /*
544 ItemStack[] cloneItemStacks(ItemStack[] input) {
545 ItemStack[] output = new ItemStack[ input.length ];
546 for (int i=0; i<input.length; i++) {
547 if (input[i] != null) {
548 output[i] = input[i].clone();
549 } else {
550 output[i] = new ItemStack(0, 0);
551 }
552 }
553 return output;
554 }*/
555
556
557 }

  ViewVC Help
Powered by ViewVC 1.1.20