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