1 |
package dk.thoerup.bukkit.hoeruputils.chests; |
2 |
|
3 |
|
4 |
import java.util.HashMap; |
5 |
import java.util.List; |
6 |
import java.util.Set; |
7 |
import java.util.TreeMap; |
8 |
import java.util.TreeSet; |
9 |
|
10 |
import org.bukkit.Location; |
11 |
import org.bukkit.Material; |
12 |
import org.bukkit.OfflinePlayer; |
13 |
import org.bukkit.Server; |
14 |
import org.bukkit.World; |
15 |
import org.bukkit.block.Block; |
16 |
import org.bukkit.block.Chest; |
17 |
import org.bukkit.block.DoubleChest; |
18 |
import org.bukkit.command.Command; |
19 |
import org.bukkit.command.CommandExecutor; |
20 |
import org.bukkit.command.CommandSender; |
21 |
import org.bukkit.entity.Player; |
22 |
import org.bukkit.event.EventHandler; |
23 |
import org.bukkit.event.Listener; |
24 |
import org.bukkit.event.block.Action; |
25 |
import org.bukkit.event.block.BlockBreakEvent; |
26 |
import org.bukkit.event.block.BlockPlaceEvent; |
27 |
import org.bukkit.event.inventory.InventoryCloseEvent; |
28 |
import org.bukkit.event.inventory.InventoryOpenEvent; |
29 |
import org.bukkit.event.player.PlayerInteractEvent; |
30 |
import org.bukkit.inventory.InventoryHolder; |
31 |
import org.bukkit.inventory.ItemStack; |
32 |
|
33 |
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
34 |
|
35 |
|
36 |
public class SnitchingChest implements Listener, CommandExecutor{ |
37 |
|
38 |
class ItemCount extends TreeMap<Integer,Integer> { |
39 |
private static final long serialVersionUID = 1L; |
40 |
}; |
41 |
|
42 |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
43 |
|
44 |
HashMap<Location,SnitchingChestBean> chestMap = new HashMap<Location, SnitchingChestBean>(); |
45 |
|
46 |
|
47 |
HoerupUtilsPlugin plugin; |
48 |
Server server; |
49 |
|
50 |
public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) { |
51 |
this.plugin = plugin; |
52 |
server = plugin.getServer(); |
53 |
try { |
54 |
loadChests(); |
55 |
} catch (Exception e) { |
56 |
e.printStackTrace(); |
57 |
//r.run(); |
58 |
loadChests(); |
59 |
} |
60 |
} |
61 |
|
62 |
|
63 |
@Override |
64 |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
65 |
if (! (sender instanceof Player) ) { |
66 |
sender.sendMessage("this is not a console command!"); |
67 |
return true; |
68 |
} |
69 |
|
70 |
Player player = (Player) sender; |
71 |
|
72 |
|
73 |
Block b = player.getTargetBlock(null, 30); |
74 |
Location loc = b.getLocation(); |
75 |
|
76 |
if (b.getTypeId() != 54) { |
77 |
player.sendMessage("[SnitchingChest] Please look at the chest you want to be a snitch"); |
78 |
return true; |
79 |
} |
80 |
|
81 |
Location loc2 = getNeighborChest(loc); |
82 |
|
83 |
SnitchingChestBean chest = chestMap.get(loc); |
84 |
if (chest != null) { |
85 |
if (chest.getOwner().equals(player.getName())) { |
86 |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
87 |
removeChest(loc); |
88 |
} else { |
89 |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
90 |
} |
91 |
|
92 |
return true; |
93 |
} |
94 |
|
95 |
chest = createChest(player.getName(), "", loc); |
96 |
if (loc2 != null) { |
97 |
chest.setDoublechest(true); |
98 |
} |
99 |
addChest(loc, chest); |
100 |
|
101 |
|
102 |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
103 |
|
104 |
|
105 |
return true; |
106 |
} |
107 |
|
108 |
@EventHandler |
109 |
public void onBlockBreak(BlockBreakEvent event) { |
110 |
Location loc = event.getBlock().getLocation(); |
111 |
SnitchingChestBean chest = chestMap.get(loc); |
112 |
if (chest != null) { |
113 |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
114 |
removeChest(loc); |
115 |
event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance"); |
116 |
} else { |
117 |
event.setCancelled(true); |
118 |
event.getPlayer().sendMessage("You can't destroy that chest"); |
119 |
} |
120 |
} |
121 |
} |
122 |
public void addChest(Location loc, SnitchingChestBean chest) { |
123 |
chestMap.put(loc, chest); |
124 |
if (chest.isDoublechest()) { |
125 |
Location loc2 = getNeighborChest(loc); |
126 |
chestMap.put(loc2, chest); |
127 |
} |
128 |
plugin.getDatabase().save(chest); |
129 |
|
130 |
reloadChests(); |
131 |
|
132 |
} |
133 |
|
134 |
void removeChest(Location loc) { |
135 |
SnitchingChestBean chest = chestMap.remove(loc); |
136 |
if (chest != null) { |
137 |
if (chest.isDoublechest()){ |
138 |
Location loc2 = getNeighborChest(loc); |
139 |
chestMap.remove(loc2); |
140 |
} |
141 |
plugin.getDatabase().delete(chest); |
142 |
} |
143 |
} |
144 |
|
145 |
int loadChestsWorker() { |
146 |
List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList(); |
147 |
for (SnitchingChestBean chest : chestlist) { |
148 |
Location loc = getChestLocation(server, chest); |
149 |
chestMap.put(loc, chest); |
150 |
|
151 |
if (chest.isDoublechest()) { |
152 |
Location loc2 = getNeighborChest(loc); |
153 |
chestMap.put(loc2, chest); |
154 |
} |
155 |
} |
156 |
|
157 |
return chestlist.size(); |
158 |
} |
159 |
|
160 |
void reloadChests() { |
161 |
chestMap.clear(); |
162 |
loadChestsWorker(); |
163 |
} |
164 |
|
165 |
void loadChests() { |
166 |
int count = loadChestsWorker(); |
167 |
plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests"); |
168 |
} |
169 |
|
170 |
|
171 |
public SnitchingChestBean createChest(String owner, String description, Location loc) { |
172 |
|
173 |
SnitchingChestBean chest = new SnitchingChestBean(); |
174 |
chest.setOwner(owner); |
175 |
chest.setDescription(description); |
176 |
setChestLocation(chest, loc); |
177 |
|
178 |
return chest; |
179 |
} |
180 |
|
181 |
|
182 |
public void setChestLocation(SnitchingChestBean chest, Location loc) { |
183 |
chest.setWorld( loc.getWorld().getName() ); |
184 |
chest.setX( loc.getBlockX() ); |
185 |
chest.setY( loc.getBlockY() ); |
186 |
chest.setZ( loc.getBlockZ() ); |
187 |
} |
188 |
|
189 |
public Location getChestLocation(Server server, SnitchingChestBean chest) { |
190 |
World wrld = server.getWorld(chest.getWorld()); |
191 |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
192 |
} |
193 |
|
194 |
|
195 |
/* |
196 |
void saveChests() { |
197 |
|
198 |
}*/ |
199 |
|
200 |
Location getNeighborChest(Location loc) { |
201 |
World world = loc.getWorld(); |
202 |
|
203 |
Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() ); |
204 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
205 |
return target; |
206 |
|
207 |
target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() ); |
208 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
209 |
return target; |
210 |
|
211 |
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() +1); |
212 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
213 |
return target; |
214 |
|
215 |
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1); |
216 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
217 |
return target; |
218 |
|
219 |
return null; |
220 |
} |
221 |
|
222 |
|
223 |
Location getChestLocation(InventoryHolder holder) { |
224 |
Location loc; |
225 |
if ( holder instanceof Chest) { |
226 |
loc = ( (Chest)holder).getLocation(); |
227 |
} else { |
228 |
loc = ( (DoubleChest)holder).getLocation(); |
229 |
} |
230 |
|
231 |
loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5 |
232 |
loc.setZ( loc.getBlockZ() ); // -- // -- |
233 |
|
234 |
return loc; |
235 |
} |
236 |
|
237 |
@EventHandler |
238 |
public void onChestPlaced(BlockPlaceEvent event) { |
239 |
Block block = event.getBlock(); |
240 |
|
241 |
if (block.getType() != Material.CHEST) { |
242 |
return; |
243 |
} |
244 |
|
245 |
Location chestloc = getNeighborChest( block.getLocation() ); |
246 |
if (chestloc != null) { |
247 |
SnitchingChestBean chest = chestMap.get(chestloc); |
248 |
|
249 |
if (chest != null) { //the neighbor is a snitching chest |
250 |
//SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc); |
251 |
//plugin.getDatabase().save(chest); |
252 |
|
253 |
chest.setDoublechest(true); |
254 |
addChest(chestloc, chest); |
255 |
|
256 |
|
257 |
event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" ); |
258 |
} |
259 |
|
260 |
} |
261 |
} |
262 |
|
263 |
/* |
264 |
* how to prevent a user from opening a chest - usefull if SnitchingChest should morph into a LockedChest |
265 |
@EventHandler |
266 |
public void onChestInteract(PlayerInteractEvent event) { |
267 |
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { |
268 |
Block b = event.getClickedBlock(); |
269 |
|
270 |
if (b.getType() == Material.CHEST) { |
271 |
event.setCancelled(true); |
272 |
} |
273 |
} |
274 |
} |
275 |
*/ |
276 |
|
277 |
@EventHandler |
278 |
public void onChestOpen(InventoryOpenEvent event) { |
279 |
|
280 |
if (! (event.getPlayer() instanceof Player)) { |
281 |
return; |
282 |
} |
283 |
|
284 |
|
285 |
|
286 |
InventoryHolder holder = event.getInventory().getHolder(); |
287 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
288 |
Location loc = getChestLocation(holder); |
289 |
|
290 |
SnitchingChestBean chest = chestMap.get( loc ); |
291 |
if (chest == null) { |
292 |
return; //chest not surveyed by this plugin |
293 |
} |
294 |
|
295 |
|
296 |
Player player = (Player) event.getPlayer(); |
297 |
if (player.getName().equals(chest.getOwner() )) { |
298 |
return; //chest is owned by it's own player |
299 |
} |
300 |
|
301 |
ItemCount contents = countItems( event.getInventory().getContents() ); |
302 |
|
303 |
contentMap.put(player.getName(), contents ); |
304 |
} |
305 |
} |
306 |
|
307 |
@EventHandler |
308 |
public void onChestClose(InventoryCloseEvent event) { |
309 |
if (! (event.getPlayer() instanceof Player)) { |
310 |
return; |
311 |
} |
312 |
|
313 |
|
314 |
InventoryHolder holder = event.getInventory().getHolder(); |
315 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
316 |
Location loc = getChestLocation(holder); |
317 |
SnitchingChestBean chest = chestMap.get(loc); |
318 |
|
319 |
if (chest == null) { //chest was not a snitching chest |
320 |
return; |
321 |
} |
322 |
|
323 |
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
324 |
|
325 |
|
326 |
Player player = (Player) event.getPlayer(); |
327 |
|
328 |
ItemCount savedContent = contentMap.get( player.getName() ); |
329 |
|
330 |
if (savedContent == null) { |
331 |
return; |
332 |
} |
333 |
contentMap.remove( player.getName() ); |
334 |
|
335 |
ItemCount content = countItems( event.getInventory().getContents() ); |
336 |
|
337 |
Set<Integer> combinedKeyset = new TreeSet<Integer>(); |
338 |
combinedKeyset.addAll( savedContent.keySet() ); |
339 |
combinedKeyset.addAll( content.keySet() ); |
340 |
|
341 |
for (Integer item : combinedKeyset ) { |
342 |
Integer savedcount = savedContent.get(item); |
343 |
Integer count = content.get(item); |
344 |
|
345 |
if (savedcount == null) |
346 |
savedcount = 0; |
347 |
if (count == null) |
348 |
count = 0; |
349 |
|
350 |
|
351 |
int diff = Math.abs( savedcount - count); |
352 |
|
353 |
if (diff > 0) { |
354 |
String material = Material.getMaterial(item).name(); |
355 |
String msg = null; |
356 |
|
357 |
if (count > savedcount) { |
358 |
msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
359 |
} else { //(count < savedcount) |
360 |
msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
361 |
} |
362 |
|
363 |
|
364 |
plugin.getLogger().info(msg); |
365 |
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
366 |
} |
367 |
|
368 |
} |
369 |
|
370 |
} |
371 |
} |
372 |
|
373 |
ItemCount countItems(ItemStack[] input) { |
374 |
ItemCount output = new ItemCount(); |
375 |
for (int i=0; i<input.length; i++) { |
376 |
ItemStack current = input[i]; |
377 |
if (current == null) |
378 |
continue; |
379 |
|
380 |
int type = current.getTypeId(); |
381 |
|
382 |
Integer amount = output.get(type); |
383 |
if (amount == null) |
384 |
amount = 0; |
385 |
|
386 |
output.put(type, amount + current.getAmount() ); |
387 |
} |
388 |
return output; |
389 |
} |
390 |
/* |
391 |
ItemStack[] cloneItemStacks(ItemStack[] input) { |
392 |
ItemStack[] output = new ItemStack[ input.length ]; |
393 |
for (int i=0; i<input.length; i++) { |
394 |
if (input[i] != null) { |
395 |
output[i] = input[i].clone(); |
396 |
} else { |
397 |
output[i] = new ItemStack(0, 0); |
398 |
} |
399 |
} |
400 |
return output; |
401 |
}*/ |
402 |
|
403 |
|
404 |
} |