24 |
import org.bukkit.event.Listener; |
import org.bukkit.event.Listener; |
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.BlockBurnEvent; |
28 |
import org.bukkit.event.block.BlockPlaceEvent; |
import org.bukkit.event.block.BlockPlaceEvent; |
29 |
import org.bukkit.event.entity.EntityExplodeEvent; |
import org.bukkit.event.entity.EntityExplodeEvent; |
30 |
import org.bukkit.event.inventory.InventoryCloseEvent; |
import org.bukkit.event.inventory.InventoryCloseEvent; |
43 |
class ItemCount extends TreeMap<Integer,Integer> { |
class ItemCount extends TreeMap<Integer,Integer> { |
44 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
45 |
}; |
}; |
46 |
|
|
47 |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
48 |
|
|
49 |
|
|
50 |
HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>(); |
HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>(); |
51 |
|
|
52 |
|
|
53 |
HoerupUtilsPlugin plugin; |
HoerupUtilsPlugin plugin; |
54 |
Server server; |
Server server; |
55 |
|
|
56 |
public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) { |
public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) { |
57 |
this.plugin = plugin; |
this.plugin = plugin; |
58 |
server = plugin.getServer(); |
server = plugin.getServer(); |
64 |
loadChests(); |
loadChests(); |
65 |
} |
} |
66 |
} |
} |
67 |
|
|
68 |
|
|
69 |
@Override |
@Override |
70 |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
72 |
sender.sendMessage("this is not a console command!"); |
sender.sendMessage("this is not a console command!"); |
73 |
return true; |
return true; |
74 |
} |
} |
75 |
|
|
76 |
Player player = (Player) sender; |
Player player = (Player) sender; |
77 |
|
|
78 |
if (args.length == 0) { |
if (args.length == 0) { |
79 |
player.sendMessage("Usage:"); |
player.sendMessage("Usage:"); |
80 |
player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer|setowner) [player]"); |
player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer|setowner|comment) [player]"); |
81 |
return true; |
return true; |
82 |
} |
} |
83 |
|
|
84 |
|
|
85 |
Block b = player.getTargetBlock(null, 30); |
Block b = player.getTargetBlock(null, 30); |
86 |
|
|
87 |
if (b.getTypeId() != 54) { |
if (b.getTypeId() != 54 && b.getTypeId() != 154) { |
88 |
player.sendMessage("[Chest] Please look at the chest you want to protect"); |
player.sendMessage("[Chest] Please look at the chest/hopper you want to protect"); |
89 |
return true; |
return true; |
90 |
} |
} |
91 |
|
|
92 |
Location loc = b.getLocation(); |
Location loc = b.getLocation(); |
93 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = null; |
94 |
|
if ( b.getTypeId() == 54) { //dont find neighbours for Hoppers |
95 |
|
loc2 = getNeighborChest(loc); |
96 |
|
} |
97 |
|
|
98 |
ChestBean chest = chestMap.get(loc); |
ChestBean chest = chestMap.get(loc); |
99 |
String cmd = args[0].toLowerCase(); |
String cmd = args[0].toLowerCase(); |
100 |
|
|
101 |
if (cmd.equals("status")) { |
if (cmd.equals("status")) { |
102 |
if (chest != null) { |
if (chest != null) { |
103 |
String mode = ""; |
String mode = ""; |
111 |
default: |
default: |
112 |
mode = "unknown ??"; |
mode = "unknown ??"; |
113 |
} |
} |
114 |
|
|
115 |
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()); |
116 |
player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() ); |
player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() ); |
117 |
|
player.sendMessage(ChatColor.GREEN + "Comment: " + chest.getComment() ); |
118 |
} else { |
} else { |
119 |
player.sendMessage(ChatColor.GREEN + "The chest is not protected"); |
player.sendMessage(ChatColor.GREEN + "The chest is not protected"); |
120 |
} |
} |
121 |
return true; |
return true; |
122 |
} |
} |
123 |
|
|
124 |
if (cmd.equals("lock") || cmd.equals("snitch")) { |
if (cmd.equals("lock") || cmd.equals("snitch")) { |
125 |
if (chest == null) { |
if (chest == null) { |
126 |
chest = createChest(player.getName(), "", loc); |
chest = createChest(player.getName(), "", loc); |
136 |
modeStr = "snitching"; |
modeStr = "snitching"; |
137 |
} |
} |
138 |
chest.setModifyPlayers(""); |
chest.setModifyPlayers(""); |
139 |
|
chest.setComment(""); |
140 |
addChest(loc, chest); |
addChest(loc, chest); |
141 |
player.sendMessage("Chest is now " + modeStr); |
player.sendMessage("Chest is now " + modeStr); |
142 |
} else { |
} else { |
143 |
|
server.getLogger().info( player.getName() + " tried to protect a chest owned by " + chest.getOwner() ); |
144 |
player.sendMessage("This chest is already protected"); |
player.sendMessage("This chest is already protected"); |
145 |
} |
} |
146 |
return true; |
return true; |
147 |
} |
} |
148 |
|
|
149 |
if (cmd.equals("remove")) { |
if (cmd.equals("remove")) { |
150 |
if (chest == null) { |
if (chest == null) { |
151 |
player.sendMessage("This chest is not protected"); |
player.sendMessage("This chest is not protected"); |
152 |
return true; |
return true; |
153 |
} |
} |
154 |
if (! chest.getOwner().equals( player.getName() ) ) { |
if ( chest.getOwner().equals( player.getName() ) || player.isOp() ) { |
155 |
|
//do nothing |
156 |
|
} else { |
157 |
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"); |
158 |
|
server.getLogger().info( player.getName() + " tried to remove protection froma chest owned by " + chest.getOwner() ); |
159 |
return true; |
return true; |
160 |
} |
} |
161 |
|
|
170 |
player.sendMessage("This chest is not protected"); |
player.sendMessage("This chest is not protected"); |
171 |
return true; |
return true; |
172 |
} |
} |
173 |
if (! chest.getOwner().equals( player.getName() ) ) { |
if ( chest.getOwner().equals( player.getName() ) || player.isOp() ) { |
174 |
|
//do nothing |
175 |
|
} else { |
176 |
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"); |
177 |
|
server.getLogger().info( player.getName() + " tried to set owner on a chest owned by " + chest.getOwner() ); |
178 |
return true; |
return true; |
179 |
} |
} |
180 |
if (args.length != 2) { |
if (args.length != 2) { |
191 |
player.sendMessage("ok"); |
player.sendMessage("ok"); |
192 |
return true; |
return true; |
193 |
} |
} |
194 |
|
|
195 |
if (cmd.equals("addplayer") || cmd.equals("removeplayer")) { |
if (cmd.equals("addplayer") || cmd.equals("removeplayer")) { |
196 |
if (chest == null) { |
if (chest == null) { |
197 |
player.sendMessage("This chest is not protected"); |
player.sendMessage("This chest is not protected"); |
199 |
} |
} |
200 |
if (! chest.getOwner().equals( player.getName() ) ) { |
if (! chest.getOwner().equals( player.getName() ) ) { |
201 |
player.sendMessage("You can not add/remove players from a chest you don't own"); |
player.sendMessage("You can not add/remove players from a chest you don't own"); |
202 |
|
server.getLogger().info( player.getName() + " tried to add/remove player on a chest owned by " + chest.getOwner() ); |
203 |
return true; |
return true; |
204 |
} |
} |
205 |
if (args.length != 2) { |
if (args.length != 2) { |
211 |
player.sendMessage("Unknown user: " + args[1] ); |
player.sendMessage("Unknown user: " + args[1] ); |
212 |
return true; |
return true; |
213 |
} |
} |
214 |
|
|
215 |
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
216 |
if (cmd.equals("addplayer")) { |
if (cmd.equals("addplayer")) { |
217 |
players.add(p2.getName()); |
players.add(p2.getName()); |
218 |
} else { |
} else { |
219 |
players.remove(p2.getName()); |
players.remove(p2.getName()); |
220 |
} |
} |
221 |
|
|
222 |
chest.setModifyPlayers( Util.setToString(players) ); |
chest.setModifyPlayers( Util.setToString(players) ); |
223 |
plugin.getDatabase().save( chest ); |
plugin.getDatabase().save( chest ); |
224 |
player.sendMessage("ok"); |
player.sendMessage("ok"); |
225 |
return true; |
return true; |
226 |
} |
} |
227 |
|
if (cmd.equals("comment")) { |
228 |
|
if (chest == null) { |
229 |
|
player.sendMessage("This chest is not protected"); |
230 |
|
return true; |
231 |
|
} |
232 |
|
if (! chest.getOwner().equals( player.getName() ) ) { |
233 |
|
player.sendMessage("You can not comment a chest you don't own"); |
234 |
|
server.getLogger().info( player.getName() + " tried to comment on a chest owned by " + chest.getOwner() ); |
235 |
|
return true; |
236 |
|
} |
237 |
|
|
238 |
|
StringBuilder sb = new StringBuilder(); |
239 |
|
for (int i=1; i<args.length; i++) { |
240 |
|
sb.append(args[i]).append(" "); |
241 |
|
} |
242 |
|
String comment = sb.toString().trim(); |
243 |
|
|
244 |
|
chest.setComment( comment ); |
245 |
|
plugin.getDatabase().save( chest ); |
246 |
|
player.sendMessage("Comment set"); |
247 |
|
|
248 |
|
return true; |
249 |
|
} |
250 |
|
|
251 |
/* |
/* |
252 |
if (chest != null) { |
if (chest != null) { |
253 |
if (chest.getOwner().equals(player.getName())) { |
if (chest.getOwner().equals(player.getName())) { |
256 |
} else { |
} else { |
257 |
player.sendMessage("[LockedChest] Chest is already protected"); |
player.sendMessage("[LockedChest] Chest is already protected"); |
258 |
} |
} |
259 |
|
|
260 |
return true; |
return true; |
261 |
} |
} |
262 |
|
|
263 |
chest = createChest(player.getName(), "", loc); |
chest = createChest(player.getName(), "", loc); |
264 |
if (loc2 != null) { |
if (loc2 != null) { |
265 |
chest.setDoublechest(true); |
chest.setDoublechest(true); |
266 |
} |
} |
267 |
|
|
268 |
addChest(loc, chest); |
addChest(loc, chest); |
269 |
|
|
270 |
|
|
271 |
player.sendMessage("[LockedChest] Chest is now locked"); |
player.sendMessage("[LockedChest] Chest is now locked"); |
272 |
*/ |
*/ |
273 |
|
|
274 |
player.sendMessage("Unknown argument, " + cmd); |
player.sendMessage("Unknown argument, " + cmd); |
275 |
|
|
276 |
return true; |
return true; |
277 |
} |
} |
278 |
|
|
279 |
@EventHandler |
@EventHandler |
280 |
public void onBlockBreak(BlockBreakEvent event) { |
public void onBlockBreak(BlockBreakEvent event) { |
281 |
Location loc = event.getBlock().getLocation(); |
Location loc = event.getBlock().getLocation(); |
287 |
} else { |
} else { |
288 |
event.setCancelled(true); |
event.setCancelled(true); |
289 |
event.getPlayer().sendMessage("You can't destroy that chest"); |
event.getPlayer().sendMessage("You can't destroy that chest"); |
290 |
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried breaking a chest owned by " + chest.getOwner() ); |
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried breaking a chest owned by " + chest.getOwner() + chest.getCommentString() ); |
291 |
} |
} |
292 |
} |
} |
293 |
} |
} |
298 |
chestMap.put(loc2, chest); |
chestMap.put(loc2, chest); |
299 |
} |
} |
300 |
plugin.getDatabase().save(chest); |
plugin.getDatabase().save(chest); |
301 |
|
|
302 |
reloadChests(); |
reloadChests(); |
303 |
|
|
304 |
} |
} |
305 |
|
|
306 |
void removeChest(Location loc) { |
void removeChest(Location loc) { |
307 |
ChestBean chest = chestMap.remove(loc); |
ChestBean chest = chestMap.remove(loc); |
308 |
if (chest != null) { |
if (chest != null) { |
313 |
plugin.getDatabase().delete(chest); |
plugin.getDatabase().delete(chest); |
314 |
} |
} |
315 |
} |
} |
316 |
|
|
317 |
int loadChestsWorker() { |
int loadChestsWorker() { |
318 |
List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList(); |
List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList(); |
319 |
for (ChestBean chest : chestlist) { |
for (ChestBean chest : chestlist) { |
320 |
Location loc = getChestLocation(server, chest); |
Location loc = getChestLocation(server, chest); |
321 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
322 |
|
|
323 |
if (chest.isDoublechest()) { |
if (chest.isDoublechest()) { |
324 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
325 |
chestMap.put(loc2, chest); |
chestMap.put(loc2, chest); |
328 |
|
|
329 |
return chestlist.size(); |
return chestlist.size(); |
330 |
} |
} |
331 |
|
|
332 |
void reloadChests() { |
void reloadChests() { |
333 |
chestMap.clear(); |
chestMap.clear(); |
334 |
loadChestsWorker(); |
loadChestsWorker(); |
335 |
} |
} |
336 |
|
|
337 |
void loadChests() { |
void loadChests() { |
338 |
int count = loadChestsWorker(); |
int count = loadChestsWorker(); |
339 |
server.getLogger().info("[AdvancedChest] loaded " + count + " chests"); |
server.getLogger().info("[AdvancedChest] loaded " + count + " chests"); |
340 |
} |
} |
341 |
|
|
342 |
|
|
343 |
public ChestBean createChest(String owner, String description, Location loc) { |
public ChestBean createChest(String owner, String description, Location loc) { |
344 |
|
|
345 |
ChestBean chest = new ChestBean(); |
ChestBean chest = new ChestBean(); |
346 |
chest.setOwner(owner); |
chest.setOwner(owner); |
347 |
chest.setDescription(description); |
chest.setDescription(description); |
349 |
|
|
350 |
return chest; |
return chest; |
351 |
} |
} |
352 |
|
|
353 |
|
|
354 |
public void setChestLocation(ChestBean chest, Location loc) { |
public void setChestLocation(ChestBean chest, Location loc) { |
355 |
chest.setWorld( loc.getWorld().getName() ); |
chest.setWorld( loc.getWorld().getName() ); |
356 |
chest.setX( loc.getBlockX() ); |
chest.setX( loc.getBlockX() ); |
357 |
chest.setY( loc.getBlockY() ); |
chest.setY( loc.getBlockY() ); |
358 |
chest.setZ( loc.getBlockZ() ); |
chest.setZ( loc.getBlockZ() ); |
359 |
} |
} |
360 |
|
|
361 |
public Location getChestLocation(Server server, ChestBean chest) { |
public Location getChestLocation(Server server, ChestBean chest) { |
362 |
World wrld = server.getWorld(chest.getWorld()); |
World wrld = server.getWorld(chest.getWorld()); |
363 |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
364 |
} |
} |
365 |
|
|
366 |
|
|
367 |
/* |
/* |
368 |
void saveChests() { |
void saveChests() { |
369 |
|
|
370 |
}*/ |
}*/ |
371 |
|
|
372 |
Location getNeighborChest(Location loc) { |
Location getNeighborChest(Location loc) { |
373 |
World world = loc.getWorld(); |
World world = loc.getWorld(); |
374 |
|
|
375 |
Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() ); |
Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() ); |
376 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
377 |
return target; |
return target; |
378 |
|
|
379 |
target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() ); |
target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() ); |
380 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
381 |
return target; |
return target; |
387 |
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1); |
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1); |
388 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
389 |
return target; |
return target; |
390 |
|
|
391 |
return null; |
return null; |
392 |
} |
} |
393 |
|
|
394 |
|
|
395 |
Location getChestLocation(InventoryHolder holder) { |
Location getChestLocation(InventoryHolder holder) { |
396 |
Location loc; |
Location loc; |
397 |
if ( holder instanceof Chest) { |
if ( holder instanceof Chest) { |
399 |
} else { |
} else { |
400 |
loc = ( (DoubleChest)holder).getLocation(); |
loc = ( (DoubleChest)holder).getLocation(); |
401 |
} |
} |
402 |
|
|
403 |
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 |
404 |
loc.setZ( loc.getBlockZ() ); // -- // -- |
loc.setZ( loc.getBlockZ() ); // -- // -- |
405 |
|
|
406 |
return loc; |
return loc; |
407 |
} |
} |
408 |
|
|
409 |
@EventHandler |
@EventHandler |
410 |
public void onChestPlaced(BlockPlaceEvent event) { |
public void onChestPlaced(BlockPlaceEvent event) { |
411 |
Block block = event.getBlock(); |
Block block = event.getBlock(); |
412 |
|
|
413 |
if (block.getType() != Material.CHEST) { |
if (block.getType() != Material.CHEST) { |
414 |
return; |
return; |
415 |
} |
} |
416 |
|
|
417 |
Location chestloc = getNeighborChest( block.getLocation() ); |
Location chestloc = getNeighborChest( block.getLocation() ); |
418 |
if (chestloc != null) { |
if (chestloc == null) |
419 |
ChestBean chest = chestMap.get(chestloc); |
return; |
420 |
|
|
421 |
if (chest != null) { //the neighbor is a locked chest |
ChestBean chest = chestMap.get(chestloc); |
422 |
|
|
423 |
|
if (chest == null)//the neighbor is not a locked chest |
424 |
chest.setDoublechest(true); |
return; |
425 |
addChest(chestloc, chest); |
|
426 |
|
|
427 |
|
chest.setDoublechest(true); |
428 |
event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); |
addChest(chestloc, chest); |
429 |
} |
|
430 |
|
|
431 |
} |
event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); |
432 |
} |
} |
433 |
|
|
434 |
|
|
437 |
for (Block b : event.blockList() ) { |
for (Block b : event.blockList() ) { |
438 |
ChestBean chest = chestMap.get( b.getLocation() ); |
ChestBean chest = chestMap.get( b.getLocation() ); |
439 |
if (chest != null) { |
if (chest != null) { |
440 |
server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() ); |
server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() + chest.getCommentString() ); |
441 |
event.setCancelled( true ); |
event.setCancelled( true ); |
442 |
return; |
return; |
443 |
} |
} |
444 |
} |
} |
445 |
} |
} |
446 |
|
|
447 |
|
@EventHandler |
448 |
|
public void onChestBurn(BlockBurnEvent event) { |
449 |
|
ChestBean chest = chestMap.get( event.getBlock().getLocation() ); |
450 |
|
if (chest != null) { |
451 |
|
server.getLogger().info( "[AdvancedChest] prevented a fire from destrying chest owned by " + chest.getOwner() + chest.getCommentString() ); |
452 |
|
event.setCancelled( true); |
453 |
|
} |
454 |
|
} |
455 |
|
|
456 |
// prevent a user from opening a chest |
// prevent a user from opening a chest |
457 |
@EventHandler |
@EventHandler |
458 |
public void onChestInteract(PlayerInteractEvent event) { |
public void onChestInteract(PlayerInteractEvent event) { |
459 |
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { |
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { |
460 |
Block b = event.getClickedBlock(); |
Block b = event.getClickedBlock(); |
461 |
|
|
462 |
if (b.getType() == Material.CHEST) { |
if (b.getType() == Material.CHEST) { |
463 |
|
|
464 |
Location loc = b.getLocation(); |
Location loc = b.getLocation(); |
465 |
|
|
466 |
ChestBean chest = chestMap.get( loc ); |
ChestBean chest = chestMap.get( loc ); |
467 |
if (chest == null) { |
if (chest == null) { |
468 |
return; //chest not surveyed by this plugin |
return; //chest not surveyed by this plugin |
469 |
} |
} |
470 |
|
|
471 |
if (chest.getChestType() != ChestBean.LOCKED ) { |
if (chest.getChestType() != ChestBean.LOCKED ) { |
472 |
return; //this is not a locked chests |
return; //this is not a locked chests |
473 |
} |
} |
474 |
|
|
475 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
476 |
if (player.getName().equals(chest.getOwner() )) { |
if (player.getName().equals(chest.getOwner() )) { |
477 |
return; //chest is opened by it's owner |
return; //chest is opened by it's owner |
478 |
} |
} |
479 |
|
|
480 |
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
Set<String> players = chest.getModifyPlayersSet() ; |
481 |
if ( players.contains(player.getName()) ) { |
if ( players.contains(player.getName()) ) { |
482 |
return; //this player is on the whitelist so he may open |
return; //this player is on the whitelist so he may open |
483 |
} |
} |
484 |
|
|
485 |
|
|
486 |
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner() ); |
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner() + chest.getCommentString() ); |
487 |
player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); |
player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); |
488 |
event.setCancelled(true); |
event.setCancelled(true); |
489 |
} |
} |
492 |
|
|
493 |
@EventHandler |
@EventHandler |
494 |
public void onChestOpen(InventoryOpenEvent event) { |
public void onChestOpen(InventoryOpenEvent event) { |
495 |
|
|
496 |
if (! (event.getPlayer() instanceof Player)) { |
if (! (event.getPlayer() instanceof Player)) { |
497 |
return; |
return; |
498 |
} |
} |
499 |
|
|
500 |
|
|
501 |
|
|
502 |
InventoryHolder holder = event.getInventory().getHolder(); |
InventoryHolder holder = event.getInventory().getHolder(); |
503 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
504 |
Location loc = getChestLocation(holder); |
Location loc = getChestLocation(holder); |
505 |
|
|
506 |
ChestBean chest = chestMap.get( loc ); |
ChestBean chest = chestMap.get( loc ); |
507 |
if (chest == null) { |
if (chest == null) { |
508 |
return; //chest not surveyed by this plugin |
return; //chest not surveyed by this plugin |
509 |
} |
} |
510 |
|
|
511 |
if (chest.getChestType() != ChestBean.SNITCHING) { |
if (chest.getChestType() != ChestBean.SNITCHING) { |
512 |
return; // not a snitching chest |
return; // not a snitching chest |
513 |
} |
} |
514 |
|
|
515 |
|
|
516 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
517 |
if (player.getName().equals(chest.getOwner() )) { |
if (player.getName().equals(chest.getOwner() )) { |
518 |
return; //chest is owned by it's own player |
return; //chest is owned by it's own player |
519 |
} |
} |
520 |
|
|
521 |
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
Set<String> players = chest.getModifyPlayersSet(); |
522 |
if ( players.contains(player.getName()) ) { |
if ( players.contains(player.getName()) ) { |
523 |
return; //this player is on the whitelist so he may open |
return; //this player is on the whitelist so he may open |
524 |
} |
} |
525 |
|
|
526 |
|
|
527 |
|
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " opened a snitching chest owned by " + chest.getOwner() + chest.getCommentString() ); |
528 |
|
|
529 |
ItemCount contents = countItems( event.getInventory().getContents() ); |
ItemCount contents = countItems( event.getInventory().getContents() ); |
530 |
|
|
531 |
contentMap.put(player.getName(), contents ); |
contentMap.put(player.getName(), contents ); |
532 |
} |
} |
533 |
} |
} |
534 |
|
|
535 |
@EventHandler |
@EventHandler |
536 |
public void onChestClose(InventoryCloseEvent event) { |
public void onChestClose(InventoryCloseEvent event) { |
537 |
if (! (event.getPlayer() instanceof Player)) { |
if (! (event.getPlayer() instanceof Player)) { |
538 |
return; |
return; |
539 |
} |
} |
540 |
|
|
541 |
|
|
542 |
InventoryHolder holder = event.getInventory().getHolder(); |
InventoryHolder holder = event.getInventory().getHolder(); |
543 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
544 |
Location loc = getChestLocation(holder); |
Location loc = getChestLocation(holder); |
545 |
ChestBean chest = chestMap.get(loc); |
ChestBean chest = chestMap.get(loc); |
546 |
|
|
547 |
if (chest == null) { //chest was not a snitching chest |
if (chest == null) { //chest was not a snitching chest |
548 |
return; |
return; |
549 |
} |
} |
550 |
|
|
551 |
if (chest.getChestType() != ChestBean.SNITCHING) { |
if (chest.getChestType() != ChestBean.SNITCHING) { |
552 |
return; // not a snitching chest |
return; // not a snitching chest |
553 |
} |
} |
554 |
|
|
555 |
|
|
556 |
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
557 |
|
|
558 |
|
|
559 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
560 |
|
|
561 |
ItemCount savedContent = contentMap.get( player.getName() ); |
ItemCount savedContent = contentMap.get( player.getName() ); |
562 |
|
|
563 |
if (savedContent == null) { |
if (savedContent == null) { |
564 |
return; |
return; |
565 |
} |
} |
566 |
|
|
567 |
contentMap.remove( player.getName() ); |
contentMap.remove( player.getName() ); |
568 |
|
|
569 |
ItemCount content = countItems( event.getInventory().getContents() ); |
ItemCount content = countItems( event.getInventory().getContents() ); |
570 |
|
|
571 |
Set<Integer> combinedKeyset = new TreeSet<Integer>(); |
Set<Integer> combinedKeyset = new TreeSet<Integer>(); |
572 |
combinedKeyset.addAll( savedContent.keySet() ); |
combinedKeyset.addAll( savedContent.keySet() ); |
573 |
combinedKeyset.addAll( content.keySet() ); |
combinedKeyset.addAll( content.keySet() ); |
574 |
|
|
575 |
for (Integer item : combinedKeyset ) { |
for (Integer item : combinedKeyset ) { |
576 |
Integer savedcount = savedContent.get(item); |
Integer savedcount = savedContent.get(item); |
577 |
Integer count = content.get(item); |
Integer count = content.get(item); |
578 |
|
|
579 |
if (savedcount == null) |
if (savedcount == null) |
580 |
savedcount = 0; |
savedcount = 0; |
581 |
if (count == null) |
if (count == null) |
582 |
count = 0; |
count = 0; |
583 |
|
|
584 |
|
|
585 |
int diff = Math.abs( savedcount - count); |
int diff = Math.abs( savedcount - count); |
586 |
|
|
587 |
if (diff > 0) { |
if (diff > 0) { |
588 |
String material = Material.getMaterial(item).name(); |
String material = Material.getMaterial(item).name(); |
589 |
String msg = null; |
String msg = null; |
590 |
|
|
591 |
if (count > savedcount) { |
if (count > savedcount) { |
592 |
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 + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ() + chest.getCommentString(); |
593 |
} else { //(count < savedcount) |
} else { //(count < savedcount) |
594 |
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 + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ() + chest.getCommentString(); |
595 |
} |
} |
596 |
|
|
597 |
|
|
598 |
server.getLogger().info( "[AdvancedChest]" + msg); |
server.getLogger().info( "[AdvancedChest]" + msg); |
599 |
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
600 |
} |
} |
601 |
|
|
602 |
} |
} |
603 |
|
|
604 |
|
|
605 |
} |
} |
606 |
} |
} |
607 |
|
|
608 |
ItemCount countItems(ItemStack[] input) { |
ItemCount countItems(ItemStack[] input) { |
609 |
ItemCount output = new ItemCount(); |
ItemCount output = new ItemCount(); |
610 |
for (int i=0; i<input.length; i++) { |
for (int i=0; i<input.length; i++) { |
611 |
ItemStack current = input[i]; |
ItemStack current = input[i]; |
612 |
if (current == null) |
if (current == null) |
613 |
continue; |
continue; |
614 |
|
|
615 |
int type = current.getTypeId(); |
int type = current.getTypeId(); |
616 |
|
|
617 |
Integer amount = output.get(type); |
Integer amount = output.get(type); |
618 |
if (amount == null) |
if (amount == null) |
619 |
amount = 0; |
amount = 0; |
620 |
|
|
621 |
output.put(type, amount + current.getAmount() ); |
output.put(type, amount + current.getAmount() ); |
622 |
} |
} |
623 |
return output; |
return output; |
634 |
} |
} |
635 |
return output; |
return output; |
636 |
}*/ |
}*/ |
637 |
|
|
638 |
|
|
639 |
} |
} |