1 |
torben |
1804 |
package dk.thoerup.bukkit.hoeruputils.chests; |
2 |
torben |
1768 |
|
3 |
|
|
|
4 |
|
|
import java.util.HashMap; |
5 |
torben |
1773 |
import java.util.List; |
6 |
torben |
1768 |
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 |
torben |
1770 |
import org.bukkit.OfflinePlayer; |
13 |
|
|
import org.bukkit.Server; |
14 |
torben |
1768 |
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 |
torben |
1788 |
import org.bukkit.event.block.Action; |
25 |
torben |
1768 |
import org.bukkit.event.block.BlockBreakEvent; |
26 |
torben |
1780 |
import org.bukkit.event.block.BlockPlaceEvent; |
27 |
torben |
1768 |
import org.bukkit.event.inventory.InventoryCloseEvent; |
28 |
|
|
import org.bukkit.event.inventory.InventoryOpenEvent; |
29 |
torben |
1788 |
import org.bukkit.event.player.PlayerInteractEvent; |
30 |
torben |
1768 |
import org.bukkit.inventory.InventoryHolder; |
31 |
|
|
import org.bukkit.inventory.ItemStack; |
32 |
|
|
|
33 |
torben |
1804 |
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
34 |
torben |
1770 |
|
35 |
torben |
1804 |
|
36 |
torben |
1768 |
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 |
torben |
1773 |
HashMap<Location,SnitchingChestBean> chestMap = new HashMap<Location, SnitchingChestBean>(); |
45 |
torben |
1768 |
|
46 |
|
|
|
47 |
torben |
1770 |
HoerupUtilsPlugin plugin; |
48 |
|
|
Server server; |
49 |
torben |
1768 |
|
50 |
torben |
1773 |
public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) { |
51 |
torben |
1768 |
this.plugin = plugin; |
52 |
torben |
1770 |
server = plugin.getServer(); |
53 |
torben |
1773 |
try { |
54 |
|
|
loadChests(); |
55 |
|
|
} catch (Exception e) { |
56 |
|
|
e.printStackTrace(); |
57 |
|
|
//r.run(); |
58 |
|
|
loadChests(); |
59 |
|
|
} |
60 |
torben |
1768 |
} |
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 |
torben |
1773 |
SnitchingChestBean chest = chestMap.get(loc); |
84 |
|
|
if (chest != null) { |
85 |
|
|
if (chest.getOwner().equals(player.getName())) { |
86 |
torben |
1768 |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
87 |
torben |
1773 |
removeChest(loc); |
88 |
torben |
1768 |
} else { |
89 |
|
|
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
return true; |
93 |
|
|
} |
94 |
|
|
|
95 |
torben |
1785 |
chest = createChest(player.getName(), "", loc); |
96 |
torben |
1768 |
if (loc2 != null) { |
97 |
torben |
1785 |
chest.setDoublechest(true); |
98 |
torben |
1768 |
} |
99 |
torben |
1785 |
addChest(loc, chest); |
100 |
|
|
|
101 |
torben |
1768 |
|
102 |
|
|
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
103 |
|
|
|
104 |
torben |
1773 |
|
105 |
torben |
1768 |
return true; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
@EventHandler |
109 |
|
|
public void onBlockBreak(BlockBreakEvent event) { |
110 |
|
|
Location loc = event.getBlock().getLocation(); |
111 |
torben |
1773 |
SnitchingChestBean chest = chestMap.get(loc); |
112 |
|
|
if (chest != null) { |
113 |
|
|
if (chest.getOwner().equals(event.getPlayer().getName())) { |
114 |
|
|
removeChest(loc); |
115 |
torben |
1768 |
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 |
torben |
1773 |
public void addChest(Location loc, SnitchingChestBean chest) { |
123 |
|
|
chestMap.put(loc, chest); |
124 |
torben |
1785 |
if (chest.isDoublechest()) { |
125 |
|
|
Location loc2 = getNeighborChest(loc); |
126 |
|
|
chestMap.put(loc2, chest); |
127 |
|
|
} |
128 |
torben |
1773 |
plugin.getDatabase().save(chest); |
129 |
torben |
1785 |
|
130 |
|
|
reloadChests(); |
131 |
|
|
|
132 |
torben |
1773 |
} |
133 |
torben |
1768 |
|
134 |
torben |
1773 |
void removeChest(Location loc) { |
135 |
|
|
SnitchingChestBean chest = chestMap.remove(loc); |
136 |
torben |
1785 |
if (chest != null) { |
137 |
|
|
if (chest.isDoublechest()){ |
138 |
|
|
Location loc2 = getNeighborChest(loc); |
139 |
|
|
chestMap.remove(loc2); |
140 |
|
|
} |
141 |
torben |
1777 |
plugin.getDatabase().delete(chest); |
142 |
|
|
} |
143 |
torben |
1773 |
} |
144 |
|
|
|
145 |
torben |
1785 |
int loadChestsWorker() { |
146 |
torben |
1773 |
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 |
torben |
1785 |
|
151 |
|
|
if (chest.isDoublechest()) { |
152 |
|
|
Location loc2 = getNeighborChest(loc); |
153 |
|
|
chestMap.put(loc2, chest); |
154 |
|
|
} |
155 |
torben |
1773 |
} |
156 |
torben |
1785 |
|
157 |
|
|
return chestlist.size(); |
158 |
torben |
1773 |
} |
159 |
|
|
|
160 |
torben |
1785 |
void reloadChests() { |
161 |
|
|
chestMap.clear(); |
162 |
|
|
loadChestsWorker(); |
163 |
|
|
} |
164 |
torben |
1773 |
|
165 |
torben |
1785 |
void loadChests() { |
166 |
|
|
int count = loadChestsWorker(); |
167 |
|
|
plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests"); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
|
171 |
torben |
1773 |
public SnitchingChestBean createChest(String owner, String description, Location loc) { |
172 |
torben |
1768 |
|
173 |
torben |
1773 |
SnitchingChestBean chest = new SnitchingChestBean(); |
174 |
|
|
chest.setOwner(owner); |
175 |
|
|
chest.setDescription(description); |
176 |
|
|
setChestLocation(chest, loc); |
177 |
|
|
|
178 |
|
|
return chest; |
179 |
torben |
1768 |
} |
180 |
|
|
|
181 |
torben |
1773 |
|
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 |
torben |
1768 |
void saveChests() { |
197 |
|
|
|
198 |
torben |
1773 |
}*/ |
199 |
torben |
1768 |
|
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 |
torben |
1779 |
|
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 |
torben |
1768 |
return loc; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
@EventHandler |
238 |
torben |
1780 |
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 |
torben |
1785 |
//SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc); |
251 |
|
|
//plugin.getDatabase().save(chest); |
252 |
|
|
|
253 |
|
|
chest.setDoublechest(true); |
254 |
|
|
addChest(chestloc, chest); |
255 |
|
|
|
256 |
|
|
|
257 |
torben |
1780 |
event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" ); |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
} |
261 |
|
|
} |
262 |
|
|
|
263 |
torben |
1788 |
/* |
264 |
|
|
* how to prevent a user from opening a chest - usefull if SnitchingChest should morph into a LockedChest |
265 |
torben |
1780 |
@EventHandler |
266 |
torben |
1788 |
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 |
torben |
1768 |
public void onChestOpen(InventoryOpenEvent event) { |
279 |
|
|
|
280 |
|
|
if (! (event.getPlayer() instanceof Player)) { |
281 |
|
|
return; |
282 |
|
|
} |
283 |
|
|
|
284 |
torben |
1788 |
|
285 |
torben |
1768 |
|
286 |
|
|
InventoryHolder holder = event.getInventory().getHolder(); |
287 |
|
|
if (holder instanceof Chest || holder instanceof DoubleChest) { |
288 |
|
|
Location loc = getChestLocation(holder); |
289 |
|
|
|
290 |
torben |
1773 |
SnitchingChestBean chest = chestMap.get( loc ); |
291 |
|
|
if (chest == null) { |
292 |
torben |
1768 |
return; //chest not surveyed by this plugin |
293 |
|
|
} |
294 |
|
|
|
295 |
|
|
|
296 |
|
|
Player player = (Player) event.getPlayer(); |
297 |
torben |
1773 |
if (player.getName().equals(chest.getOwner() )) { |
298 |
torben |
1768 |
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 |
torben |
1773 |
SnitchingChestBean chest = chestMap.get(loc); |
318 |
torben |
1775 |
|
319 |
|
|
if (chest == null) { //chest was not a snitching chest |
320 |
|
|
return; |
321 |
|
|
} |
322 |
|
|
|
323 |
torben |
1773 |
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
324 |
torben |
1768 |
|
325 |
torben |
1769 |
|
326 |
torben |
1768 |
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 |
torben |
1771 |
|
353 |
torben |
1782 |
if (diff > 0) { |
354 |
|
|
String material = Material.getMaterial(item).name(); |
355 |
|
|
String msg = null; |
356 |
|
|
|
357 |
|
|
if (count > savedcount) { |
358 |
torben |
1784 |
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 |
torben |
1782 |
} else { //(count < savedcount) |
360 |
torben |
1784 |
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 |
torben |
1782 |
} |
362 |
|
|
|
363 |
|
|
|
364 |
torben |
1768 |
plugin.getLogger().info(msg); |
365 |
torben |
1803 |
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
366 |
torben |
1768 |
} |
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 |
|
|
} |