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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20