35 |
|
|
36 |
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
37 |
import dk.thoerup.bukkit.hoeruputils.Util; |
import dk.thoerup.bukkit.hoeruputils.Util; |
38 |
|
import io.ebean.Ebean; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
public class AdvancedChest implements Listener, CommandExecutor{ |
public class AdvancedChest implements Listener, CommandExecutor{ |
43 |
|
|
44 |
class ItemCount extends TreeMap<Integer,Integer> { |
class ItemCount extends TreeMap<Material,Integer> { |
45 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
46 |
}; |
}; |
47 |
|
|
48 |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
49 |
|
|
50 |
|
|
51 |
HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>(); |
HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>(); |
52 |
|
|
53 |
|
|
54 |
HoerupUtilsPlugin plugin; |
HoerupUtilsPlugin plugin; |
55 |
Server server; |
Server server; |
56 |
|
|
57 |
public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) { |
public AdvancedChest(HoerupUtilsPlugin plugin) { |
58 |
this.plugin = plugin; |
this.plugin = plugin; |
59 |
server = plugin.getServer(); |
server = plugin.getServer(); |
60 |
try { |
try { |
65 |
loadChests(); |
loadChests(); |
66 |
} |
} |
67 |
} |
} |
68 |
|
|
69 |
|
|
70 |
@Override |
@Override |
71 |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
73 |
sender.sendMessage("this is not a console command!"); |
sender.sendMessage("this is not a console command!"); |
74 |
return true; |
return true; |
75 |
} |
} |
76 |
|
|
77 |
Player player = (Player) sender; |
Player player = (Player) sender; |
78 |
|
|
79 |
if (args.length == 0) { |
if (args.length == 0) { |
80 |
player.sendMessage("Usage:"); |
player.sendMessage("Usage:"); |
81 |
player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer|setowner|comment) [player]"); |
player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer|setowner|comment) [player]"); |
82 |
return true; |
return true; |
83 |
} |
} |
84 |
|
|
85 |
|
|
86 |
|
Block b = player.getTargetBlock( (Set<Material>)null, 30); |
87 |
|
Material mat = b.getType(); |
88 |
|
|
89 |
|
if (mat != Material.CHEST && mat != Material.HOPPER) { |
90 |
Block b = player.getTargetBlock(null, 30); |
player.sendMessage("[Chest] Please look at the chest/hopper you want to protect"); |
|
|
|
|
if (b.getTypeId() != 54) { |
|
|
player.sendMessage("[Chest] Please look at the chest you want to protect"); |
|
91 |
return true; |
return true; |
92 |
} |
} |
93 |
|
|
94 |
Location loc = b.getLocation(); |
Location loc = b.getLocation(); |
95 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = null; |
96 |
|
if ( mat == Material.HOPPER) { //dont find neighbours for Hoppers |
97 |
|
loc2 = getNeighborChest(loc); |
98 |
|
} |
99 |
|
|
100 |
ChestBean chest = chestMap.get(loc); |
ChestBean chest = chestMap.get(loc); |
101 |
String cmd = args[0].toLowerCase(); |
String cmd = args[0].toLowerCase(); |
102 |
|
|
103 |
if (cmd.equals("status")) { |
if (cmd.equals("status")) { |
104 |
if (chest != null) { |
if (chest != null) { |
105 |
String mode = ""; |
String mode = ""; |
113 |
default: |
default: |
114 |
mode = "unknown ??"; |
mode = "unknown ??"; |
115 |
} |
} |
116 |
|
|
117 |
player.sendMessage(ChatColor.GREEN + "Chest is a " + mode + " chest owned by " + chest.getOwner()); |
player.sendMessage(ChatColor.GREEN + "Chest is a " + mode + " chest owned by " + chest.getOwner()); |
118 |
player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() ); |
player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() ); |
119 |
player.sendMessage(ChatColor.GREEN + "Comment: " + chest.getComment() ); |
player.sendMessage(ChatColor.GREEN + "Comment: " + chest.getComment() ); |
122 |
} |
} |
123 |
return true; |
return true; |
124 |
} |
} |
125 |
|
|
126 |
if (cmd.equals("lock") || cmd.equals("snitch")) { |
if (cmd.equals("lock") || cmd.equals("snitch")) { |
127 |
if (chest == null) { |
if (chest == null) { |
128 |
chest = createChest(player.getName(), "", loc); |
chest = createChest(player.getName(), "", loc); |
147 |
} |
} |
148 |
return true; |
return true; |
149 |
} |
} |
150 |
|
|
151 |
if (cmd.equals("remove")) { |
if (cmd.equals("remove")) { |
152 |
if (chest == null) { |
if (chest == null) { |
153 |
player.sendMessage("This chest is not protected"); |
player.sendMessage("This chest is not protected"); |
154 |
return true; |
return true; |
155 |
} |
} |
156 |
if (! chest.getOwner().equals( player.getName() ) ) { |
if ( chest.getOwner().equals( player.getName() ) || player.isOp() ) { |
157 |
|
//do nothing |
158 |
|
} else { |
159 |
player.sendMessage("You can not remove lock from a chest you don't own"); |
player.sendMessage("You can not remove lock from a chest you don't own"); |
160 |
server.getLogger().info( player.getName() + " tried to remove protection froma chest owned by " + chest.getOwner() ); |
server.getLogger().info( player.getName() + " tried to remove protection froma chest owned by " + chest.getOwner() ); |
161 |
return true; |
return true; |
172 |
player.sendMessage("This chest is not protected"); |
player.sendMessage("This chest is not protected"); |
173 |
return true; |
return true; |
174 |
} |
} |
175 |
if (! chest.getOwner().equals( player.getName() ) ) { |
if ( chest.getOwner().equals( player.getName() ) || player.isOp() ) { |
176 |
|
//do nothing |
177 |
|
} else { |
178 |
player.sendMessage("You can not set new owner of a chest you dont own"); |
player.sendMessage("You can not set new owner of a chest you dont own"); |
179 |
server.getLogger().info( player.getName() + " tried to set owner on a chest owned by " + chest.getOwner() ); |
server.getLogger().info( player.getName() + " tried to set owner on a chest owned by " + chest.getOwner() ); |
180 |
return true; |
return true; |
183 |
player.sendMessage("You need to specify which player should own this chest"); |
player.sendMessage("You need to specify which player should own this chest"); |
184 |
return true; |
return true; |
185 |
} |
} |
186 |
|
|
187 |
|
//@SuppressWarnings("deprecation")//user by name is our only option here |
188 |
OfflinePlayer p2 = server.getOfflinePlayer(args[1]); |
OfflinePlayer p2 = server.getOfflinePlayer(args[1]); |
189 |
|
|
190 |
if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { |
if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { |
191 |
player.sendMessage("Unknown user: " + args[1] ); |
player.sendMessage("Unknown user: " + args[1] ); |
192 |
return true; |
return true; |
193 |
} |
} |
194 |
chest.setOwner( p2.getName() ); |
chest.setOwner( p2.getName() ); |
195 |
plugin.getDatabase().save( chest ); |
Ebean.getDefaultServer().save( chest ); |
196 |
player.sendMessage("ok"); |
player.sendMessage("ok"); |
197 |
return true; |
return true; |
198 |
} |
} |
199 |
|
|
200 |
if (cmd.equals("addplayer") || cmd.equals("removeplayer")) { |
if (cmd.equals("addplayer") || cmd.equals("removeplayer")) { |
201 |
if (chest == null) { |
if (chest == null) { |
202 |
player.sendMessage("This chest is not protected"); |
player.sendMessage("This chest is not protected"); |
211 |
player.sendMessage("You need to specify which player to add or remove"); |
player.sendMessage("You need to specify which player to add or remove"); |
212 |
return true; |
return true; |
213 |
} |
} |
214 |
|
|
215 |
|
//@SuppressWarnings("deprecation")//user by name is our only option here |
216 |
OfflinePlayer p2 = server.getOfflinePlayer(args[1]); |
OfflinePlayer p2 = server.getOfflinePlayer(args[1]); |
217 |
if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { |
if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { |
218 |
player.sendMessage("Unknown user: " + args[1] ); |
player.sendMessage("Unknown user: " + args[1] ); |
219 |
return true; |
return true; |
220 |
} |
} |
221 |
|
|
222 |
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
223 |
if (cmd.equals("addplayer")) { |
if (cmd.equals("addplayer")) { |
224 |
players.add(p2.getName()); |
players.add(p2.getName()); |
225 |
} else { |
} else { |
226 |
players.remove(p2.getName()); |
players.remove(p2.getName()); |
227 |
} |
} |
228 |
|
|
229 |
chest.setModifyPlayers( Util.setToString(players) ); |
chest.setModifyPlayers( Util.setToString(players) ); |
230 |
plugin.getDatabase().save( chest ); |
Ebean.getDefaultServer().save( chest ); |
231 |
player.sendMessage("ok"); |
player.sendMessage("ok"); |
232 |
return true; |
return true; |
233 |
} |
} |
249 |
String comment = sb.toString().trim(); |
String comment = sb.toString().trim(); |
250 |
|
|
251 |
chest.setComment( comment ); |
chest.setComment( comment ); |
252 |
plugin.getDatabase().save( chest ); |
Ebean.getDefaultServer().save( chest ); |
253 |
player.sendMessage("Comment set"); |
player.sendMessage("Comment set"); |
254 |
|
|
255 |
return true; |
return true; |
256 |
} |
} |
257 |
|
|
258 |
/* |
/* |
259 |
if (chest != null) { |
if (chest != null) { |
260 |
if (chest.getOwner().equals(player.getName())) { |
if (chest.getOwner().equals(player.getName())) { |
263 |
} else { |
} else { |
264 |
player.sendMessage("[LockedChest] Chest is already protected"); |
player.sendMessage("[LockedChest] Chest is already protected"); |
265 |
} |
} |
266 |
|
|
267 |
return true; |
return true; |
268 |
} |
} |
269 |
|
|
270 |
chest = createChest(player.getName(), "", loc); |
chest = createChest(player.getName(), "", loc); |
271 |
if (loc2 != null) { |
if (loc2 != null) { |
272 |
chest.setDoublechest(true); |
chest.setDoublechest(true); |
273 |
} |
} |
274 |
|
|
275 |
addChest(loc, chest); |
addChest(loc, chest); |
276 |
|
|
277 |
|
|
278 |
player.sendMessage("[LockedChest] Chest is now locked"); |
player.sendMessage("[LockedChest] Chest is now locked"); |
279 |
*/ |
*/ |
280 |
|
|
281 |
player.sendMessage("Unknown argument, " + cmd); |
player.sendMessage("Unknown argument, " + cmd); |
282 |
|
|
283 |
return true; |
return true; |
284 |
} |
} |
285 |
|
|
286 |
@EventHandler |
@EventHandler |
287 |
public void onBlockBreak(BlockBreakEvent event) { |
public void onBlockBreak(BlockBreakEvent event) { |
288 |
Location loc = event.getBlock().getLocation(); |
Location loc = event.getBlock().getLocation(); |
304 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
305 |
chestMap.put(loc2, chest); |
chestMap.put(loc2, chest); |
306 |
} |
} |
307 |
plugin.getDatabase().save(chest); |
Ebean.getDefaultServer().save(chest); |
308 |
|
|
309 |
reloadChests(); |
reloadChests(); |
310 |
|
|
311 |
} |
} |
312 |
|
|
313 |
void removeChest(Location loc) { |
void removeChest(Location loc) { |
314 |
ChestBean chest = chestMap.remove(loc); |
ChestBean chest = chestMap.remove(loc); |
315 |
if (chest != null) { |
if (chest != null) { |
317 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
318 |
chestMap.remove(loc2); |
chestMap.remove(loc2); |
319 |
} |
} |
320 |
plugin.getDatabase().delete(chest); |
Ebean.getDefaultServer().delete(chest); |
321 |
} |
} |
322 |
} |
} |
323 |
|
|
324 |
int loadChestsWorker() { |
int loadChestsWorker() { |
325 |
List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList(); |
List<ChestBean> chestlist = Ebean.getDefaultServer().find( ChestBean.class).findList(); |
326 |
for (ChestBean chest : chestlist) { |
for (ChestBean chest : chestlist) { |
327 |
Location loc = getChestLocation(server, chest); |
Location loc = getChestLocation(server, chest); |
328 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
329 |
|
|
330 |
if (chest.isDoublechest()) { |
if (chest.isDoublechest()) { |
331 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
332 |
chestMap.put(loc2, chest); |
chestMap.put(loc2, chest); |
335 |
|
|
336 |
return chestlist.size(); |
return chestlist.size(); |
337 |
} |
} |
338 |
|
|
339 |
void reloadChests() { |
void reloadChests() { |
340 |
chestMap.clear(); |
chestMap.clear(); |
341 |
loadChestsWorker(); |
loadChestsWorker(); |
342 |
} |
} |
343 |
|
|
344 |
void loadChests() { |
void loadChests() { |
345 |
int count = loadChestsWorker(); |
int count = loadChestsWorker(); |
346 |
server.getLogger().info("[AdvancedChest] loaded " + count + " chests"); |
server.getLogger().info("[AdvancedChest] loaded " + count + " chests"); |
347 |
} |
} |
348 |
|
|
349 |
|
|
350 |
public ChestBean createChest(String owner, String description, Location loc) { |
public ChestBean createChest(String owner, String description, Location loc) { |
351 |
|
|
352 |
ChestBean chest = new ChestBean(); |
ChestBean chest = new ChestBean(); |
353 |
chest.setOwner(owner); |
chest.setOwner(owner); |
354 |
chest.setDescription(description); |
chest.setDescription(description); |
356 |
|
|
357 |
return chest; |
return chest; |
358 |
} |
} |
359 |
|
|
360 |
|
|
361 |
public void setChestLocation(ChestBean chest, Location loc) { |
public void setChestLocation(ChestBean chest, Location loc) { |
362 |
chest.setWorld( loc.getWorld().getName() ); |
chest.setWorld( loc.getWorld().getName() ); |
363 |
chest.setX( loc.getBlockX() ); |
chest.setX( loc.getBlockX() ); |
364 |
chest.setY( loc.getBlockY() ); |
chest.setY( loc.getBlockY() ); |
365 |
chest.setZ( loc.getBlockZ() ); |
chest.setZ( loc.getBlockZ() ); |
366 |
} |
} |
367 |
|
|
368 |
public Location getChestLocation(Server server, ChestBean chest) { |
public Location getChestLocation(Server server, ChestBean chest) { |
369 |
World wrld = server.getWorld(chest.getWorld()); |
World wrld = server.getWorld(chest.getWorld()); |
370 |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
371 |
} |
} |
372 |
|
|
373 |
|
|
374 |
/* |
/* |
375 |
void saveChests() { |
void saveChests() { |
376 |
|
|
377 |
}*/ |
}*/ |
378 |
|
|
379 |
Location getNeighborChest(Location loc) { |
Location getNeighborChest(Location loc) { |
380 |
World world = loc.getWorld(); |
World world = loc.getWorld(); |
381 |
|
|
382 |
Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() ); |
Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() ); |
383 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
384 |
return target; |
return target; |
385 |
|
|
386 |
target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() ); |
target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() ); |
387 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
388 |
return target; |
return target; |
394 |
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1); |
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1); |
395 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
396 |
return target; |
return target; |
397 |
|
|
398 |
return null; |
return null; |
399 |
} |
} |
400 |
|
|
401 |
|
|
402 |
Location getChestLocation(InventoryHolder holder) { |
Location getChestLocation(InventoryHolder holder) { |
403 |
Location loc; |
Location loc; |
404 |
if ( holder instanceof Chest) { |
if ( holder instanceof Chest) { |
406 |
} else { |
} else { |
407 |
loc = ( (DoubleChest)holder).getLocation(); |
loc = ( (DoubleChest)holder).getLocation(); |
408 |
} |
} |
409 |
|
|
410 |
loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5 |
loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5 |
411 |
loc.setZ( loc.getBlockZ() ); // -- // -- |
loc.setZ( loc.getBlockZ() ); // -- // -- |
412 |
|
|
413 |
return loc; |
return loc; |
414 |
} |
} |
415 |
|
|
416 |
@EventHandler |
@EventHandler |
417 |
public void onChestPlaced(BlockPlaceEvent event) { |
public void onChestPlaced(BlockPlaceEvent event) { |
418 |
Block block = event.getBlock(); |
Block block = event.getBlock(); |
419 |
|
|
420 |
if (block.getType() != Material.CHEST) { |
if (block.getType() != Material.CHEST) { |
421 |
return; |
return; |
422 |
} |
} |
423 |
|
|
424 |
Location chestloc = getNeighborChest( block.getLocation() ); |
Location chestloc = getNeighborChest( block.getLocation() ); |
425 |
if (chestloc != null) { |
if (chestloc == null) |
426 |
ChestBean chest = chestMap.get(chestloc); |
return; |
|
|
|
|
if (chest != null) { //the neighbor is a locked chest |
|
427 |
|
|
428 |
|
ChestBean chest = chestMap.get(chestloc); |
429 |
chest.setDoublechest(true); |
|
430 |
addChest(chestloc, chest); |
if (chest == null)//the neighbor is not a locked chest |
431 |
|
return; |
432 |
|
|
433 |
event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); |
|
434 |
} |
chest.setDoublechest(true); |
435 |
|
addChest(chestloc, chest); |
436 |
} |
|
437 |
|
|
438 |
|
event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); |
439 |
} |
} |
440 |
|
|
441 |
|
|
459 |
event.setCancelled( true); |
event.setCancelled( true); |
460 |
} |
} |
461 |
} |
} |
462 |
|
|
463 |
// prevent a user from opening a chest |
// prevent a user from opening a chest |
464 |
@EventHandler |
@EventHandler |
465 |
public void onChestInteract(PlayerInteractEvent event) { |
public void onChestInteract(PlayerInteractEvent event) { |
466 |
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { |
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { |
467 |
Block b = event.getClickedBlock(); |
Block b = event.getClickedBlock(); |
468 |
|
|
469 |
if (b.getType() == Material.CHEST) { |
if (b.getType() == Material.CHEST) { |
470 |
|
|
471 |
Location loc = b.getLocation(); |
Location loc = b.getLocation(); |
472 |
|
|
473 |
ChestBean chest = chestMap.get( loc ); |
ChestBean chest = chestMap.get( loc ); |
474 |
if (chest == null) { |
if (chest == null) { |
475 |
return; //chest not surveyed by this plugin |
return; //chest not surveyed by this plugin |
476 |
} |
} |
477 |
|
|
478 |
if (chest.getChestType() != ChestBean.LOCKED ) { |
if (chest.getChestType() != ChestBean.LOCKED ) { |
479 |
return; //this is not a locked chests |
return; //this is not a locked chests |
480 |
} |
} |
481 |
|
|
482 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
483 |
if (player.getName().equals(chest.getOwner() )) { |
if (player.getName().equals(chest.getOwner() )) { |
484 |
return; //chest is opened by it's owner |
return; //chest is opened by it's owner |
485 |
} |
} |
486 |
|
|
487 |
Set<String> players = chest.getModifyPlayersSet() ; |
Set<String> players = chest.getModifyPlayersSet() ; |
488 |
if ( players.contains(player.getName()) ) { |
if ( players.contains(player.getName()) ) { |
489 |
return; //this player is on the whitelist so he may open |
return; //this player is on the whitelist so he may open |
499 |
|
|
500 |
@EventHandler |
@EventHandler |
501 |
public void onChestOpen(InventoryOpenEvent event) { |
public void onChestOpen(InventoryOpenEvent event) { |
502 |
|
|
503 |
if (! (event.getPlayer() instanceof Player)) { |
if (! (event.getPlayer() instanceof Player)) { |
504 |
return; |
return; |
505 |
} |
} |
506 |
|
|
507 |
|
|
508 |
|
|
509 |
InventoryHolder holder = event.getInventory().getHolder(); |
InventoryHolder holder = event.getInventory().getHolder(); |
510 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
511 |
Location loc = getChestLocation(holder); |
Location loc = getChestLocation(holder); |
512 |
|
|
513 |
ChestBean chest = chestMap.get( loc ); |
ChestBean chest = chestMap.get( loc ); |
514 |
if (chest == null) { |
if (chest == null) { |
515 |
return; //chest not surveyed by this plugin |
return; //chest not surveyed by this plugin |
516 |
} |
} |
517 |
|
|
518 |
if (chest.getChestType() != ChestBean.SNITCHING) { |
if (chest.getChestType() != ChestBean.SNITCHING) { |
519 |
return; // not a snitching chest |
return; // not a snitching chest |
520 |
} |
} |
521 |
|
|
522 |
|
|
523 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
524 |
if (player.getName().equals(chest.getOwner() )) { |
if (player.getName().equals(chest.getOwner() )) { |
525 |
return; //chest is owned by it's own player |
return; //chest is owned by it's own player |
526 |
} |
} |
527 |
|
|
528 |
Set<String> players = chest.getModifyPlayersSet(); |
Set<String> players = chest.getModifyPlayersSet(); |
529 |
if ( players.contains(player.getName()) ) { |
if ( players.contains(player.getName()) ) { |
530 |
return; //this player is on the whitelist so he may open |
return; //this player is on the whitelist so he may open |
531 |
} |
} |
532 |
|
|
533 |
|
|
534 |
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " opened a snitching chest owned by " + chest.getOwner() + chest.getCommentString() ); |
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " opened a snitching chest owned by " + chest.getOwner() + chest.getCommentString() ); |
535 |
|
|
536 |
ItemCount contents = countItems( event.getInventory().getContents() ); |
ItemCount contents = countItems( event.getInventory().getContents() ); |
537 |
|
|
538 |
contentMap.put(player.getName(), contents ); |
contentMap.put(player.getName(), contents ); |
539 |
} |
} |
540 |
} |
} |
541 |
|
|
542 |
@EventHandler |
@EventHandler |
543 |
public void onChestClose(InventoryCloseEvent event) { |
public void onChestClose(InventoryCloseEvent event) { |
544 |
if (! (event.getPlayer() instanceof Player)) { |
if (! (event.getPlayer() instanceof Player)) { |
545 |
return; |
return; |
546 |
} |
} |
547 |
|
|
548 |
|
|
549 |
InventoryHolder holder = event.getInventory().getHolder(); |
InventoryHolder holder = event.getInventory().getHolder(); |
550 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
551 |
Location loc = getChestLocation(holder); |
Location loc = getChestLocation(holder); |
552 |
ChestBean chest = chestMap.get(loc); |
ChestBean chest = chestMap.get(loc); |
553 |
|
|
554 |
if (chest == null) { //chest was not a snitching chest |
if (chest == null) { //chest was not a snitching chest |
555 |
return; |
return; |
556 |
} |
} |
557 |
|
|
558 |
if (chest.getChestType() != ChestBean.SNITCHING) { |
if (chest.getChestType() != ChestBean.SNITCHING) { |
559 |
return; // not a snitching chest |
return; // not a snitching chest |
560 |
} |
} |
561 |
|
|
562 |
|
|
563 |
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
564 |
|
|
565 |
|
|
566 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
567 |
|
|
568 |
ItemCount savedContent = contentMap.get( player.getName() ); |
ItemCount savedContent = contentMap.get( player.getName() ); |
569 |
|
|
570 |
if (savedContent == null) { |
if (savedContent == null) { |
571 |
return; |
return; |
572 |
} |
} |
573 |
|
|
574 |
contentMap.remove( player.getName() ); |
contentMap.remove( player.getName() ); |
575 |
|
|
576 |
ItemCount content = countItems( event.getInventory().getContents() ); |
ItemCount content = countItems( event.getInventory().getContents() ); |
577 |
|
|
578 |
Set<Integer> combinedKeyset = new TreeSet<Integer>(); |
Set<Material> combinedKeyset = new TreeSet<Material>(); |
579 |
combinedKeyset.addAll( savedContent.keySet() ); |
combinedKeyset.addAll( savedContent.keySet() ); |
580 |
combinedKeyset.addAll( content.keySet() ); |
combinedKeyset.addAll( content.keySet() ); |
581 |
|
|
582 |
for (Integer item : combinedKeyset ) { |
for (Material material : combinedKeyset ) { |
583 |
Integer savedcount = savedContent.get(item); |
Integer savedcount = savedContent.get(material); |
584 |
Integer count = content.get(item); |
Integer count = content.get(material); |
585 |
|
|
586 |
if (savedcount == null) |
if (savedcount == null) |
587 |
savedcount = 0; |
savedcount = 0; |
588 |
if (count == null) |
if (count == null) |
589 |
count = 0; |
count = 0; |
590 |
|
|
591 |
|
|
592 |
int diff = Math.abs( savedcount - count); |
int diff = Math.abs( savedcount - count); |
593 |
|
|
594 |
if (diff > 0) { |
if (diff > 0) { |
|
String material = Material.getMaterial(item).name(); |
|
595 |
String msg = null; |
String msg = null; |
596 |
|
|
597 |
if (count > savedcount) { |
if (count > savedcount) { |
598 |
msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
msg = player.getName() + " added " + diff + " units of " + material + " to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ() + chest.getCommentString(); |
599 |
} else { //(count < savedcount) |
} else { //(count < savedcount) |
600 |
msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
msg = player.getName() + " removed " + diff + " units of " + material + " from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ() + chest.getCommentString(); |
601 |
} |
} |
602 |
|
|
603 |
|
|
604 |
server.getLogger().info( "[AdvancedChest]" + msg); |
server.getLogger().info( "[AdvancedChest]" + msg); |
605 |
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
606 |
} |
} |
607 |
|
|
608 |
} |
} |
609 |
|
|
610 |
|
|
611 |
} |
} |
612 |
} |
} |
613 |
|
|
614 |
ItemCount countItems(ItemStack[] input) { |
ItemCount countItems(ItemStack[] input) { |
615 |
ItemCount output = new ItemCount(); |
ItemCount output = new ItemCount(); |
616 |
for (int i=0; i<input.length; i++) { |
for (int i=0; i<input.length; i++) { |
617 |
ItemStack current = input[i]; |
ItemStack current = input[i]; |
618 |
if (current == null) |
if (current == null) |
619 |
continue; |
continue; |
620 |
|
|
621 |
int type = current.getTypeId(); |
Material type = current.getType(); |
622 |
|
|
623 |
Integer amount = output.get(type); |
Integer amount = output.get(type); |
624 |
if (amount == null) |
if (amount == null) |
625 |
amount = 0; |
amount = 0; |
626 |
|
|
627 |
output.put(type, amount + current.getAmount() ); |
output.put(type, amount + current.getAmount() ); |
628 |
} |
} |
629 |
return output; |
return output; |
640 |
} |
} |
641 |
return output; |
return output; |
642 |
}*/ |
}*/ |
643 |
|
|
644 |
|
|
645 |
} |
} |