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

  ViewVC Help
Powered by ViewVC 1.1.20