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.getOwner().equals( player.getName() ) ) { |
144 |
|
player.sendMessage("You can not remove lock from a chest you don't own"); |
145 |
|
return true; |
146 |
|
} |
147 |
|
if (chest != null) { |
148 |
|
player.sendMessage("[LockedChest] Removing protection from chest"); |
149 |
|
removeChest(loc); |
150 |
|
} else { |
151 |
|
player.sendMessage("This chest is not protected"); |
152 |
|
} |
153 |
|
return true; |
154 |
|
} |
155 |
|
|
156 |
|
if (cmd.equals("addplayer") || cmd.equals("removeplayer")) { |
157 |
|
if (chest == null) { |
158 |
|
player.sendMessage("This chest is not protected"); |
159 |
|
return true; |
160 |
|
} |
161 |
|
if (! chest.getOwner().equals( player.getName() ) ) { |
162 |
|
player.sendMessage("You can not add/remove players from a chest you don't own"); |
163 |
|
return true; |
164 |
|
} |
165 |
|
if (args.length != 2) { |
166 |
|
player.sendMessage("You need to specify which player to add or remove"); |
167 |
|
return true; |
168 |
|
} |
169 |
|
OfflinePlayer p2 = server.getOfflinePlayer(args[1]); |
170 |
|
if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { |
171 |
|
player.sendMessage("Unknown user: " + args[1] ); |
172 |
|
return true; |
173 |
|
} |
174 |
|
|
175 |
|
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
176 |
|
if (cmd.equals("addplayer")) { |
177 |
|
players.add(p2.getName()); |
178 |
|
} else { |
179 |
|
players.remove(p2.getName()); |
180 |
|
} |
181 |
|
|
182 |
|
chest.setModifyPlayers( Util.setToString(players) ); |
183 |
|
plugin.getDatabase().save( chest ); |
184 |
|
player.sendMessage("ok"); |
185 |
|
return true; |
186 |
|
} |
187 |
|
|
188 |
|
/* |
189 |
if (chest != null) { |
if (chest != null) { |
190 |
if (chest.getOwner().equals(player.getName())) { |
if (chest.getOwner().equals(player.getName())) { |
191 |
player.sendMessage("[LockedChest] Removing lock from chest"); |
player.sendMessage("[LockedChest] Removing lock from chest"); |
192 |
removeChest(loc); |
removeChest(loc); |
193 |
} else { |
} else { |
194 |
player.sendMessage("[LockedChest] Chest is already locked"); |
player.sendMessage("[LockedChest] Chest is already protected"); |
195 |
} |
} |
196 |
|
|
197 |
return true; |
return true; |
206 |
|
|
207 |
|
|
208 |
player.sendMessage("[LockedChest] Chest is now locked"); |
player.sendMessage("[LockedChest] Chest is now locked"); |
209 |
|
*/ |
210 |
|
|
211 |
|
player.sendMessage("Unknown argument, " + cmd); |
212 |
|
|
213 |
return true; |
return true; |
214 |
} |
} |
216 |
@EventHandler |
@EventHandler |
217 |
public void onBlockBreak(BlockBreakEvent event) { |
public void onBlockBreak(BlockBreakEvent event) { |
218 |
Location loc = event.getBlock().getLocation(); |
Location loc = event.getBlock().getLocation(); |
219 |
LockedChestBean chest = chestMap.get(loc); |
ChestBean chest = chestMap.get(loc); |
220 |
if (chest != null) { |
if (chest != null) { |
221 |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
222 |
removeChest(loc); |
removeChest(loc); |
223 |
event.getPlayer().sendMessage("[LockedChest] The destroyed chest was locked"); |
event.getPlayer().sendMessage("[AdvancedChest] The destroyed chest was locked or snitching"); |
224 |
} else { |
} else { |
225 |
event.setCancelled(true); |
event.setCancelled(true); |
226 |
event.getPlayer().sendMessage("You can't destroy that chest"); |
event.getPlayer().sendMessage("You can't destroy that chest"); |
227 |
|
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried breaking a chest owned by " + chest.getOwner() ); |
228 |
} |
} |
229 |
} |
} |
230 |
} |
} |
231 |
public void addChest(Location loc, LockedChestBean chest) { |
public void addChest(Location loc, ChestBean chest) { |
232 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
233 |
if (chest.isDoublechest()) { |
if (chest.isDoublechest()) { |
234 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
241 |
} |
} |
242 |
|
|
243 |
void removeChest(Location loc) { |
void removeChest(Location loc) { |
244 |
LockedChestBean chest = chestMap.remove(loc); |
ChestBean chest = chestMap.remove(loc); |
245 |
if (chest != null) { |
if (chest != null) { |
246 |
if (chest.isDoublechest()){ |
if (chest.isDoublechest()){ |
247 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
252 |
} |
} |
253 |
|
|
254 |
int loadChestsWorker() { |
int loadChestsWorker() { |
255 |
List<LockedChestBean> chestlist = plugin.getDatabase().find( LockedChestBean.class).findList(); |
List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList(); |
256 |
for (LockedChestBean chest : chestlist) { |
for (ChestBean chest : chestlist) { |
257 |
Location loc = getChestLocation(server, chest); |
Location loc = getChestLocation(server, chest); |
258 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
259 |
|
|
273 |
|
|
274 |
void loadChests() { |
void loadChests() { |
275 |
int count = loadChestsWorker(); |
int count = loadChestsWorker(); |
276 |
plugin.getLogger().info("[LockedChest] loaded " + count + " chests"); |
server.getLogger().info("[AdvancedChest] loaded " + count + " chests"); |
277 |
} |
} |
278 |
|
|
279 |
|
|
280 |
public LockedChestBean createChest(String owner, String description, Location loc) { |
public ChestBean createChest(String owner, String description, Location loc) { |
281 |
|
|
282 |
LockedChestBean chest = new LockedChestBean(); |
ChestBean chest = new ChestBean(); |
283 |
chest.setOwner(owner); |
chest.setOwner(owner); |
284 |
chest.setDescription(description); |
chest.setDescription(description); |
285 |
setChestLocation(chest, loc); |
setChestLocation(chest, loc); |
288 |
} |
} |
289 |
|
|
290 |
|
|
291 |
public void setChestLocation(LockedChestBean chest, Location loc) { |
public void setChestLocation(ChestBean chest, Location loc) { |
292 |
chest.setWorld( loc.getWorld().getName() ); |
chest.setWorld( loc.getWorld().getName() ); |
293 |
chest.setX( loc.getBlockX() ); |
chest.setX( loc.getBlockX() ); |
294 |
chest.setY( loc.getBlockY() ); |
chest.setY( loc.getBlockY() ); |
295 |
chest.setZ( loc.getBlockZ() ); |
chest.setZ( loc.getBlockZ() ); |
296 |
} |
} |
297 |
|
|
298 |
public Location getChestLocation(Server server, LockedChestBean chest) { |
public Location getChestLocation(Server server, ChestBean chest) { |
299 |
World wrld = server.getWorld(chest.getWorld()); |
World wrld = server.getWorld(chest.getWorld()); |
300 |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
301 |
} |
} |
353 |
|
|
354 |
Location chestloc = getNeighborChest( block.getLocation() ); |
Location chestloc = getNeighborChest( block.getLocation() ); |
355 |
if (chestloc != null) { |
if (chestloc != null) { |
356 |
LockedChestBean chest = chestMap.get(chestloc); |
ChestBean chest = chestMap.get(chestloc); |
357 |
|
|
358 |
if (chest != null) { //the neighbor is a locked chest |
if (chest != null) { //the neighbor is a locked chest |
359 |
|
|
362 |
addChest(chestloc, chest); |
addChest(chestloc, chest); |
363 |
|
|
364 |
|
|
365 |
event.getPlayer().sendMessage( "[LockedChest] Chest has been expanded" ); |
event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); |
366 |
} |
} |
367 |
|
|
368 |
} |
} |
369 |
} |
} |
370 |
|
|
371 |
|
|
372 |
|
@EventHandler |
373 |
|
public void onChestExplode(EntityExplodeEvent event) { |
374 |
|
for (Block b : event.blockList() ) { |
375 |
|
ChestBean chest = chestMap.get( b.getLocation() ); |
376 |
|
if (chest != null) { |
377 |
|
server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() ); |
378 |
|
event.setCancelled( true ); |
379 |
|
return; |
380 |
|
} |
381 |
|
} |
382 |
|
} |
383 |
|
|
384 |
// prevent a user from opening a chest |
// prevent a user from opening a chest |
385 |
@EventHandler |
@EventHandler |
391 |
|
|
392 |
Location loc = b.getLocation(); |
Location loc = b.getLocation(); |
393 |
|
|
394 |
LockedChestBean chest = chestMap.get( loc ); |
ChestBean chest = chestMap.get( loc ); |
395 |
if (chest == null) { |
if (chest == null) { |
396 |
return; //chest not surveyed by this plugin |
return; //chest not surveyed by this plugin |
397 |
} |
} |
398 |
|
|
399 |
|
if (chest.getChestType() != ChestBean.LOCKED ) { |
400 |
|
return; //this is not a locked chests |
401 |
|
} |
402 |
|
|
403 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
404 |
if (player.getName().equals(chest.getOwner() )) { |
if (player.getName().equals(chest.getOwner() )) { |
405 |
return; //chest is opened by it's owner |
return; //chest is opened by it's owner |
406 |
} |
} |
407 |
|
|
408 |
if (chest.getModifyPlayers() != null && chest.getModifyPlayers().length()>0) { |
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
409 |
String modplayers[] = chest.getModifyPlayers().split(","); |
if ( players.contains(player.getName()) ) { |
410 |
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 |
|
|
} |
|
|
} |
|
411 |
} |
} |
412 |
|
|
413 |
|
|
414 |
|
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner() ); |
415 |
player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); |
player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); |
416 |
event.setCancelled(true); |
event.setCancelled(true); |
417 |
} |
} |
418 |
} |
} |
419 |
} |
} |
420 |
|
|
421 |
|
@EventHandler |
422 |
|
public void onChestOpen(InventoryOpenEvent event) { |
423 |
|
|
424 |
|
if (! (event.getPlayer() instanceof Player)) { |
425 |
|
return; |
426 |
|
} |
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
InventoryHolder holder = event.getInventory().getHolder(); |
431 |
|
if (holder instanceof Chest || holder instanceof DoubleChest) { |
432 |
|
Location loc = getChestLocation(holder); |
433 |
|
|
434 |
|
ChestBean chest = chestMap.get( loc ); |
435 |
|
if (chest == null) { |
436 |
|
return; //chest not surveyed by this plugin |
437 |
|
} |
438 |
|
|
439 |
|
if (chest.getChestType() != ChestBean.SNITCHING) { |
440 |
|
return; // not a snitching chest |
441 |
|
} |
442 |
|
|
443 |
|
|
444 |
|
Player player = (Player) event.getPlayer(); |
445 |
|
if (player.getName().equals(chest.getOwner() )) { |
446 |
|
return; //chest is owned by it's own player |
447 |
|
} |
448 |
|
|
449 |
|
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
450 |
|
if ( players.contains(player.getName()) ) { |
451 |
|
return; //this player is on the whitelist so he may open |
452 |
|
} |
453 |
|
|
454 |
|
|
455 |
|
ItemCount contents = countItems( event.getInventory().getContents() ); |
456 |
|
|
457 |
|
contentMap.put(player.getName(), contents ); |
458 |
|
} |
459 |
|
} |
460 |
|
|
461 |
|
@EventHandler |
462 |
|
public void onChestClose(InventoryCloseEvent event) { |
463 |
|
if (! (event.getPlayer() instanceof Player)) { |
464 |
|
return; |
465 |
|
} |
466 |
|
|
467 |
|
|
468 |
|
InventoryHolder holder = event.getInventory().getHolder(); |
469 |
|
if (holder instanceof Chest || holder instanceof DoubleChest) { |
470 |
|
Location loc = getChestLocation(holder); |
471 |
|
ChestBean chest = chestMap.get(loc); |
472 |
|
|
473 |
|
if (chest == null) { //chest was not a snitching chest |
474 |
|
return; |
475 |
|
} |
476 |
|
|
477 |
|
if (chest.getChestType() != ChestBean.SNITCHING) { |
478 |
|
return; // not a snitching chest |
479 |
|
} |
480 |
|
|
481 |
|
|
482 |
|
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
483 |
|
|
484 |
|
|
485 |
|
Player player = (Player) event.getPlayer(); |
486 |
|
|
487 |
|
ItemCount savedContent = contentMap.get( player.getName() ); |
488 |
|
|
489 |
|
if (savedContent == null) { |
490 |
|
return; |
491 |
|
} |
492 |
|
|
493 |
|
contentMap.remove( player.getName() ); |
494 |
|
|
495 |
|
ItemCount content = countItems( event.getInventory().getContents() ); |
496 |
|
|
497 |
|
Set<Integer> combinedKeyset = new TreeSet<Integer>(); |
498 |
|
combinedKeyset.addAll( savedContent.keySet() ); |
499 |
|
combinedKeyset.addAll( content.keySet() ); |
500 |
|
|
501 |
|
for (Integer item : combinedKeyset ) { |
502 |
|
Integer savedcount = savedContent.get(item); |
503 |
|
Integer count = content.get(item); |
504 |
|
|
505 |
|
if (savedcount == null) |
506 |
|
savedcount = 0; |
507 |
|
if (count == null) |
508 |
|
count = 0; |
509 |
|
|
510 |
|
|
511 |
|
int diff = Math.abs( savedcount - count); |
512 |
|
|
513 |
|
if (diff > 0) { |
514 |
|
String material = Material.getMaterial(item).name(); |
515 |
|
String msg = null; |
516 |
|
|
517 |
|
if (count > savedcount) { |
518 |
|
msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
519 |
|
} else { //(count < savedcount) |
520 |
|
msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
521 |
|
} |
522 |
|
|
523 |
|
|
524 |
|
server.getLogger().info( "[AdvancedChest]" + msg); |
525 |
|
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
526 |
|
} |
527 |
|
|
528 |
|
} |
529 |
|
|
530 |
|
|
531 |
|
} |
532 |
|
} |
533 |
|
|
534 |
|
ItemCount countItems(ItemStack[] input) { |
535 |
|
ItemCount output = new ItemCount(); |
536 |
|
for (int i=0; i<input.length; i++) { |
537 |
|
ItemStack current = input[i]; |
538 |
|
if (current == null) |
539 |
|
continue; |
540 |
|
|
541 |
|
int type = current.getTypeId(); |
542 |
|
|
543 |
|
Integer amount = output.get(type); |
544 |
|
if (amount == null) |
545 |
|
amount = 0; |
546 |
|
|
547 |
|
output.put(type, amount + current.getAmount() ); |
548 |
|
} |
549 |
|
return output; |
550 |
|
} |
551 |
|
/* |
552 |
|
ItemStack[] cloneItemStacks(ItemStack[] input) { |
553 |
|
ItemStack[] output = new ItemStack[ input.length ]; |
554 |
|
for (int i=0; i<input.length; i++) { |
555 |
|
if (input[i] != null) { |
556 |
|
output[i] = input[i].clone(); |
557 |
|
} else { |
558 |
|
output[i] = new ItemStack(0, 0); |
559 |
|
} |
560 |
|
} |
561 |
|
return output; |
562 |
|
}*/ |
563 |
|
|
564 |
|
|
565 |
} |
} |