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