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