/[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 1771 - (hide annotations) (download)
Tue Apr 3 20:34:54 2012 UTC (12 years, 2 months ago) by torben
Original Path: miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java
File size: 7051 byte(s)
refactor message sending
1 torben 1768 package dk.thoerup.bukkit.hoeruputils;
2    
3    
4     import java.util.HashMap;
5     import java.util.Set;
6     import java.util.TreeMap;
7     import java.util.TreeSet;
8    
9     import org.bukkit.Location;
10     import org.bukkit.Material;
11 torben 1770 import org.bukkit.OfflinePlayer;
12     import org.bukkit.Server;
13 torben 1768 import org.bukkit.World;
14     import org.bukkit.block.Block;
15     import org.bukkit.block.Chest;
16     import org.bukkit.block.DoubleChest;
17     import org.bukkit.command.Command;
18     import org.bukkit.command.CommandExecutor;
19     import org.bukkit.command.CommandSender;
20     import org.bukkit.entity.Player;
21     import org.bukkit.event.EventHandler;
22     import org.bukkit.event.Listener;
23     import org.bukkit.event.block.BlockBreakEvent;
24     import org.bukkit.event.inventory.InventoryCloseEvent;
25     import org.bukkit.event.inventory.InventoryOpenEvent;
26     import org.bukkit.inventory.InventoryHolder;
27     import org.bukkit.inventory.ItemStack;
28    
29 torben 1770
30 torben 1768 public class SnitchingChest implements Listener, CommandExecutor{
31    
32     class ItemCount extends TreeMap<Integer,Integer> {
33     private static final long serialVersionUID = 1L;
34     };
35    
36     HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>();
37    
38     HashMap<Location,String> chestMap = new HashMap<Location, String>();
39    
40    
41 torben 1770 HoerupUtilsPlugin plugin;
42     Server server;
43 torben 1768
44 torben 1770 public SnitchingChest(HoerupUtilsPlugin plugin) {
45 torben 1768 this.plugin = plugin;
46 torben 1770 server = plugin.getServer();
47 torben 1768
48     loadChests();
49     }
50    
51    
52     @Override
53     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
54     if (! (sender instanceof Player) ) {
55     sender.sendMessage("this is not a console command!");
56     return true;
57     }
58    
59     Player player = (Player) sender;
60    
61    
62     Block b = player.getTargetBlock(null, 30);
63     Location loc = b.getLocation();
64    
65     if (b.getTypeId() != 54) {
66     player.sendMessage("[SnitchingChest] Please look at the chest you want to be a snitch");
67     return true;
68     }
69    
70     Location loc2 = getNeighborChest(loc);
71    
72     String owner = chestMap.get(loc);
73     if (owner != null) {
74     if (owner.equals(player.getName())) {
75     player.sendMessage("[SnitchingChest] Removing surveillance from chest");
76     chestMap.remove(loc);
77     if (loc2 != null) {
78     chestMap.remove(loc2);
79     }
80     saveChests();
81     } else {
82     player.sendMessage("[SnitchingChest] Chest is already under surveillance");
83     }
84    
85     return true;
86     }
87    
88     chestMap.put(loc, player.getName() );
89     if (loc2 != null) {
90     chestMap.put(loc2, player.getName() );
91     }
92    
93     player.sendMessage("[SnitchingChest] Chest is now under surveillance");
94     saveChests();
95    
96     return true;
97     }
98    
99     @EventHandler
100     public void onBlockBreak(BlockBreakEvent event) {
101     Location loc = event.getBlock().getLocation();
102     String owner = chestMap.get(loc);
103     if (owner != null) {
104     if (owner.equals(event.getPlayer().getName())) {
105     chestMap.remove(loc);
106     saveChests();
107     event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance");
108     } else {
109     event.setCancelled(true);
110     event.getPlayer().sendMessage("You can't destroy that chest");
111     }
112     }
113     }
114    
115     void loadChests() {
116    
117     }
118    
119     void saveChests() {
120    
121     }
122    
123     Location getNeighborChest(Location loc) {
124     World world = loc.getWorld();
125    
126     Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() );
127     if (world.getBlockAt(target).getType() == Material.CHEST )
128     return target;
129    
130     target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() );
131     if (world.getBlockAt(target).getType() == Material.CHEST )
132     return target;
133    
134     target = new Location(world, loc.getX(), loc.getY(), loc.getZ() +1);
135     if (world.getBlockAt(target).getType() == Material.CHEST )
136     return target;
137    
138     target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1);
139     if (world.getBlockAt(target).getType() == Material.CHEST )
140     return target;
141    
142     return null;
143     }
144    
145    
146     Location getChestLocation(InventoryHolder holder) {
147     Location loc;
148     if ( holder instanceof Chest) {
149     loc = ( (Chest)holder).getLocation();
150     } else {
151     loc = ( (DoubleChest)holder).getLocation();
152     }
153     return loc;
154     }
155    
156     @EventHandler
157     public void onChestOpen(InventoryOpenEvent event) {
158    
159     if (! (event.getPlayer() instanceof Player)) {
160     return;
161     }
162    
163    
164     InventoryHolder holder = event.getInventory().getHolder();
165     if (holder instanceof Chest || holder instanceof DoubleChest) {
166     Location loc = getChestLocation(holder);
167    
168     String owner = chestMap.get( loc );
169     if (owner == null) {
170     return; //chest not surveyed by this plugin
171     }
172    
173    
174     Player player = (Player) event.getPlayer();
175     if (player.getName().equals(owner)) {
176     return; //chest is owned by it's own player
177     }
178    
179     ItemCount contents = countItems( event.getInventory().getContents() );
180    
181     contentMap.put(player.getName(), contents );
182     }
183     }
184    
185     @EventHandler
186     public void onChestClose(InventoryCloseEvent event) {
187     if (! (event.getPlayer() instanceof Player)) {
188     return;
189     }
190    
191    
192     InventoryHolder holder = event.getInventory().getHolder();
193     if (holder instanceof Chest || holder instanceof DoubleChest) {
194     Location loc = getChestLocation(holder);
195 torben 1770 String ownerName = chestMap.get(loc);
196     OfflinePlayer owner = server.getOfflinePlayer(ownerName);
197 torben 1768
198 torben 1769
199 torben 1768 Player player = (Player) event.getPlayer();
200    
201     ItemCount savedContent = contentMap.get( player.getName() );
202    
203     if (savedContent == null) {
204     return;
205     }
206     contentMap.remove( player.getName() );
207    
208     ItemCount content = countItems( event.getInventory().getContents() );
209    
210     Set<Integer> combinedKeyset = new TreeSet<Integer>();
211     combinedKeyset.addAll( savedContent.keySet() );
212     combinedKeyset.addAll( content.keySet() );
213    
214     for (Integer item : combinedKeyset ) {
215     Integer savedcount = savedContent.get(item);
216     Integer count = content.get(item);
217    
218     if (savedcount == null)
219     savedcount = 0;
220     if (count == null)
221     count = 0;
222    
223    
224     int diff = Math.abs( savedcount - count);
225 torben 1771 String msg = null;
226 torben 1768 if (count > savedcount) {
227 torben 1771 msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest";
228 torben 1768 }
229     if (count < savedcount) {
230 torben 1771 msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest";
231     }
232    
233     if (msg != null) {
234 torben 1768 plugin.getLogger().info(msg);
235 torben 1770 plugin.getMessageWrapper().sendMessage(owner, msg);
236 torben 1768 }
237    
238     }
239    
240     }
241     }
242    
243     ItemCount countItems(ItemStack[] input) {
244     ItemCount output = new ItemCount();
245     for (int i=0; i<input.length; i++) {
246     ItemStack current = input[i];
247     if (current == null)
248     continue;
249    
250     int type = current.getTypeId();
251    
252     Integer amount = output.get(type);
253     if (amount == null)
254     amount = 0;
255    
256     output.put(type, amount + current.getAmount() );
257     }
258     return output;
259     }
260     /*
261     ItemStack[] cloneItemStacks(ItemStack[] input) {
262     ItemStack[] output = new ItemStack[ input.length ];
263     for (int i=0; i<input.length; i++) {
264     if (input[i] != null) {
265     output[i] = input[i].clone();
266     } else {
267     output[i] = new ItemStack(0, 0);
268     }
269     }
270     return output;
271     }*/
272    
273    
274     }

  ViewVC Help
Powered by ViewVC 1.1.20