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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1784 - (hide annotations) (download)
Thu Apr 5 21:15:30 2012 UTC (12 years, 1 month ago) by torben
Original Path: miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java
File size: 9861 byte(s)
again use the object's name instead of relying on toString()
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     import org.bukkit.event.block.BlockBreakEvent;
25 torben 1780 import org.bukkit.event.block.BlockPlaceEvent;
26 torben 1768 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 torben 1770
32 torben 1768 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 torben 1773 HashMap<Location,SnitchingChestBean> chestMap = new HashMap<Location, SnitchingChestBean>();
41 torben 1768
42    
43 torben 1770 HoerupUtilsPlugin plugin;
44     Server server;
45 torben 1768
46 torben 1773 public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) {
47 torben 1768 this.plugin = plugin;
48 torben 1770 server = plugin.getServer();
49 torben 1773 try {
50     loadChests();
51     } catch (Exception e) {
52     e.printStackTrace();
53     //r.run();
54     loadChests();
55     }
56 torben 1768 }
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 torben 1773 SnitchingChestBean chest = chestMap.get(loc);
80     if (chest != null) {
81     if (chest.getOwner().equals(player.getName())) {
82 torben 1768 player.sendMessage("[SnitchingChest] Removing surveillance from chest");
83 torben 1773 removeChest(loc);
84 torben 1768 if (loc2 != null) {
85 torben 1773 removeChest(loc2);
86 torben 1768 }
87     } else {
88     player.sendMessage("[SnitchingChest] Chest is already under surveillance");
89     }
90    
91     return true;
92     }
93    
94 torben 1773 SnitchingChestBean chest1 = createChest(player.getName(), "", loc);
95     addChest(loc, chest1);
96 torben 1768 if (loc2 != null) {
97 torben 1773
98     SnitchingChestBean chest2 = createChest (player.getName(), "", loc2);
99     addChest(loc, chest2);
100 torben 1768 }
101    
102     player.sendMessage("[SnitchingChest] Chest is now under surveillance");
103    
104 torben 1773
105 torben 1768 return true;
106     }
107    
108     @EventHandler
109     public void onBlockBreak(BlockBreakEvent event) {
110     Location loc = event.getBlock().getLocation();
111 torben 1773 SnitchingChestBean chest = chestMap.get(loc);
112     if (chest != null) {
113     if (chest.getOwner().equals(event.getPlayer().getName())) {
114     removeChest(loc);
115 torben 1768 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 torben 1773 public void addChest(Location loc, SnitchingChestBean chest) {
123     chestMap.put(loc, chest);
124     plugin.getDatabase().save(chest);
125     }
126 torben 1768
127 torben 1773 void removeChest(Location loc) {
128     SnitchingChestBean chest = chestMap.remove(loc);
129 torben 1777 if (chest != null) {
130     plugin.getDatabase().delete(chest);
131     }
132 torben 1773 }
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 torben 1776
141     plugin.getLogger().info("[SnitchingChest] loaded " + chestMap.size() + " chests");
142 torben 1773 }
143    
144    
145     public SnitchingChestBean createChest(String owner, String description, Location loc) {
146 torben 1768
147 torben 1773 SnitchingChestBean chest = new SnitchingChestBean();
148     chest.setOwner(owner);
149     chest.setDescription(description);
150     setChestLocation(chest, loc);
151    
152     return chest;
153 torben 1768 }
154    
155 torben 1773
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 torben 1768 void saveChests() {
171    
172 torben 1773 }*/
173 torben 1768
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 torben 1779
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 torben 1768 return loc;
209     }
210    
211     @EventHandler
212 torben 1780 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 torben 1768 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 torben 1773 SnitchingChestBean chest = chestMap.get( loc );
245     if (chest == null) {
246 torben 1768 return; //chest not surveyed by this plugin
247     }
248    
249    
250     Player player = (Player) event.getPlayer();
251 torben 1773 if (player.getName().equals(chest.getOwner() )) {
252 torben 1768 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 torben 1773 SnitchingChestBean chest = chestMap.get(loc);
272 torben 1775
273     if (chest == null) { //chest was not a snitching chest
274     return;
275     }
276    
277 torben 1773 OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
278 torben 1768
279 torben 1769
280 torben 1768 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 torben 1771
307 torben 1782 if (diff > 0) {
308     String material = Material.getMaterial(item).name();
309     String msg = null;
310    
311     if (count > savedcount) {
312 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();
313 torben 1782 } else { //(count < savedcount)
314 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();
315 torben 1782 }
316    
317    
318 torben 1768 plugin.getLogger().info(msg);
319 torben 1770 plugin.getMessageWrapper().sendMessage(owner, msg);
320 torben 1768 }
321    
322     }
323    
324     }
325     }
326    
327     ItemCount countItems(ItemStack[] input) {
328     ItemCount output = new ItemCount();
329     for (int i=0; i<input.length; i++) {
330     ItemStack current = input[i];
331     if (current == null)
332     continue;
333    
334     int type = current.getTypeId();
335    
336     Integer amount = output.get(type);
337     if (amount == null)
338     amount = 0;
339    
340     output.put(type, amount + current.getAmount() );
341     }
342     return output;
343     }
344     /*
345     ItemStack[] cloneItemStacks(ItemStack[] input) {
346     ItemStack[] output = new ItemStack[ input.length ];
347     for (int i=0; i<input.length; i++) {
348     if (input[i] != null) {
349     output[i] = input[i].clone();
350     } else {
351     output[i] = new ItemStack(0, 0);
352     }
353     }
354     return output;
355     }*/
356    
357    
358     }

  ViewVC Help
Powered by ViewVC 1.1.20