/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java
ViewVC logotype

Contents of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1803 - (show annotations) (download)
Mon May 28 14:32:53 2012 UTC (11 years, 11 months ago) by torben
File size: 10790 byte(s)
offline messages should also register the sender
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.Action;
25 import org.bukkit.event.block.BlockBreakEvent;
26 import org.bukkit.event.block.BlockPlaceEvent;
27 import org.bukkit.event.inventory.InventoryCloseEvent;
28 import org.bukkit.event.inventory.InventoryOpenEvent;
29 import org.bukkit.event.player.PlayerInteractEvent;
30 import org.bukkit.inventory.InventoryHolder;
31 import org.bukkit.inventory.ItemStack;
32
33
34 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 HashMap<Location,SnitchingChestBean> chestMap = new HashMap<Location, SnitchingChestBean>();
43
44
45 HoerupUtilsPlugin plugin;
46 Server server;
47
48 public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) {
49 this.plugin = plugin;
50 server = plugin.getServer();
51 try {
52 loadChests();
53 } catch (Exception e) {
54 e.printStackTrace();
55 //r.run();
56 loadChests();
57 }
58 }
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 SnitchingChestBean chest = chestMap.get(loc);
82 if (chest != null) {
83 if (chest.getOwner().equals(player.getName())) {
84 player.sendMessage("[SnitchingChest] Removing surveillance from chest");
85 removeChest(loc);
86 } else {
87 player.sendMessage("[SnitchingChest] Chest is already under surveillance");
88 }
89
90 return true;
91 }
92
93 chest = createChest(player.getName(), "", loc);
94 if (loc2 != null) {
95 chest.setDoublechest(true);
96 }
97 addChest(loc, chest);
98
99
100 player.sendMessage("[SnitchingChest] Chest is now under surveillance");
101
102
103 return true;
104 }
105
106 @EventHandler
107 public void onBlockBreak(BlockBreakEvent event) {
108 Location loc = event.getBlock().getLocation();
109 SnitchingChestBean chest = chestMap.get(loc);
110 if (chest != null) {
111 if (chest.getOwner().equals(event.getPlayer().getName())) {
112 removeChest(loc);
113 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 public void addChest(Location loc, SnitchingChestBean chest) {
121 chestMap.put(loc, chest);
122 if (chest.isDoublechest()) {
123 Location loc2 = getNeighborChest(loc);
124 chestMap.put(loc2, chest);
125 }
126 plugin.getDatabase().save(chest);
127
128 reloadChests();
129
130 }
131
132 void removeChest(Location loc) {
133 SnitchingChestBean chest = chestMap.remove(loc);
134 if (chest != null) {
135 if (chest.isDoublechest()){
136 Location loc2 = getNeighborChest(loc);
137 chestMap.remove(loc2);
138 }
139 plugin.getDatabase().delete(chest);
140 }
141 }
142
143 int loadChestsWorker() {
144 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
149 if (chest.isDoublechest()) {
150 Location loc2 = getNeighborChest(loc);
151 chestMap.put(loc2, chest);
152 }
153 }
154
155 return chestlist.size();
156 }
157
158 void reloadChests() {
159 chestMap.clear();
160 loadChestsWorker();
161 }
162
163 void loadChests() {
164 int count = loadChestsWorker();
165 plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests");
166 }
167
168
169 public SnitchingChestBean createChest(String owner, String description, Location loc) {
170
171 SnitchingChestBean chest = new SnitchingChestBean();
172 chest.setOwner(owner);
173 chest.setDescription(description);
174 setChestLocation(chest, loc);
175
176 return chest;
177 }
178
179
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 void saveChests() {
195
196 }*/
197
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
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 return loc;
233 }
234
235 @EventHandler
236 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 //SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc);
249 //plugin.getDatabase().save(chest);
250
251 chest.setDoublechest(true);
252 addChest(chestloc, chest);
253
254
255 event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" );
256 }
257
258 }
259 }
260
261 /*
262 * how to prevent a user from opening a chest - usefull if SnitchingChest should morph into a LockedChest
263 @EventHandler
264 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 public void onChestOpen(InventoryOpenEvent event) {
277
278 if (! (event.getPlayer() instanceof Player)) {
279 return;
280 }
281
282
283
284 InventoryHolder holder = event.getInventory().getHolder();
285 if (holder instanceof Chest || holder instanceof DoubleChest) {
286 Location loc = getChestLocation(holder);
287
288 SnitchingChestBean chest = chestMap.get( loc );
289 if (chest == null) {
290 return; //chest not surveyed by this plugin
291 }
292
293
294 Player player = (Player) event.getPlayer();
295 if (player.getName().equals(chest.getOwner() )) {
296 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 SnitchingChestBean chest = chestMap.get(loc);
316
317 if (chest == null) { //chest was not a snitching chest
318 return;
319 }
320
321 OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
322
323
324 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
351 if (diff > 0) {
352 String material = Material.getMaterial(item).name();
353 String msg = null;
354
355 if (count > savedcount) {
356 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 } else { //(count < savedcount)
358 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 }
360
361
362 plugin.getLogger().info(msg);
363 plugin.getMessageWrapper().sendMessage("system", owner, msg);
364 }
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 }

  ViewVC Help
Powered by ViewVC 1.1.20