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 |
//chestMap.put(loc, chest1); |
95 |
addChest(loc, chest1); |
96 |
if (loc2 != null) { |
97 |
|
98 |
SnitchingChestBean chest2 = createChest (player.getName(), "", loc2); |
99 |
addChest(loc, chest2); |
100 |
//chestMap.put(loc2, chest2 ); |
101 |
} |
102 |
|
103 |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
104 |
|
105 |
|
106 |
return true; |
107 |
} |
108 |
|
109 |
@EventHandler |
110 |
public void onBlockBreak(BlockBreakEvent event) { |
111 |
Location loc = event.getBlock().getLocation(); |
112 |
SnitchingChestBean chest = chestMap.get(loc); |
113 |
if (chest != null) { |
114 |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
115 |
removeChest(loc); |
116 |
event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance"); |
117 |
} else { |
118 |
event.setCancelled(true); |
119 |
event.getPlayer().sendMessage("You can't destroy that chest"); |
120 |
} |
121 |
} |
122 |
} |
123 |
public void addChest(Location loc, SnitchingChestBean chest) { |
124 |
chestMap.put(loc, chest); |
125 |
plugin.getDatabase().save(chest); |
126 |
} |
127 |
|
128 |
void removeChest(Location loc) { |
129 |
SnitchingChestBean chest = chestMap.remove(loc); |
130 |
plugin.getDatabase().delete(chest); |
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 |
|
141 |
|
142 |
public SnitchingChestBean createChest(String owner, String description, Location loc) { |
143 |
|
144 |
SnitchingChestBean chest = new SnitchingChestBean(); |
145 |
chest.setOwner(owner); |
146 |
chest.setDescription(description); |
147 |
setChestLocation(chest, loc); |
148 |
|
149 |
return chest; |
150 |
} |
151 |
|
152 |
|
153 |
public void setChestLocation(SnitchingChestBean chest, Location loc) { |
154 |
chest.setWorld( loc.getWorld().getName() ); |
155 |
chest.setX( loc.getBlockX() ); |
156 |
chest.setY( loc.getBlockY() ); |
157 |
chest.setZ( loc.getBlockZ() ); |
158 |
} |
159 |
|
160 |
public Location getChestLocation(Server server, SnitchingChestBean chest) { |
161 |
World wrld = server.getWorld(chest.getWorld()); |
162 |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
163 |
} |
164 |
|
165 |
|
166 |
/* |
167 |
void saveChests() { |
168 |
|
169 |
}*/ |
170 |
|
171 |
Location getNeighborChest(Location loc) { |
172 |
World world = loc.getWorld(); |
173 |
|
174 |
Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() ); |
175 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
176 |
return target; |
177 |
|
178 |
target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() ); |
179 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
180 |
return target; |
181 |
|
182 |
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() +1); |
183 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
184 |
return target; |
185 |
|
186 |
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1); |
187 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
188 |
return target; |
189 |
|
190 |
return null; |
191 |
} |
192 |
|
193 |
|
194 |
Location getChestLocation(InventoryHolder holder) { |
195 |
Location loc; |
196 |
if ( holder instanceof Chest) { |
197 |
loc = ( (Chest)holder).getLocation(); |
198 |
} else { |
199 |
loc = ( (DoubleChest)holder).getLocation(); |
200 |
} |
201 |
return loc; |
202 |
} |
203 |
|
204 |
@EventHandler |
205 |
public void onChestOpen(InventoryOpenEvent event) { |
206 |
|
207 |
if (! (event.getPlayer() instanceof Player)) { |
208 |
return; |
209 |
} |
210 |
|
211 |
|
212 |
InventoryHolder holder = event.getInventory().getHolder(); |
213 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
214 |
Location loc = getChestLocation(holder); |
215 |
|
216 |
SnitchingChestBean chest = chestMap.get( loc ); |
217 |
if (chest == null) { |
218 |
return; //chest not surveyed by this plugin |
219 |
} |
220 |
|
221 |
|
222 |
Player player = (Player) event.getPlayer(); |
223 |
if (player.getName().equals(chest.getOwner() )) { |
224 |
return; //chest is owned by it's own player |
225 |
} |
226 |
|
227 |
ItemCount contents = countItems( event.getInventory().getContents() ); |
228 |
|
229 |
contentMap.put(player.getName(), contents ); |
230 |
} |
231 |
} |
232 |
|
233 |
@EventHandler |
234 |
public void onChestClose(InventoryCloseEvent event) { |
235 |
if (! (event.getPlayer() instanceof Player)) { |
236 |
return; |
237 |
} |
238 |
|
239 |
|
240 |
InventoryHolder holder = event.getInventory().getHolder(); |
241 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
242 |
Location loc = getChestLocation(holder); |
243 |
SnitchingChestBean chest = chestMap.get(loc); |
244 |
|
245 |
if (chest == null) { //chest was not a snitching chest |
246 |
return; |
247 |
} |
248 |
|
249 |
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
250 |
|
251 |
|
252 |
Player player = (Player) event.getPlayer(); |
253 |
|
254 |
ItemCount savedContent = contentMap.get( player.getName() ); |
255 |
|
256 |
if (savedContent == null) { |
257 |
return; |
258 |
} |
259 |
contentMap.remove( player.getName() ); |
260 |
|
261 |
ItemCount content = countItems( event.getInventory().getContents() ); |
262 |
|
263 |
Set<Integer> combinedKeyset = new TreeSet<Integer>(); |
264 |
combinedKeyset.addAll( savedContent.keySet() ); |
265 |
combinedKeyset.addAll( content.keySet() ); |
266 |
|
267 |
for (Integer item : combinedKeyset ) { |
268 |
Integer savedcount = savedContent.get(item); |
269 |
Integer count = content.get(item); |
270 |
|
271 |
if (savedcount == null) |
272 |
savedcount = 0; |
273 |
if (count == null) |
274 |
count = 0; |
275 |
|
276 |
|
277 |
int diff = Math.abs( savedcount - count); |
278 |
String msg = null; |
279 |
if (count > savedcount) { |
280 |
msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest"; |
281 |
} |
282 |
if (count < savedcount) { |
283 |
msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest"; |
284 |
} |
285 |
|
286 |
if (msg != null) { |
287 |
plugin.getLogger().info(msg); |
288 |
plugin.getMessageWrapper().sendMessage(owner, msg); |
289 |
} |
290 |
|
291 |
} |
292 |
|
293 |
} |
294 |
} |
295 |
|
296 |
ItemCount countItems(ItemStack[] input) { |
297 |
ItemCount output = new ItemCount(); |
298 |
for (int i=0; i<input.length; i++) { |
299 |
ItemStack current = input[i]; |
300 |
if (current == null) |
301 |
continue; |
302 |
|
303 |
int type = current.getTypeId(); |
304 |
|
305 |
Integer amount = output.get(type); |
306 |
if (amount == null) |
307 |
amount = 0; |
308 |
|
309 |
output.put(type, amount + current.getAmount() ); |
310 |
} |
311 |
return output; |
312 |
} |
313 |
/* |
314 |
ItemStack[] cloneItemStacks(ItemStack[] input) { |
315 |
ItemStack[] output = new ItemStack[ input.length ]; |
316 |
for (int i=0; i<input.length; i++) { |
317 |
if (input[i] != null) { |
318 |
output[i] = input[i].clone(); |
319 |
} else { |
320 |
output[i] = new ItemStack(0, 0); |
321 |
} |
322 |
} |
323 |
return output; |
324 |
}*/ |
325 |
|
326 |
|
327 |
} |