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