/[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 1777 - (hide annotations) (download)
Thu Apr 5 14:10:07 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: 8797 byte(s)
Only try to delete the a chest if it actually was in the map
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     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 torben 1770
31 torben 1768 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 torben 1773 HashMap<Location,SnitchingChestBean> chestMap = new HashMap<Location, SnitchingChestBean>();
40 torben 1768
41    
42 torben 1770 HoerupUtilsPlugin plugin;
43     Server server;
44 torben 1768
45 torben 1773 public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) {
46 torben 1768 this.plugin = plugin;
47 torben 1770 server = plugin.getServer();
48 torben 1773 try {
49     loadChests();
50     } catch (Exception e) {
51     e.printStackTrace();
52     //r.run();
53     loadChests();
54     }
55 torben 1768 }
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 torben 1773 SnitchingChestBean chest = chestMap.get(loc);
79     if (chest != null) {
80     if (chest.getOwner().equals(player.getName())) {
81 torben 1768 player.sendMessage("[SnitchingChest] Removing surveillance from chest");
82 torben 1773 removeChest(loc);
83 torben 1768 if (loc2 != null) {
84 torben 1773 removeChest(loc2);
85 torben 1768 }
86     } else {
87     player.sendMessage("[SnitchingChest] Chest is already under surveillance");
88     }
89    
90     return true;
91     }
92    
93 torben 1773 SnitchingChestBean chest1 = createChest(player.getName(), "", loc);
94     //chestMap.put(loc, chest1);
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     //chestMap.put(loc2, chest2 );
101 torben 1768 }
102    
103     player.sendMessage("[SnitchingChest] Chest is now under surveillance");
104    
105 torben 1773
106 torben 1768 return true;
107     }
108    
109     @EventHandler
110     public void onBlockBreak(BlockBreakEvent event) {
111     Location loc = event.getBlock().getLocation();
112 torben 1773 SnitchingChestBean chest = chestMap.get(loc);
113     if (chest != null) {
114     if (chest.getOwner().equals(event.getPlayer().getName())) {
115     removeChest(loc);
116 torben 1768 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 torben 1773 public void addChest(Location loc, SnitchingChestBean chest) {
124     chestMap.put(loc, chest);
125     plugin.getDatabase().save(chest);
126     }
127 torben 1768
128 torben 1773 void removeChest(Location loc) {
129     SnitchingChestBean chest = chestMap.remove(loc);
130 torben 1777 if (chest != null) {
131     plugin.getDatabase().delete(chest);
132     }
133 torben 1773 }
134    
135     void loadChests() {
136     List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList();
137     for (SnitchingChestBean chest : chestlist) {
138     Location loc = getChestLocation(server, chest);
139     chestMap.put(loc, chest);
140     }
141 torben 1776
142     plugin.getLogger().info("[SnitchingChest] loaded " + chestMap.size() + " chests");
143 torben 1773 }
144    
145    
146     public SnitchingChestBean createChest(String owner, String description, Location loc) {
147 torben 1768
148 torben 1773 SnitchingChestBean chest = new SnitchingChestBean();
149     chest.setOwner(owner);
150     chest.setDescription(description);
151     setChestLocation(chest, loc);
152    
153     return chest;
154 torben 1768 }
155    
156 torben 1773
157     public void setChestLocation(SnitchingChestBean chest, Location loc) {
158     chest.setWorld( loc.getWorld().getName() );
159     chest.setX( loc.getBlockX() );
160     chest.setY( loc.getBlockY() );
161     chest.setZ( loc.getBlockZ() );
162     }
163    
164     public Location getChestLocation(Server server, SnitchingChestBean chest) {
165     World wrld = server.getWorld(chest.getWorld());
166     return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());
167     }
168    
169    
170     /*
171 torben 1768 void saveChests() {
172    
173 torben 1773 }*/
174 torben 1768
175     Location getNeighborChest(Location loc) {
176     World world = loc.getWorld();
177    
178     Location 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()-1, loc.getY(), loc.getZ() );
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     target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1);
191     if (world.getBlockAt(target).getType() == Material.CHEST )
192     return target;
193    
194     return null;
195     }
196    
197    
198     Location getChestLocation(InventoryHolder holder) {
199     Location loc;
200     if ( holder instanceof Chest) {
201     loc = ( (Chest)holder).getLocation();
202     } else {
203     loc = ( (DoubleChest)holder).getLocation();
204     }
205     return loc;
206     }
207    
208     @EventHandler
209     public void onChestOpen(InventoryOpenEvent event) {
210    
211     if (! (event.getPlayer() instanceof Player)) {
212     return;
213     }
214    
215    
216     InventoryHolder holder = event.getInventory().getHolder();
217     if (holder instanceof Chest || holder instanceof DoubleChest) {
218     Location loc = getChestLocation(holder);
219    
220 torben 1773 SnitchingChestBean chest = chestMap.get( loc );
221     if (chest == null) {
222 torben 1768 return; //chest not surveyed by this plugin
223     }
224    
225    
226     Player player = (Player) event.getPlayer();
227 torben 1773 if (player.getName().equals(chest.getOwner() )) {
228 torben 1768 return; //chest is owned by it's own player
229     }
230    
231     ItemCount contents = countItems( event.getInventory().getContents() );
232    
233     contentMap.put(player.getName(), contents );
234     }
235     }
236    
237     @EventHandler
238     public void onChestClose(InventoryCloseEvent event) {
239     if (! (event.getPlayer() instanceof Player)) {
240     return;
241     }
242    
243    
244     InventoryHolder holder = event.getInventory().getHolder();
245     if (holder instanceof Chest || holder instanceof DoubleChest) {
246     Location loc = getChestLocation(holder);
247 torben 1773 SnitchingChestBean chest = chestMap.get(loc);
248 torben 1775
249     if (chest == null) { //chest was not a snitching chest
250     return;
251     }
252    
253 torben 1773 OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
254 torben 1768
255 torben 1769
256 torben 1768 Player player = (Player) event.getPlayer();
257    
258     ItemCount savedContent = contentMap.get( player.getName() );
259    
260     if (savedContent == null) {
261     return;
262     }
263     contentMap.remove( player.getName() );
264    
265     ItemCount content = countItems( event.getInventory().getContents() );
266    
267     Set<Integer> combinedKeyset = new TreeSet<Integer>();
268     combinedKeyset.addAll( savedContent.keySet() );
269     combinedKeyset.addAll( content.keySet() );
270    
271     for (Integer item : combinedKeyset ) {
272     Integer savedcount = savedContent.get(item);
273     Integer count = content.get(item);
274    
275     if (savedcount == null)
276     savedcount = 0;
277     if (count == null)
278     count = 0;
279    
280    
281     int diff = Math.abs( savedcount - count);
282 torben 1771 String msg = null;
283 torben 1768 if (count > savedcount) {
284 torben 1771 msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest";
285 torben 1768 }
286     if (count < savedcount) {
287 torben 1771 msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest";
288     }
289    
290     if (msg != null) {
291 torben 1768 plugin.getLogger().info(msg);
292 torben 1770 plugin.getMessageWrapper().sendMessage(owner, msg);
293 torben 1768 }
294    
295     }
296    
297     }
298     }
299    
300     ItemCount countItems(ItemStack[] input) {
301     ItemCount output = new ItemCount();
302     for (int i=0; i<input.length; i++) {
303     ItemStack current = input[i];
304     if (current == null)
305     continue;
306    
307     int type = current.getTypeId();
308    
309     Integer amount = output.get(type);
310     if (amount == null)
311     amount = 0;
312    
313     output.put(type, amount + current.getAmount() );
314     }
315     return output;
316     }
317     /*
318     ItemStack[] cloneItemStacks(ItemStack[] input) {
319     ItemStack[] output = new ItemStack[ input.length ];
320     for (int i=0; i<input.length; i++) {
321     if (input[i] != null) {
322     output[i] = input[i].clone();
323     } else {
324     output[i] = new ItemStack(0, 0);
325     }
326     }
327     return output;
328     }*/
329    
330    
331     }

  ViewVC Help
Powered by ViewVC 1.1.20