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