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