/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/AdvancedChest.java
ViewVC logotype

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

  ViewVC Help
Powered by ViewVC 1.1.20