3 |
|
|
4 |
import java.util.HashMap; |
import java.util.HashMap; |
5 |
import java.util.List; |
import java.util.List; |
6 |
|
import java.util.Set; |
7 |
|
import java.util.TreeMap; |
8 |
|
import java.util.TreeSet; |
9 |
|
|
10 |
import org.bukkit.ChatColor; |
import org.bukkit.ChatColor; |
11 |
import org.bukkit.Location; |
import org.bukkit.Location; |
12 |
import org.bukkit.Material; |
import org.bukkit.Material; |
13 |
|
import org.bukkit.OfflinePlayer; |
14 |
import org.bukkit.Server; |
import org.bukkit.Server; |
15 |
import org.bukkit.World; |
import org.bukkit.World; |
16 |
import org.bukkit.block.Block; |
import org.bukkit.block.Block; |
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; |
31 |
|
import org.bukkit.event.inventory.InventoryOpenEvent; |
32 |
import org.bukkit.event.player.PlayerInteractEvent; |
import org.bukkit.event.player.PlayerInteractEvent; |
33 |
import org.bukkit.inventory.InventoryHolder; |
import org.bukkit.inventory.InventoryHolder; |
34 |
|
import org.bukkit.inventory.ItemStack; |
35 |
|
|
36 |
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
37 |
|
import dk.thoerup.bukkit.hoeruputils.Util; |
38 |
|
|
39 |
|
|
40 |
public class LockedChest implements Listener, CommandExecutor{ |
|
41 |
|
public class AdvancedChest implements Listener, CommandExecutor{ |
42 |
|
|
43 |
|
class ItemCount extends TreeMap<Integer,Integer> { |
44 |
|
private static final long serialVersionUID = 1L; |
45 |
|
}; |
46 |
|
|
47 |
|
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
48 |
|
|
49 |
|
|
50 |
HashMap<Location,LockedChestBean> chestMap = new HashMap<Location, LockedChestBean>(); |
HashMap<Location,ChestBean> chestMap = new HashMap<Location, ChestBean>(); |
51 |
|
|
52 |
|
|
53 |
HoerupUtilsPlugin plugin; |
HoerupUtilsPlugin plugin; |
54 |
Server server; |
Server server; |
55 |
|
|
56 |
public LockedChest(HoerupUtilsPlugin plugin, Runnable r) { |
public AdvancedChest(HoerupUtilsPlugin plugin, Runnable r) { |
57 |
this.plugin = plugin; |
this.plugin = plugin; |
58 |
server = plugin.getServer(); |
server = plugin.getServer(); |
59 |
try { |
try { |
75 |
|
|
76 |
Player player = (Player) sender; |
Player player = (Player) sender; |
77 |
|
|
78 |
|
if (args.length == 0) { |
79 |
|
player.sendMessage("Usage:"); |
80 |
|
player.sendMessage("/chest (status|lock|snitch|remove|addplayer|removeplayer|setowner|comment) [player]"); |
81 |
|
return true; |
82 |
|
} |
83 |
|
|
84 |
Block b = player.getTargetBlock(null, 30); |
|
85 |
Location loc = b.getLocation(); |
Block b = player.getTargetBlock(null, 30); |
86 |
|
|
87 |
if (b.getTypeId() != 54) { |
if (b.getTypeId() != 54) { |
88 |
player.sendMessage("[LockedChest] Please look at the chest you want to lock"); |
player.sendMessage("[Chest] Please look at the chest you want to protect"); |
89 |
return true; |
return true; |
90 |
} |
} |
91 |
|
|
92 |
|
Location loc = b.getLocation(); |
93 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
94 |
|
|
95 |
LockedChestBean chest = chestMap.get(loc); |
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 |
|
player.sendMessage(ChatColor.GREEN + "Comment: " + chest.getComment() ); |
115 |
|
} else { |
116 |
|
player.sendMessage(ChatColor.GREEN + "The chest is not protected"); |
117 |
|
} |
118 |
|
return true; |
119 |
|
} |
120 |
|
|
121 |
|
if (cmd.equals("lock") || cmd.equals("snitch")) { |
122 |
|
if (chest == null) { |
123 |
|
chest = createChest(player.getName(), "", loc); |
124 |
|
if (loc2 != null) { |
125 |
|
chest.setDoublechest(true); |
126 |
|
} |
127 |
|
String modeStr = ""; |
128 |
|
if (cmd.equals("lock")) { |
129 |
|
chest.setChestType( ChestBean.LOCKED); |
130 |
|
modeStr = "locked"; |
131 |
|
} else { |
132 |
|
chest.setChestType( ChestBean.SNITCHING); |
133 |
|
modeStr = "snitching"; |
134 |
|
} |
135 |
|
chest.setModifyPlayers(""); |
136 |
|
chest.setComment(""); |
137 |
|
addChest(loc, chest); |
138 |
|
player.sendMessage("Chest is now " + modeStr); |
139 |
|
} else { |
140 |
|
server.getLogger().info( player.getName() + " tried to protect a chest owned by " + chest.getOwner() ); |
141 |
|
player.sendMessage("This chest is already protected"); |
142 |
|
} |
143 |
|
return true; |
144 |
|
} |
145 |
|
|
146 |
|
if (cmd.equals("remove")) { |
147 |
|
if (chest == null) { |
148 |
|
player.sendMessage("This chest is not protected"); |
149 |
|
return true; |
150 |
|
} |
151 |
|
if (! chest.getOwner().equals( player.getName() ) ) { |
152 |
|
player.sendMessage("You can not remove lock from a chest you don't own"); |
153 |
|
server.getLogger().info( player.getName() + " tried to remove protection froma chest owned by " + chest.getOwner() ); |
154 |
|
return true; |
155 |
|
} |
156 |
|
|
157 |
|
player.sendMessage("[LockedChest] Removing protection from chest"); |
158 |
|
removeChest(loc); |
159 |
|
|
160 |
|
return true; |
161 |
|
} |
162 |
|
|
163 |
|
if (cmd.equals("setowner")) { |
164 |
|
if (chest == null) { |
165 |
|
player.sendMessage("This chest is not protected"); |
166 |
|
return true; |
167 |
|
} |
168 |
|
if (! chest.getOwner().equals( player.getName() ) ) { |
169 |
|
player.sendMessage("You can not set new owner of a chest you dont own"); |
170 |
|
server.getLogger().info( player.getName() + " tried to set owner on a chest owned by " + chest.getOwner() ); |
171 |
|
return true; |
172 |
|
} |
173 |
|
if (args.length != 2) { |
174 |
|
player.sendMessage("You need to specify which player should own this chest"); |
175 |
|
return true; |
176 |
|
} |
177 |
|
OfflinePlayer p2 = server.getOfflinePlayer(args[1]); |
178 |
|
if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { |
179 |
|
player.sendMessage("Unknown user: " + args[1] ); |
180 |
|
return true; |
181 |
|
} |
182 |
|
chest.setOwner( p2.getName() ); |
183 |
|
plugin.getDatabase().save( chest ); |
184 |
|
player.sendMessage("ok"); |
185 |
|
return true; |
186 |
|
} |
187 |
|
|
188 |
|
if (cmd.equals("addplayer") || cmd.equals("removeplayer")) { |
189 |
|
if (chest == null) { |
190 |
|
player.sendMessage("This chest is not protected"); |
191 |
|
return true; |
192 |
|
} |
193 |
|
if (! chest.getOwner().equals( player.getName() ) ) { |
194 |
|
player.sendMessage("You can not add/remove players from a chest you don't own"); |
195 |
|
server.getLogger().info( player.getName() + " tried to add/remove player on a chest owned by " + chest.getOwner() ); |
196 |
|
return true; |
197 |
|
} |
198 |
|
if (args.length != 2) { |
199 |
|
player.sendMessage("You need to specify which player to add or remove"); |
200 |
|
return true; |
201 |
|
} |
202 |
|
OfflinePlayer p2 = server.getOfflinePlayer(args[1]); |
203 |
|
if ( p2.hasPlayedBefore() == false && p2.isOnline() == false) { |
204 |
|
player.sendMessage("Unknown user: " + args[1] ); |
205 |
|
return true; |
206 |
|
} |
207 |
|
|
208 |
|
Set<String> players = Util.stringToSet( chest.getModifyPlayers() ); |
209 |
|
if (cmd.equals("addplayer")) { |
210 |
|
players.add(p2.getName()); |
211 |
|
} else { |
212 |
|
players.remove(p2.getName()); |
213 |
|
} |
214 |
|
|
215 |
|
chest.setModifyPlayers( Util.setToString(players) ); |
216 |
|
plugin.getDatabase().save( chest ); |
217 |
|
player.sendMessage("ok"); |
218 |
|
return true; |
219 |
|
} |
220 |
|
if (cmd.equals("comment")) { |
221 |
|
if (chest == null) { |
222 |
|
player.sendMessage("This chest is not protected"); |
223 |
|
return true; |
224 |
|
} |
225 |
|
if (! chest.getOwner().equals( player.getName() ) ) { |
226 |
|
player.sendMessage("You can not comment a chest you don't own"); |
227 |
|
server.getLogger().info( player.getName() + " tried to comment on a chest owned by " + chest.getOwner() ); |
228 |
|
return true; |
229 |
|
} |
230 |
|
|
231 |
|
StringBuilder sb = new StringBuilder(); |
232 |
|
for (int i=1; i<args.length; i++) { |
233 |
|
sb.append(args[i]).append(" "); |
234 |
|
} |
235 |
|
String comment = sb.toString().trim(); |
236 |
|
|
237 |
|
chest.setComment( comment ); |
238 |
|
plugin.getDatabase().save( chest ); |
239 |
|
player.sendMessage("Comment set"); |
240 |
|
|
241 |
|
return true; |
242 |
|
} |
243 |
|
|
244 |
|
/* |
245 |
if (chest != null) { |
if (chest != null) { |
246 |
if (chest.getOwner().equals(player.getName())) { |
if (chest.getOwner().equals(player.getName())) { |
247 |
player.sendMessage("[LockedChest] Removing lock from chest"); |
player.sendMessage("[LockedChest] Removing lock from chest"); |
248 |
removeChest(loc); |
removeChest(loc); |
249 |
} else { |
} else { |
250 |
player.sendMessage("[LockedChest] Chest is already locked"); |
player.sendMessage("[LockedChest] Chest is already protected"); |
251 |
} |
} |
252 |
|
|
253 |
return true; |
return true; |
262 |
|
|
263 |
|
|
264 |
player.sendMessage("[LockedChest] Chest is now locked"); |
player.sendMessage("[LockedChest] Chest is now locked"); |
265 |
|
*/ |
266 |
|
|
267 |
|
player.sendMessage("Unknown argument, " + cmd); |
268 |
|
|
269 |
return true; |
return true; |
270 |
} |
} |
272 |
@EventHandler |
@EventHandler |
273 |
public void onBlockBreak(BlockBreakEvent event) { |
public void onBlockBreak(BlockBreakEvent event) { |
274 |
Location loc = event.getBlock().getLocation(); |
Location loc = event.getBlock().getLocation(); |
275 |
LockedChestBean chest = chestMap.get(loc); |
ChestBean chest = chestMap.get(loc); |
276 |
if (chest != null) { |
if (chest != null) { |
277 |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
278 |
removeChest(loc); |
removeChest(loc); |
279 |
event.getPlayer().sendMessage("[LockedChest] The destroyed chest was locked"); |
event.getPlayer().sendMessage("[AdvancedChest] The destroyed chest was locked or snitching"); |
280 |
} else { |
} else { |
281 |
event.setCancelled(true); |
event.setCancelled(true); |
282 |
event.getPlayer().sendMessage("You can't destroy that chest"); |
event.getPlayer().sendMessage("You can't destroy that chest"); |
283 |
|
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried breaking a chest owned by " + chest.getOwner() + chest.getCommentString() ); |
284 |
} |
} |
285 |
} |
} |
286 |
} |
} |
287 |
public void addChest(Location loc, LockedChestBean chest) { |
public void addChest(Location loc, ChestBean chest) { |
288 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
289 |
if (chest.isDoublechest()) { |
if (chest.isDoublechest()) { |
290 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
297 |
} |
} |
298 |
|
|
299 |
void removeChest(Location loc) { |
void removeChest(Location loc) { |
300 |
LockedChestBean chest = chestMap.remove(loc); |
ChestBean chest = chestMap.remove(loc); |
301 |
if (chest != null) { |
if (chest != null) { |
302 |
if (chest.isDoublechest()){ |
if (chest.isDoublechest()){ |
303 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
308 |
} |
} |
309 |
|
|
310 |
int loadChestsWorker() { |
int loadChestsWorker() { |
311 |
List<LockedChestBean> chestlist = plugin.getDatabase().find( LockedChestBean.class).findList(); |
List<ChestBean> chestlist = plugin.getDatabase().find( ChestBean.class).findList(); |
312 |
for (LockedChestBean chest : chestlist) { |
for (ChestBean chest : chestlist) { |
313 |
Location loc = getChestLocation(server, chest); |
Location loc = getChestLocation(server, chest); |
314 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
315 |
|
|
329 |
|
|
330 |
void loadChests() { |
void loadChests() { |
331 |
int count = loadChestsWorker(); |
int count = loadChestsWorker(); |
332 |
plugin.getLogger().info("[LockedChest] loaded " + count + " chests"); |
server.getLogger().info("[AdvancedChest] loaded " + count + " chests"); |
333 |
} |
} |
334 |
|
|
335 |
|
|
336 |
public LockedChestBean createChest(String owner, String description, Location loc) { |
public ChestBean createChest(String owner, String description, Location loc) { |
337 |
|
|
338 |
LockedChestBean chest = new LockedChestBean(); |
ChestBean chest = new ChestBean(); |
339 |
chest.setOwner(owner); |
chest.setOwner(owner); |
340 |
chest.setDescription(description); |
chest.setDescription(description); |
341 |
setChestLocation(chest, loc); |
setChestLocation(chest, loc); |
344 |
} |
} |
345 |
|
|
346 |
|
|
347 |
public void setChestLocation(LockedChestBean chest, Location loc) { |
public void setChestLocation(ChestBean chest, Location loc) { |
348 |
chest.setWorld( loc.getWorld().getName() ); |
chest.setWorld( loc.getWorld().getName() ); |
349 |
chest.setX( loc.getBlockX() ); |
chest.setX( loc.getBlockX() ); |
350 |
chest.setY( loc.getBlockY() ); |
chest.setY( loc.getBlockY() ); |
351 |
chest.setZ( loc.getBlockZ() ); |
chest.setZ( loc.getBlockZ() ); |
352 |
} |
} |
353 |
|
|
354 |
public Location getChestLocation(Server server, LockedChestBean chest) { |
public Location getChestLocation(Server server, ChestBean chest) { |
355 |
World wrld = server.getWorld(chest.getWorld()); |
World wrld = server.getWorld(chest.getWorld()); |
356 |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
357 |
} |
} |
409 |
|
|
410 |
Location chestloc = getNeighborChest( block.getLocation() ); |
Location chestloc = getNeighborChest( block.getLocation() ); |
411 |
if (chestloc != null) { |
if (chestloc != null) { |
412 |
LockedChestBean chest = chestMap.get(chestloc); |
ChestBean chest = chestMap.get(chestloc); |
413 |
|
|
414 |
if (chest != null) { //the neighbor is a locked chest |
if (chest != null) { //the neighbor is a locked chest |
415 |
|
|
418 |
addChest(chestloc, chest); |
addChest(chestloc, chest); |
419 |
|
|
420 |
|
|
421 |
event.getPlayer().sendMessage( "[LockedChest] Chest has been expanded" ); |
event.getPlayer().sendMessage( "[AdvancedChest] Chest has been expanded" ); |
422 |
} |
} |
423 |
|
|
424 |
} |
} |
428 |
@EventHandler |
@EventHandler |
429 |
public void onChestExplode(EntityExplodeEvent event) { |
public void onChestExplode(EntityExplodeEvent event) { |
430 |
for (Block b : event.blockList() ) { |
for (Block b : event.blockList() ) { |
431 |
LockedChestBean chest = chestMap.get( b.getLocation() ); |
ChestBean chest = chestMap.get( b.getLocation() ); |
432 |
if (chest != null) { |
if (chest != null) { |
433 |
|
server.getLogger().info( "[AdvancedChest] Prevented an explosion from destroying chest owned by " + chest.getOwner() + chest.getCommentString() ); |
434 |
event.setCancelled( true ); |
event.setCancelled( true ); |
435 |
return; |
return; |
436 |
} |
} |
437 |
} |
} |
438 |
} |
} |
439 |
|
|
440 |
|
@EventHandler |
441 |
|
public void onChestBurn(BlockBurnEvent event) { |
442 |
|
ChestBean chest = chestMap.get( event.getBlock().getLocation() ); |
443 |
|
if (chest != null) { |
444 |
|
server.getLogger().info( "[AdvancedChest] prevented a fire from destrying chest owned by " + chest.getOwner() + chest.getCommentString() ); |
445 |
|
event.setCancelled( true); |
446 |
|
} |
447 |
|
} |
448 |
|
|
449 |
// prevent a user from opening a chest |
// prevent a user from opening a chest |
450 |
@EventHandler |
@EventHandler |
456 |
|
|
457 |
Location loc = b.getLocation(); |
Location loc = b.getLocation(); |
458 |
|
|
459 |
LockedChestBean chest = chestMap.get( loc ); |
ChestBean chest = chestMap.get( loc ); |
460 |
if (chest == null) { |
if (chest == null) { |
461 |
return; //chest not surveyed by this plugin |
return; //chest not surveyed by this plugin |
462 |
} |
} |
463 |
|
|
464 |
|
if (chest.getChestType() != ChestBean.LOCKED ) { |
465 |
|
return; //this is not a locked chests |
466 |
|
} |
467 |
|
|
468 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
469 |
if (player.getName().equals(chest.getOwner() )) { |
if (player.getName().equals(chest.getOwner() )) { |
470 |
return; //chest is opened by it's owner |
return; //chest is opened by it's owner |
471 |
} |
} |
472 |
|
|
473 |
if (chest.getModifyPlayers() != null && chest.getModifyPlayers().length()>0) { |
Set<String> players = chest.getModifyPlayersSet() ; |
474 |
String modplayers[] = chest.getModifyPlayers().split(","); |
if ( players.contains(player.getName()) ) { |
475 |
for (String p : modplayers) { |
return; //this player is on the whitelist so he may open |
|
if ( player.getName().equals(p) ) { |
|
|
return; //this player is on the whitelist so he may open |
|
|
} |
|
|
} |
|
476 |
} |
} |
477 |
|
|
478 |
|
|
479 |
|
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " tried opening a chest owned by " + chest.getOwner() + chest.getCommentString() ); |
480 |
player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); |
player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); |
481 |
event.setCancelled(true); |
event.setCancelled(true); |
482 |
} |
} |
483 |
} |
} |
484 |
} |
} |
485 |
|
|
486 |
|
@EventHandler |
487 |
|
public void onChestOpen(InventoryOpenEvent event) { |
488 |
|
|
489 |
|
if (! (event.getPlayer() instanceof Player)) { |
490 |
|
return; |
491 |
|
} |
492 |
|
|
493 |
|
|
494 |
|
|
495 |
|
InventoryHolder holder = event.getInventory().getHolder(); |
496 |
|
if (holder instanceof Chest || holder instanceof DoubleChest) { |
497 |
|
Location loc = getChestLocation(holder); |
498 |
|
|
499 |
|
ChestBean chest = chestMap.get( loc ); |
500 |
|
if (chest == null) { |
501 |
|
return; //chest not surveyed by this plugin |
502 |
|
} |
503 |
|
|
504 |
|
if (chest.getChestType() != ChestBean.SNITCHING) { |
505 |
|
return; // not a snitching chest |
506 |
|
} |
507 |
|
|
508 |
|
|
509 |
|
Player player = (Player) event.getPlayer(); |
510 |
|
if (player.getName().equals(chest.getOwner() )) { |
511 |
|
return; //chest is owned by it's own player |
512 |
|
} |
513 |
|
|
514 |
|
Set<String> players = chest.getModifyPlayersSet(); |
515 |
|
if ( players.contains(player.getName()) ) { |
516 |
|
return; //this player is on the whitelist so he may open |
517 |
|
} |
518 |
|
|
519 |
|
|
520 |
|
server.getLogger().info( "[AdvancedChest] " + event.getPlayer().getName() + " opened a snitching chest owned by " + chest.getOwner() + chest.getCommentString() ); |
521 |
|
|
522 |
|
ItemCount contents = countItems( event.getInventory().getContents() ); |
523 |
|
|
524 |
|
contentMap.put(player.getName(), contents ); |
525 |
|
} |
526 |
|
} |
527 |
|
|
528 |
|
@EventHandler |
529 |
|
public void onChestClose(InventoryCloseEvent event) { |
530 |
|
if (! (event.getPlayer() instanceof Player)) { |
531 |
|
return; |
532 |
|
} |
533 |
|
|
534 |
|
|
535 |
|
InventoryHolder holder = event.getInventory().getHolder(); |
536 |
|
if (holder instanceof Chest || holder instanceof DoubleChest) { |
537 |
|
Location loc = getChestLocation(holder); |
538 |
|
ChestBean chest = chestMap.get(loc); |
539 |
|
|
540 |
|
if (chest == null) { //chest was not a snitching chest |
541 |
|
return; |
542 |
|
} |
543 |
|
|
544 |
|
if (chest.getChestType() != ChestBean.SNITCHING) { |
545 |
|
return; // not a snitching chest |
546 |
|
} |
547 |
|
|
548 |
|
|
549 |
|
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
550 |
|
|
551 |
|
|
552 |
|
Player player = (Player) event.getPlayer(); |
553 |
|
|
554 |
|
ItemCount savedContent = contentMap.get( player.getName() ); |
555 |
|
|
556 |
|
if (savedContent == null) { |
557 |
|
return; |
558 |
|
} |
559 |
|
|
560 |
|
contentMap.remove( player.getName() ); |
561 |
|
|
562 |
|
ItemCount content = countItems( event.getInventory().getContents() ); |
563 |
|
|
564 |
|
Set<Integer> combinedKeyset = new TreeSet<Integer>(); |
565 |
|
combinedKeyset.addAll( savedContent.keySet() ); |
566 |
|
combinedKeyset.addAll( content.keySet() ); |
567 |
|
|
568 |
|
for (Integer item : combinedKeyset ) { |
569 |
|
Integer savedcount = savedContent.get(item); |
570 |
|
Integer count = content.get(item); |
571 |
|
|
572 |
|
if (savedcount == null) |
573 |
|
savedcount = 0; |
574 |
|
if (count == null) |
575 |
|
count = 0; |
576 |
|
|
577 |
|
|
578 |
|
int diff = Math.abs( savedcount - count); |
579 |
|
|
580 |
|
if (diff > 0) { |
581 |
|
String material = Material.getMaterial(item).name(); |
582 |
|
String msg = null; |
583 |
|
|
584 |
|
if (count > savedcount) { |
585 |
|
msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
586 |
|
} else { //(count < savedcount) |
587 |
|
msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
588 |
|
} |
589 |
|
|
590 |
|
|
591 |
|
server.getLogger().info( "[AdvancedChest]" + msg); |
592 |
|
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
593 |
|
} |
594 |
|
|
595 |
|
} |
596 |
|
|
597 |
|
|
598 |
|
} |
599 |
|
} |
600 |
|
|
601 |
|
ItemCount countItems(ItemStack[] input) { |
602 |
|
ItemCount output = new ItemCount(); |
603 |
|
for (int i=0; i<input.length; i++) { |
604 |
|
ItemStack current = input[i]; |
605 |
|
if (current == null) |
606 |
|
continue; |
607 |
|
|
608 |
|
int type = current.getTypeId(); |
609 |
|
|
610 |
|
Integer amount = output.get(type); |
611 |
|
if (amount == null) |
612 |
|
amount = 0; |
613 |
|
|
614 |
|
output.put(type, amount + current.getAmount() ); |
615 |
|
} |
616 |
|
return output; |
617 |
|
} |
618 |
|
/* |
619 |
|
ItemStack[] cloneItemStacks(ItemStack[] input) { |
620 |
|
ItemStack[] output = new ItemStack[ input.length ]; |
621 |
|
for (int i=0; i<input.length; i++) { |
622 |
|
if (input[i] != null) { |
623 |
|
output[i] = input[i].clone(); |
624 |
|
} else { |
625 |
|
output[i] = new ItemStack(0, 0); |
626 |
|
} |
627 |
|
} |
628 |
|
return output; |
629 |
|
}*/ |
630 |
|
|
631 |
|
|
632 |
} |
} |