3 |
|
|
4 |
import java.util.HashMap; |
import java.util.HashMap; |
5 |
import java.util.List; |
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; |
import org.bukkit.ChatColor; |
11 |
import org.bukkit.Location; |
import org.bukkit.Location; |
12 |
import org.bukkit.Material; |
import org.bukkit.Material; |
13 |
|
import org.bukkit.OfflinePlayer; |
14 |
import org.bukkit.Server; |
import org.bukkit.Server; |
15 |
import org.bukkit.World; |
import org.bukkit.World; |
16 |
import org.bukkit.block.Block; |
import org.bukkit.block.Block; |
25 |
import org.bukkit.event.block.Action; |
import org.bukkit.event.block.Action; |
26 |
import org.bukkit.event.block.BlockBreakEvent; |
import org.bukkit.event.block.BlockBreakEvent; |
27 |
import org.bukkit.event.block.BlockPlaceEvent; |
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; |
import org.bukkit.event.player.PlayerInteractEvent; |
32 |
import org.bukkit.inventory.InventoryHolder; |
import org.bukkit.inventory.InventoryHolder; |
33 |
|
import org.bukkit.inventory.ItemStack; |
34 |
|
|
35 |
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
36 |
|
import dk.thoerup.bukkit.hoeruputils.Util; |
37 |
|
|
38 |
|
|
39 |
public class LockedChest implements Listener, CommandExecutor{ |
|
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,LockedChestBean> chestMap = new HashMap<Location, LockedChestBean>(); |
HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>(); |
50 |
|
|
51 |
|
|
52 |
HoerupUtilsPlugin plugin; |
HoerupUtilsPlugin plugin; |
53 |
Server server; |
Server server; |
54 |
|
|
55 |
public LockedChest(HoerupUtilsPlugin plugin, Runnable r) { |
public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) { |
56 |
this.plugin = plugin; |
this.plugin = plugin; |
57 |
server = plugin.getServer(); |
server = plugin.getServer(); |
58 |
try { |
try { |
74 |
|
|
75 |
Player player = (Player) sender; |
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 |
Block b = player.getTargetBlock(null, 30); |
|
84 |
Location loc = b.getLocation(); |
Block b = player.getTargetBlock(null, 30); |
85 |
|
|
86 |
if (b.getTypeId() != 54) { |
if (b.getTypeId() != 54) { |
87 |
player.sendMessage("[LockedChest] Please look at the chest you want to lock"); |
player.sendMessage("[Chest] Please look at the chest you want to protect"); |
88 |
return true; |
return true; |
89 |
} |
} |
90 |
|
|
91 |
|
Location loc = b.getLocation(); |
92 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
93 |
|
|
94 |
LockedChestBean chest = chestMap.get(loc); |
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("addplayer") || cmd.equals("removeplayer")) { |
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 add/remove players from a chest you don't own"); |
165 |
|
return true; |
166 |
|
} |
167 |
|
if (args.length != 2) { |
168 |
|
player.sendMessage("You need to specify which player to add or remove"); |
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 |
|
|
177 |
|
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
178 |
|
if (cmd.equals("addplayer")) { |
179 |
|
players.add(p2.getName()); |
180 |
|
} else { |
181 |
|
players.remove(p2.getName()); |
182 |
|
} |
183 |
|
|
184 |
|
chest.setModifyPlayers( Util.setToString(players) ); |
185 |
|
plugin.getDatabase().save( chest ); |
186 |
|
player.sendMessage("ok"); |
187 |
|
return true; |
188 |
|
} |
189 |
|
|
190 |
|
/* |
191 |
if (chest != null) { |
if (chest != null) { |
192 |
if (chest.getOwner().equals(player.getName())) { |
if (chest.getOwner().equals(player.getName())) { |
193 |
player.sendMessage("[LockedChest] Removing lock from chest"); |
player.sendMessage("[LockedChest] Removing lock from chest"); |
194 |
removeChest(loc); |
removeChest(loc); |
195 |
} else { |
} else { |
196 |
player.sendMessage("[LockedChest] Chest is already locked"); |
player.sendMessage("[LockedChest] Chest is already protected"); |
197 |
} |
} |
198 |
|
|
199 |
return true; |
return true; |
208 |
|
|
209 |
|
|
210 |
player.sendMessage("[LockedChest] Chest is now locked"); |
player.sendMessage("[LockedChest] Chest is now locked"); |
211 |
|
*/ |
212 |
|
|
213 |
|
player.sendMessage("Unknown argument, " + cmd); |
214 |
|
|
215 |
return true; |
return true; |
216 |
} |
} |
218 |
@EventHandler |
@EventHandler |
219 |
public void onBlockBreak(BlockBreakEvent event) { |
public void onBlockBreak(BlockBreakEvent event) { |
220 |
Location loc = event.getBlock().getLocation(); |
Location loc = event.getBlock().getLocation(); |
221 |
LockedChestBean chest = chestMap.get(loc); |
ChestBean chest = chestMap.get(loc); |
222 |
if (chest != null) { |
if (chest != null) { |
223 |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
224 |
removeChest(loc); |
removeChest(loc); |
225 |
event.getPlayer().sendMessage("[LockedChest] The destroyed chest was locked"); |
event.getPlayer().sendMessage("[AdvancedChest] The destroyed chest was locked or snitching"); |
226 |
} else { |
} else { |
227 |
event.setCancelled(true); |
event.setCancelled(true); |
228 |
event.getPlayer().sendMessage("You can't destroy that chest"); |
event.getPlayer().sendMessage("You can't destroy that chest"); |
229 |
|
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried breaking a chest owned by " + chest.getOwner() ); |
230 |
} |
} |
231 |
} |
} |
232 |
} |
} |
233 |
public void addChest(Location loc, LockedChestBean chest) { |
public void addChest(Location loc, ChestBean chest) { |
234 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
235 |
if (chest.isDoublechest()) { |
if (chest.isDoublechest()) { |
236 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
243 |
} |
} |
244 |
|
|
245 |
void removeChest(Location loc) { |
void removeChest(Location loc) { |
246 |
LockedChestBean chest = chestMap.remove(loc); |
ChestBean chest = chestMap.remove(loc); |
247 |
if (chest != null) { |
if (chest != null) { |
248 |
if (chest.isDoublechest()){ |
if (chest.isDoublechest()){ |
249 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
254 |
} |
} |
255 |
|
|
256 |
int loadChestsWorker() { |
int loadChestsWorker() { |
257 |
List<LockedChestBean> chestlist = plugin.getDatabase().find( LockedChestBean.class).findList(); |
List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList(); |
258 |
for (LockedChestBean chest : chestlist) { |
for (ChestBean chest : chestlist) { |
259 |
Location loc = getChestLocation(server, chest); |
Location loc = getChestLocation(server, chest); |
260 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
261 |
|
|
275 |
|
|
276 |
void loadChests() { |
void loadChests() { |
277 |
int count = loadChestsWorker(); |
int count = loadChestsWorker(); |
278 |
plugin.getLogger().info("[LockedChest] loaded " + count + " chests"); |
server.getLogger().info("[AdvancedChest] loaded " + count + " chests"); |
279 |
} |
} |
280 |
|
|
281 |
|
|
282 |
public LockedChestBean createChest(String owner, String description, Location loc) { |
public ChestBean createChest(String owner, String description, Location loc) { |
283 |
|
|
284 |
LockedChestBean chest = new LockedChestBean(); |
ChestBean chest = new ChestBean(); |
285 |
chest.setOwner(owner); |
chest.setOwner(owner); |
286 |
chest.setDescription(description); |
chest.setDescription(description); |
287 |
setChestLocation(chest, loc); |
setChestLocation(chest, loc); |
290 |
} |
} |
291 |
|
|
292 |
|
|
293 |
public void setChestLocation(LockedChestBean chest, Location loc) { |
public void setChestLocation(ChestBean chest, Location loc) { |
294 |
chest.setWorld( loc.getWorld().getName() ); |
chest.setWorld( loc.getWorld().getName() ); |
295 |
chest.setX( loc.getBlockX() ); |
chest.setX( loc.getBlockX() ); |
296 |
chest.setY( loc.getBlockY() ); |
chest.setY( loc.getBlockY() ); |
297 |
chest.setZ( loc.getBlockZ() ); |
chest.setZ( loc.getBlockZ() ); |
298 |
} |
} |
299 |
|
|
300 |
public Location getChestLocation(Server server, LockedChestBean chest) { |
public Location getChestLocation(Server server, ChestBean chest) { |
301 |
World wrld = server.getWorld(chest.getWorld()); |
World wrld = server.getWorld(chest.getWorld()); |
302 |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
303 |
} |
} |
355 |
|
|
356 |
Location chestloc = getNeighborChest( block.getLocation() ); |
Location chestloc = getNeighborChest( block.getLocation() ); |
357 |
if (chestloc != null) { |
if (chestloc != null) { |
358 |
LockedChestBean chest = chestMap.get(chestloc); |
ChestBean chest = chestMap.get(chestloc); |
359 |
|
|
360 |
if (chest != null) { //the neighbor is a locked chest |
if (chest != null) { //the neighbor is a locked chest |
361 |
|
|
364 |
addChest(chestloc, chest); |
addChest(chestloc, chest); |
365 |
|
|
366 |
|
|
367 |
event.getPlayer().sendMessage( "[LockedChest] Chest has been expanded" ); |
event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); |
368 |
} |
} |
369 |
|
|
370 |
} |
} |
371 |
} |
} |
372 |
|
|
373 |
|
|
374 |
|
@EventHandler |
375 |
|
public void onChestExplode(EntityExplodeEvent event) { |
376 |
|
for (Block b : event.blockList() ) { |
377 |
|
ChestBean chest = chestMap.get( b.getLocation() ); |
378 |
|
if (chest != null) { |
379 |
|
server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() ); |
380 |
|
event.setCancelled( true ); |
381 |
|
return; |
382 |
|
} |
383 |
|
} |
384 |
|
} |
385 |
|
|
386 |
// prevent a user from opening a chest |
// prevent a user from opening a chest |
387 |
@EventHandler |
@EventHandler |
393 |
|
|
394 |
Location loc = b.getLocation(); |
Location loc = b.getLocation(); |
395 |
|
|
396 |
LockedChestBean chest = chestMap.get( loc ); |
ChestBean chest = chestMap.get( loc ); |
397 |
if (chest == null) { |
if (chest == null) { |
398 |
return; //chest not surveyed by this plugin |
return; //chest not surveyed by this plugin |
399 |
} |
} |
400 |
|
|
401 |
|
if (chest.getChestType() != ChestBean.LOCKED ) { |
402 |
|
return; //this is not a locked chests |
403 |
|
} |
404 |
|
|
405 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
406 |
if (player.getName().equals(chest.getOwner() )) { |
if (player.getName().equals(chest.getOwner() )) { |
407 |
return; //chest is opened by it's owner |
return; //chest is opened by it's owner |
408 |
} |
} |
409 |
|
|
410 |
if (chest.getModifyPlayers() != null && chest.getModifyPlayers().length()>0) { |
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
411 |
String modplayers[] = chest.getModifyPlayers().split(","); |
if ( players.contains(player.getName()) ) { |
412 |
for (String p : modplayers) { |
return; //this player is on the whitelist so he may open |
|
if ( player.getName().equals(p) ) { |
|
|
return; //this player is on the whitelist so he may open |
|
|
} |
|
|
} |
|
413 |
} |
} |
414 |
|
|
415 |
|
|
416 |
|
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner() ); |
417 |
player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); |
player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); |
418 |
event.setCancelled(true); |
event.setCancelled(true); |
419 |
} |
} |
420 |
} |
} |
421 |
} |
} |
422 |
|
|
423 |
|
@EventHandler |
424 |
|
public void onChestOpen(InventoryOpenEvent event) { |
425 |
|
|
426 |
|
if (! (event.getPlayer() instanceof Player)) { |
427 |
|
return; |
428 |
|
} |
429 |
|
|
430 |
|
|
431 |
|
|
432 |
|
InventoryHolder holder = event.getInventory().getHolder(); |
433 |
|
if (holder instanceof Chest || holder instanceof DoubleChest) { |
434 |
|
Location loc = getChestLocation(holder); |
435 |
|
|
436 |
|
ChestBean chest = chestMap.get( loc ); |
437 |
|
if (chest == null) { |
438 |
|
return; //chest not surveyed by this plugin |
439 |
|
} |
440 |
|
|
441 |
|
if (chest.getChestType() != ChestBean.SNITCHING) { |
442 |
|
return; // not a snitching chest |
443 |
|
} |
444 |
|
|
445 |
|
|
446 |
|
Player player = (Player) event.getPlayer(); |
447 |
|
if (player.getName().equals(chest.getOwner() )) { |
448 |
|
return; //chest is owned by it's own player |
449 |
|
} |
450 |
|
|
451 |
|
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
452 |
|
if ( players.contains(player.getName()) ) { |
453 |
|
return; //this player is on the whitelist so he may open |
454 |
|
} |
455 |
|
|
456 |
|
|
457 |
|
ItemCount contents = countItems( event.getInventory().getContents() ); |
458 |
|
|
459 |
|
contentMap.put(player.getName(), contents ); |
460 |
|
} |
461 |
|
} |
462 |
|
|
463 |
|
@EventHandler |
464 |
|
public void onChestClose(InventoryCloseEvent event) { |
465 |
|
if (! (event.getPlayer() instanceof Player)) { |
466 |
|
return; |
467 |
|
} |
468 |
|
|
469 |
|
|
470 |
|
InventoryHolder holder = event.getInventory().getHolder(); |
471 |
|
if (holder instanceof Chest || holder instanceof DoubleChest) { |
472 |
|
Location loc = getChestLocation(holder); |
473 |
|
ChestBean chest = chestMap.get(loc); |
474 |
|
|
475 |
|
if (chest == null) { //chest was not a snitching chest |
476 |
|
return; |
477 |
|
} |
478 |
|
|
479 |
|
if (chest.getChestType() != ChestBean.SNITCHING) { |
480 |
|
return; // not a snitching chest |
481 |
|
} |
482 |
|
|
483 |
|
|
484 |
|
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
485 |
|
|
486 |
|
|
487 |
|
Player player = (Player) event.getPlayer(); |
488 |
|
|
489 |
|
ItemCount savedContent = contentMap.get( player.getName() ); |
490 |
|
|
491 |
|
if (savedContent == null) { |
492 |
|
return; |
493 |
|
} |
494 |
|
|
495 |
|
contentMap.remove( player.getName() ); |
496 |
|
|
497 |
|
ItemCount content = countItems( event.getInventory().getContents() ); |
498 |
|
|
499 |
|
Set<Integer> combinedKeyset = new TreeSet<Integer>(); |
500 |
|
combinedKeyset.addAll( savedContent.keySet() ); |
501 |
|
combinedKeyset.addAll( content.keySet() ); |
502 |
|
|
503 |
|
for (Integer item : combinedKeyset ) { |
504 |
|
Integer savedcount = savedContent.get(item); |
505 |
|
Integer count = content.get(item); |
506 |
|
|
507 |
|
if (savedcount == null) |
508 |
|
savedcount = 0; |
509 |
|
if (count == null) |
510 |
|
count = 0; |
511 |
|
|
512 |
|
|
513 |
|
int diff = Math.abs( savedcount - count); |
514 |
|
|
515 |
|
if (diff > 0) { |
516 |
|
String material = Material.getMaterial(item).name(); |
517 |
|
String msg = null; |
518 |
|
|
519 |
|
if (count > savedcount) { |
520 |
|
msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
521 |
|
} else { //(count < savedcount) |
522 |
|
msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
523 |
|
} |
524 |
|
|
525 |
|
|
526 |
|
server.getLogger().info( "[AdvancedChest]" + msg); |
527 |
|
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
528 |
|
} |
529 |
|
|
530 |
|
} |
531 |
|
|
532 |
|
|
533 |
|
} |
534 |
|
} |
535 |
|
|
536 |
|
ItemCount countItems(ItemStack[] input) { |
537 |
|
ItemCount output = new ItemCount(); |
538 |
|
for (int i=0; i<input.length; i++) { |
539 |
|
ItemStack current = input[i]; |
540 |
|
if (current == null) |
541 |
|
continue; |
542 |
|
|
543 |
|
int type = current.getTypeId(); |
544 |
|
|
545 |
|
Integer amount = output.get(type); |
546 |
|
if (amount == null) |
547 |
|
amount = 0; |
548 |
|
|
549 |
|
output.put(type, amount + current.getAmount() ); |
550 |
|
} |
551 |
|
return output; |
552 |
|
} |
553 |
|
/* |
554 |
|
ItemStack[] cloneItemStacks(ItemStack[] input) { |
555 |
|
ItemStack[] output = new ItemStack[ input.length ]; |
556 |
|
for (int i=0; i<input.length; i++) { |
557 |
|
if (input[i] != null) { |
558 |
|
output[i] = input[i].clone(); |
559 |
|
} else { |
560 |
|
output[i] = new ItemStack(0, 0); |
561 |
|
} |
562 |
|
} |
563 |
|
return output; |
564 |
|
}*/ |
565 |
|
|
566 |
|
|
567 |
} |
} |