1 |
torben |
1805 |
package dk.thoerup.bukkit.hoeruputils.chests; |
2 |
|
|
|
3 |
|
|
|
4 |
|
|
import java.util.HashMap; |
5 |
|
|
import java.util.List; |
6 |
torben |
1807 |
import java.util.Set; |
7 |
|
|
import java.util.TreeMap; |
8 |
|
|
import java.util.TreeSet; |
9 |
torben |
1805 |
|
10 |
|
|
import org.bukkit.ChatColor; |
11 |
|
|
import org.bukkit.Location; |
12 |
|
|
import org.bukkit.Material; |
13 |
torben |
1807 |
import org.bukkit.OfflinePlayer; |
14 |
torben |
1805 |
import org.bukkit.Server; |
15 |
|
|
import org.bukkit.World; |
16 |
|
|
import org.bukkit.block.Block; |
17 |
|
|
import org.bukkit.block.Chest; |
18 |
|
|
import org.bukkit.block.DoubleChest; |
19 |
|
|
import org.bukkit.command.Command; |
20 |
|
|
import org.bukkit.command.CommandExecutor; |
21 |
|
|
import org.bukkit.command.CommandSender; |
22 |
|
|
import org.bukkit.entity.Player; |
23 |
|
|
import org.bukkit.event.EventHandler; |
24 |
|
|
import org.bukkit.event.Listener; |
25 |
|
|
import org.bukkit.event.block.Action; |
26 |
|
|
import org.bukkit.event.block.BlockBreakEvent; |
27 |
torben |
1868 |
import org.bukkit.event.block.BlockBurnEvent; |
28 |
torben |
1805 |
import org.bukkit.event.block.BlockPlaceEvent; |
29 |
torben |
1806 |
import org.bukkit.event.entity.EntityExplodeEvent; |
30 |
torben |
1807 |
import org.bukkit.event.inventory.InventoryCloseEvent; |
31 |
|
|
import org.bukkit.event.inventory.InventoryOpenEvent; |
32 |
torben |
1805 |
import org.bukkit.event.player.PlayerInteractEvent; |
33 |
|
|
import org.bukkit.inventory.InventoryHolder; |
34 |
torben |
1807 |
import org.bukkit.inventory.ItemStack; |
35 |
torben |
1805 |
|
36 |
|
|
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
37 |
torben |
1807 |
import dk.thoerup.bukkit.hoeruputils.Util; |
38 |
torben |
1805 |
|
39 |
|
|
|
40 |
torben |
1807 |
|
41 |
|
|
public class AdvancedChest implements Listener, CommandExecutor{ |
42 |
|
|
|
43 |
|
|
class ItemCount extends TreeMap<Integer,Integer> { |
44 |
|
|
private static final long serialVersionUID = 1L; |
45 |
|
|
}; |
46 |
torben |
1805 |
|
47 |
torben |
1807 |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
48 |
|
|
|
49 |
torben |
1805 |
|
50 |
torben |
1807 |
HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>(); |
51 |
torben |
1805 |
|
52 |
|
|
|
53 |
|
|
HoerupUtilsPlugin plugin; |
54 |
|
|
Server server; |
55 |
|
|
|
56 |
torben |
1807 |
public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) { |
57 |
torben |
1805 |
this.plugin = plugin; |
58 |
|
|
server = plugin.getServer(); |
59 |
|
|
try { |
60 |
|
|
loadChests(); |
61 |
|
|
} catch (Exception e) { |
62 |
|
|
e.printStackTrace(); |
63 |
|
|
//r.run(); |
64 |
|
|
loadChests(); |
65 |
|
|
} |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
@Override |
70 |
|
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
71 |
|
|
if (! (sender instanceof Player) ) { |
72 |
|
|
sender.sendMessage("this is not a console command!"); |
73 |
|
|
return true; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
Player player = (Player) sender; |
77 |
|
|
|
78 |
torben |
1807 |
if (args.length == 0) { |
79 |
|
|
player.sendMessage("Usage:"); |
80 |
torben |
1854 |
player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer|setowner) [player]"); |
81 |
torben |
1807 |
return true; |
82 |
|
|
} |
83 |
torben |
1805 |
|
84 |
torben |
1807 |
|
85 |
|
|
Block b = player.getTargetBlock(null, 30); |
86 |
torben |
1805 |
|
87 |
|
|
if (b.getTypeId() != 54) { |
88 |
torben |
1807 |
player.sendMessage("[Chest] Please look at the chest you want to protect"); |
89 |
torben |
1805 |
return true; |
90 |
|
|
} |
91 |
|
|
|
92 |
torben |
1807 |
Location loc = b.getLocation(); |
93 |
torben |
1805 |
Location loc2 = getNeighborChest(loc); |
94 |
|
|
|
95 |
torben |
1807 |
ChestBean chest = chestMap.get(loc); |
96 |
|
|
String cmd = args[0].toLowerCase(); |
97 |
|
|
|
98 |
|
|
if (cmd.equals("status")) { |
99 |
|
|
if (chest != null) { |
100 |
|
|
String mode = ""; |
101 |
|
|
switch (chest.getChestType()) { |
102 |
|
|
case ChestBean.LOCKED: |
103 |
|
|
mode = "locked"; |
104 |
|
|
break; |
105 |
|
|
case ChestBean.SNITCHING: |
106 |
|
|
mode = "snitching"; |
107 |
|
|
break; |
108 |
|
|
default: |
109 |
|
|
mode = "unknown ??"; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
player.sendMessage(ChatColor.GREEN + "Chest is a " + mode + " chest owned by " + chest.getOwner()); |
113 |
|
|
player.sendMessage(ChatColor.GREEN + "Allowed players: " + chest.getModifyPlayers() ); |
114 |
|
|
} else { |
115 |
|
|
player.sendMessage(ChatColor.GREEN + "The chest is not protected"); |
116 |
|
|
} |
117 |
|
|
return true; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
if (cmd.equals("lock") || cmd.equals("snitch")) { |
121 |
|
|
if (chest == null) { |
122 |
|
|
chest = createChest(player.getName(), "", loc); |
123 |
|
|
if (loc2 != null) { |
124 |
|
|
chest.setDoublechest(true); |
125 |
|
|
} |
126 |
|
|
String modeStr = ""; |
127 |
|
|
if (cmd.equals("lock")) { |
128 |
|
|
chest.setChestType( ChestBean.LOCKED); |
129 |
|
|
modeStr = "locked"; |
130 |
|
|
} else { |
131 |
|
|
chest.setChestType( ChestBean.SNITCHING); |
132 |
|
|
modeStr = "snitching"; |
133 |
|
|
} |
134 |
|
|
chest.setModifyPlayers(""); |
135 |
|
|
addChest(loc, chest); |
136 |
|
|
player.sendMessage("Chest is now " + modeStr); |
137 |
|
|
} else { |
138 |
|
|
player.sendMessage("This chest is already protected"); |
139 |
|
|
} |
140 |
|
|
return true; |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
if (cmd.equals("remove")) { |
144 |
torben |
1853 |
if (chest == null) { |
145 |
|
|
player.sendMessage("This chest is not protected"); |
146 |
|
|
return true; |
147 |
|
|
} |
148 |
torben |
1852 |
if (! chest.getOwner().equals( player.getName() ) ) { |
149 |
|
|
player.sendMessage("You can not remove lock from a chest you don't own"); |
150 |
|
|
return true; |
151 |
|
|
} |
152 |
torben |
1853 |
|
153 |
|
|
player.sendMessage("[LockedChest] Removing protection from chest"); |
154 |
|
|
removeChest(loc); |
155 |
|
|
|
156 |
torben |
1807 |
return true; |
157 |
|
|
} |
158 |
torben |
1854 |
|
159 |
|
|
if (cmd.equals("setowner")) { |
160 |
|
|
if (chest == null) { |
161 |
|
|
player.sendMessage("This chest is not protected"); |
162 |
|
|
return true; |
163 |
|
|
} |
164 |
|
|
if (! chest.getOwner().equals( player.getName() ) ) { |
165 |
|
|
player.sendMessage("You can not set new owner of a chest you dont own"); |
166 |
|
|
return true; |
167 |
|
|
} |
168 |
|
|
if (args.length != 2) { |
169 |
|
|
player.sendMessage("You need to specify which player should own this chest"); |
170 |
|
|
return true; |
171 |
|
|
} |
172 |
|
|
OfflinePlayer p2 = server.getOfflinePlayer(args[1]); |
173 |
|
|
if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { |
174 |
|
|
player.sendMessage("Unknown user: " + args[1] ); |
175 |
|
|
return true; |
176 |
|
|
} |
177 |
|
|
chest.setOwner( p2.getName() ); |
178 |
|
|
plugin.getDatabase().save( chest ); |
179 |
|
|
player.sendMessage("ok"); |
180 |
|
|
return true; |
181 |
|
|
} |
182 |
torben |
1807 |
|
183 |
|
|
if (cmd.equals("addplayer") || cmd.equals("removeplayer")) { |
184 |
|
|
if (chest == null) { |
185 |
|
|
player.sendMessage("This chest is not protected"); |
186 |
|
|
return true; |
187 |
|
|
} |
188 |
torben |
1852 |
if (! chest.getOwner().equals( player.getName() ) ) { |
189 |
|
|
player.sendMessage("You can not add/remove players from a chest you don't own"); |
190 |
|
|
return true; |
191 |
|
|
} |
192 |
torben |
1807 |
if (args.length != 2) { |
193 |
|
|
player.sendMessage("You need to specify which player to add or remove"); |
194 |
|
|
return true; |
195 |
|
|
} |
196 |
|
|
OfflinePlayer p2 = server.getOfflinePlayer(args[1]); |
197 |
|
|
if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { |
198 |
|
|
player.sendMessage("Unknown user: " + args[1] ); |
199 |
|
|
return true; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
203 |
|
|
if (cmd.equals("addplayer")) { |
204 |
torben |
1808 |
players.add(p2.getName()); |
205 |
torben |
1807 |
} else { |
206 |
torben |
1808 |
players.remove(p2.getName()); |
207 |
torben |
1807 |
} |
208 |
|
|
|
209 |
|
|
chest.setModifyPlayers( Util.setToString(players) ); |
210 |
|
|
plugin.getDatabase().save( chest ); |
211 |
|
|
player.sendMessage("ok"); |
212 |
|
|
return true; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
/* |
216 |
torben |
1805 |
if (chest != null) { |
217 |
|
|
if (chest.getOwner().equals(player.getName())) { |
218 |
|
|
player.sendMessage("[LockedChest] Removing lock from chest"); |
219 |
|
|
removeChest(loc); |
220 |
|
|
} else { |
221 |
torben |
1807 |
player.sendMessage("[LockedChest] Chest is already protected"); |
222 |
torben |
1805 |
} |
223 |
|
|
|
224 |
|
|
return true; |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
chest = createChest(player.getName(), "", loc); |
228 |
|
|
if (loc2 != null) { |
229 |
|
|
chest.setDoublechest(true); |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
addChest(loc, chest); |
233 |
|
|
|
234 |
|
|
|
235 |
|
|
player.sendMessage("[LockedChest] Chest is now locked"); |
236 |
torben |
1807 |
*/ |
237 |
torben |
1805 |
|
238 |
torben |
1807 |
player.sendMessage("Unknown argument, " + cmd); |
239 |
torben |
1805 |
|
240 |
|
|
return true; |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
@EventHandler |
244 |
|
|
public void onBlockBreak(BlockBreakEvent event) { |
245 |
|
|
Location loc = event.getBlock().getLocation(); |
246 |
torben |
1807 |
ChestBean chest = chestMap.get(loc); |
247 |
torben |
1805 |
if (chest != null) { |
248 |
|
|
if (chest.getOwner().equals(event.getPlayer().getName())) { |
249 |
|
|
removeChest(loc); |
250 |
torben |
1807 |
event.getPlayer().sendMessage("[AdvancedChest] The destroyed chest was locked or snitching"); |
251 |
torben |
1805 |
} else { |
252 |
|
|
event.setCancelled(true); |
253 |
|
|
event.getPlayer().sendMessage("You can't destroy that chest"); |
254 |
torben |
1810 |
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried breaking a chest owned by " + chest.getOwner() ); |
255 |
torben |
1805 |
} |
256 |
|
|
} |
257 |
|
|
} |
258 |
torben |
1807 |
public void addChest(Location loc, ChestBean chest) { |
259 |
torben |
1805 |
chestMap.put(loc, chest); |
260 |
|
|
if (chest.isDoublechest()) { |
261 |
|
|
Location loc2 = getNeighborChest(loc); |
262 |
|
|
chestMap.put(loc2, chest); |
263 |
|
|
} |
264 |
|
|
plugin.getDatabase().save(chest); |
265 |
|
|
|
266 |
|
|
reloadChests(); |
267 |
|
|
|
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
void removeChest(Location loc) { |
271 |
torben |
1807 |
ChestBean chest = chestMap.remove(loc); |
272 |
torben |
1805 |
if (chest != null) { |
273 |
|
|
if (chest.isDoublechest()){ |
274 |
|
|
Location loc2 = getNeighborChest(loc); |
275 |
|
|
chestMap.remove(loc2); |
276 |
|
|
} |
277 |
|
|
plugin.getDatabase().delete(chest); |
278 |
|
|
} |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
int loadChestsWorker() { |
282 |
torben |
1807 |
List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList(); |
283 |
|
|
for (ChestBean chest : chestlist) { |
284 |
torben |
1805 |
Location loc = getChestLocation(server, chest); |
285 |
|
|
chestMap.put(loc, chest); |
286 |
|
|
|
287 |
|
|
if (chest.isDoublechest()) { |
288 |
|
|
Location loc2 = getNeighborChest(loc); |
289 |
|
|
chestMap.put(loc2, chest); |
290 |
|
|
} |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
return chestlist.size(); |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
void reloadChests() { |
297 |
|
|
chestMap.clear(); |
298 |
|
|
loadChestsWorker(); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
void loadChests() { |
302 |
|
|
int count = loadChestsWorker(); |
303 |
torben |
1813 |
server.getLogger().info("[AdvancedChest] loaded " + count + " chests"); |
304 |
torben |
1805 |
} |
305 |
|
|
|
306 |
|
|
|
307 |
torben |
1807 |
public ChestBean createChest(String owner, String description, Location loc) { |
308 |
torben |
1805 |
|
309 |
torben |
1807 |
ChestBean chest = new ChestBean(); |
310 |
torben |
1805 |
chest.setOwner(owner); |
311 |
|
|
chest.setDescription(description); |
312 |
|
|
setChestLocation(chest, loc); |
313 |
|
|
|
314 |
|
|
return chest; |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
|
318 |
torben |
1807 |
public void setChestLocation(ChestBean chest, Location loc) { |
319 |
torben |
1805 |
chest.setWorld( loc.getWorld().getName() ); |
320 |
|
|
chest.setX( loc.getBlockX() ); |
321 |
|
|
chest.setY( loc.getBlockY() ); |
322 |
|
|
chest.setZ( loc.getBlockZ() ); |
323 |
|
|
} |
324 |
|
|
|
325 |
torben |
1807 |
public Location getChestLocation(Server server, ChestBean chest) { |
326 |
torben |
1805 |
World wrld = server.getWorld(chest.getWorld()); |
327 |
|
|
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
|
331 |
|
|
/* |
332 |
|
|
void saveChests() { |
333 |
|
|
|
334 |
|
|
}*/ |
335 |
|
|
|
336 |
|
|
Location getNeighborChest(Location loc) { |
337 |
|
|
World world = loc.getWorld(); |
338 |
|
|
|
339 |
|
|
Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() ); |
340 |
|
|
if (world.getBlockAt(target).getType() == Material.CHEST ) |
341 |
|
|
return target; |
342 |
|
|
|
343 |
|
|
target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() ); |
344 |
|
|
if (world.getBlockAt(target).getType() == Material.CHEST ) |
345 |
|
|
return target; |
346 |
|
|
|
347 |
|
|
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() +1); |
348 |
|
|
if (world.getBlockAt(target).getType() == Material.CHEST ) |
349 |
|
|
return target; |
350 |
|
|
|
351 |
|
|
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1); |
352 |
|
|
if (world.getBlockAt(target).getType() == Material.CHEST ) |
353 |
|
|
return target; |
354 |
|
|
|
355 |
|
|
return null; |
356 |
|
|
} |
357 |
|
|
|
358 |
|
|
|
359 |
|
|
Location getChestLocation(InventoryHolder holder) { |
360 |
|
|
Location loc; |
361 |
|
|
if ( holder instanceof Chest) { |
362 |
|
|
loc = ( (Chest)holder).getLocation(); |
363 |
|
|
} else { |
364 |
|
|
loc = ( (DoubleChest)holder).getLocation(); |
365 |
|
|
} |
366 |
|
|
|
367 |
|
|
loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5 |
368 |
|
|
loc.setZ( loc.getBlockZ() ); // -- // -- |
369 |
|
|
|
370 |
|
|
return loc; |
371 |
|
|
} |
372 |
|
|
|
373 |
|
|
@EventHandler |
374 |
|
|
public void onChestPlaced(BlockPlaceEvent event) { |
375 |
|
|
Block block = event.getBlock(); |
376 |
|
|
|
377 |
|
|
if (block.getType() != Material.CHEST) { |
378 |
|
|
return; |
379 |
|
|
} |
380 |
|
|
|
381 |
|
|
Location chestloc = getNeighborChest( block.getLocation() ); |
382 |
|
|
if (chestloc != null) { |
383 |
torben |
1807 |
ChestBean chest = chestMap.get(chestloc); |
384 |
torben |
1805 |
|
385 |
|
|
if (chest != null) { //the neighbor is a locked chest |
386 |
|
|
|
387 |
|
|
|
388 |
|
|
chest.setDoublechest(true); |
389 |
|
|
addChest(chestloc, chest); |
390 |
|
|
|
391 |
|
|
|
392 |
torben |
1807 |
event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); |
393 |
torben |
1805 |
} |
394 |
|
|
|
395 |
|
|
} |
396 |
|
|
} |
397 |
torben |
1806 |
|
398 |
|
|
|
399 |
|
|
@EventHandler |
400 |
|
|
public void onChestExplode(EntityExplodeEvent event) { |
401 |
|
|
for (Block b : event.blockList() ) { |
402 |
torben |
1807 |
ChestBean chest = chestMap.get( b.getLocation() ); |
403 |
torben |
1806 |
if (chest != null) { |
404 |
torben |
1810 |
server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() ); |
405 |
torben |
1806 |
event.setCancelled( true ); |
406 |
|
|
return; |
407 |
|
|
} |
408 |
|
|
} |
409 |
torben |
1868 |
} |
410 |
|
|
|
411 |
|
|
@EventHandler |
412 |
|
|
public void onChestBurn(BlockBurnEvent event) { |
413 |
|
|
ChestBean chest = chestMap.get( event.getBlock().getLocation() ); |
414 |
|
|
if (chest != null) { |
415 |
|
|
server.getLogger().info( "[AdvancedChest] prevented a fire from destrying chest owned by " + chest.getOwner() ); |
416 |
|
|
event.setCancelled( true); |
417 |
|
|
} |
418 |
|
|
} |
419 |
torben |
1805 |
|
420 |
|
|
// prevent a user from opening a chest |
421 |
|
|
@EventHandler |
422 |
|
|
public void onChestInteract(PlayerInteractEvent event) { |
423 |
|
|
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { |
424 |
|
|
Block b = event.getClickedBlock(); |
425 |
|
|
|
426 |
|
|
if (b.getType() == Material.CHEST) { |
427 |
|
|
|
428 |
|
|
Location loc = b.getLocation(); |
429 |
|
|
|
430 |
torben |
1807 |
ChestBean chest = chestMap.get( loc ); |
431 |
torben |
1805 |
if (chest == null) { |
432 |
|
|
return; //chest not surveyed by this plugin |
433 |
|
|
} |
434 |
|
|
|
435 |
torben |
1807 |
if (chest.getChestType() != ChestBean.LOCKED ) { |
436 |
|
|
return; //this is not a locked chests |
437 |
|
|
} |
438 |
|
|
|
439 |
torben |
1805 |
Player player = (Player) event.getPlayer(); |
440 |
|
|
if (player.getName().equals(chest.getOwner() )) { |
441 |
|
|
return; //chest is opened by it's owner |
442 |
|
|
} |
443 |
|
|
|
444 |
torben |
1809 |
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
445 |
|
|
if ( players.contains(player.getName()) ) { |
446 |
|
|
return; //this player is on the whitelist so he may open |
447 |
torben |
1805 |
} |
448 |
|
|
|
449 |
torben |
1809 |
|
450 |
torben |
1811 |
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner() ); |
451 |
torben |
1805 |
player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); |
452 |
|
|
event.setCancelled(true); |
453 |
|
|
} |
454 |
|
|
} |
455 |
|
|
} |
456 |
torben |
1807 |
|
457 |
|
|
@EventHandler |
458 |
|
|
public void onChestOpen(InventoryOpenEvent event) { |
459 |
|
|
|
460 |
|
|
if (! (event.getPlayer() instanceof Player)) { |
461 |
|
|
return; |
462 |
|
|
} |
463 |
|
|
|
464 |
|
|
|
465 |
|
|
|
466 |
|
|
InventoryHolder holder = event.getInventory().getHolder(); |
467 |
|
|
if (holder instanceof Chest || holder instanceof DoubleChest) { |
468 |
|
|
Location loc = getChestLocation(holder); |
469 |
|
|
|
470 |
|
|
ChestBean chest = chestMap.get( loc ); |
471 |
|
|
if (chest == null) { |
472 |
|
|
return; //chest not surveyed by this plugin |
473 |
|
|
} |
474 |
|
|
|
475 |
|
|
if (chest.getChestType() != ChestBean.SNITCHING) { |
476 |
|
|
return; // not a snitching chest |
477 |
|
|
} |
478 |
|
|
|
479 |
|
|
|
480 |
|
|
Player player = (Player) event.getPlayer(); |
481 |
|
|
if (player.getName().equals(chest.getOwner() )) { |
482 |
|
|
return; //chest is owned by it's own player |
483 |
torben |
1809 |
} |
484 |
torben |
1807 |
|
485 |
torben |
1809 |
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
486 |
|
|
if ( players.contains(player.getName()) ) { |
487 |
|
|
return; //this player is on the whitelist so he may open |
488 |
|
|
} |
489 |
torben |
1807 |
|
490 |
torben |
1809 |
|
491 |
torben |
1807 |
ItemCount contents = countItems( event.getInventory().getContents() ); |
492 |
|
|
|
493 |
|
|
contentMap.put(player.getName(), contents ); |
494 |
|
|
} |
495 |
|
|
} |
496 |
torben |
1805 |
|
497 |
torben |
1807 |
@EventHandler |
498 |
|
|
public void onChestClose(InventoryCloseEvent event) { |
499 |
|
|
if (! (event.getPlayer() instanceof Player)) { |
500 |
|
|
return; |
501 |
|
|
} |
502 |
|
|
|
503 |
|
|
|
504 |
|
|
InventoryHolder holder = event.getInventory().getHolder(); |
505 |
|
|
if (holder instanceof Chest || holder instanceof DoubleChest) { |
506 |
|
|
Location loc = getChestLocation(holder); |
507 |
|
|
ChestBean chest = chestMap.get(loc); |
508 |
|
|
|
509 |
|
|
if (chest == null) { //chest was not a snitching chest |
510 |
|
|
return; |
511 |
|
|
} |
512 |
|
|
|
513 |
|
|
if (chest.getChestType() != ChestBean.SNITCHING) { |
514 |
|
|
return; // not a snitching chest |
515 |
|
|
} |
516 |
|
|
|
517 |
|
|
|
518 |
|
|
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
519 |
|
|
|
520 |
|
|
|
521 |
|
|
Player player = (Player) event.getPlayer(); |
522 |
|
|
|
523 |
|
|
ItemCount savedContent = contentMap.get( player.getName() ); |
524 |
|
|
|
525 |
|
|
if (savedContent == null) { |
526 |
|
|
return; |
527 |
|
|
} |
528 |
|
|
|
529 |
|
|
contentMap.remove( player.getName() ); |
530 |
|
|
|
531 |
|
|
ItemCount content = countItems( event.getInventory().getContents() ); |
532 |
|
|
|
533 |
|
|
Set<Integer> combinedKeyset = new TreeSet<Integer>(); |
534 |
|
|
combinedKeyset.addAll( savedContent.keySet() ); |
535 |
|
|
combinedKeyset.addAll( content.keySet() ); |
536 |
|
|
|
537 |
|
|
for (Integer item : combinedKeyset ) { |
538 |
|
|
Integer savedcount = savedContent.get(item); |
539 |
|
|
Integer count = content.get(item); |
540 |
|
|
|
541 |
|
|
if (savedcount == null) |
542 |
|
|
savedcount = 0; |
543 |
|
|
if (count == null) |
544 |
|
|
count = 0; |
545 |
|
|
|
546 |
|
|
|
547 |
|
|
int diff = Math.abs( savedcount - count); |
548 |
|
|
|
549 |
|
|
if (diff > 0) { |
550 |
|
|
String material = Material.getMaterial(item).name(); |
551 |
|
|
String msg = null; |
552 |
|
|
|
553 |
|
|
if (count > savedcount) { |
554 |
|
|
msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
555 |
|
|
} else { //(count < savedcount) |
556 |
|
|
msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
557 |
|
|
} |
558 |
|
|
|
559 |
|
|
|
560 |
torben |
1813 |
server.getLogger().info( "[AdvancedChest]" + msg); |
561 |
torben |
1807 |
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
562 |
|
|
} |
563 |
|
|
|
564 |
|
|
} |
565 |
|
|
|
566 |
|
|
|
567 |
|
|
} |
568 |
|
|
} |
569 |
|
|
|
570 |
|
|
ItemCount countItems(ItemStack[] input) { |
571 |
|
|
ItemCount output = new ItemCount(); |
572 |
|
|
for (int i=0; i<input.length; i++) { |
573 |
|
|
ItemStack current = input[i]; |
574 |
|
|
if (current == null) |
575 |
|
|
continue; |
576 |
|
|
|
577 |
|
|
int type = current.getTypeId(); |
578 |
|
|
|
579 |
|
|
Integer amount = output.get(type); |
580 |
|
|
if (amount == null) |
581 |
|
|
amount = 0; |
582 |
|
|
|
583 |
|
|
output.put(type, amount + current.getAmount() ); |
584 |
|
|
} |
585 |
|
|
return output; |
586 |
|
|
} |
587 |
|
|
/* |
588 |
|
|
ItemStack[] cloneItemStacks(ItemStack[] input) { |
589 |
|
|
ItemStack[] output = new ItemStack[ input.length ]; |
590 |
|
|
for (int i=0; i<input.length; i++) { |
591 |
|
|
if (input[i] != null) { |
592 |
|
|
output[i] = input[i].clone(); |
593 |
|
|
} else { |
594 |
|
|
output[i] = new ItemStack(0, 0); |
595 |
|
|
} |
596 |
|
|
} |
597 |
|
|
return output; |
598 |
|
|
}*/ |
599 |
|
|
|
600 |
|
|
|
601 |
torben |
1805 |
} |