1 |
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 |
|
192 |
Player player = (Player) event.getPlayer(); |
193 |
|
194 |
ItemCount savedContent = contentMap.get( player.getName() ); |
195 |
|
196 |
if (savedContent == null) { |
197 |
return; |
198 |
} |
199 |
contentMap.remove( player.getName() ); |
200 |
|
201 |
ItemCount content = countItems( event.getInventory().getContents() ); |
202 |
|
203 |
Set<Integer> combinedKeyset = new TreeSet<Integer>(); |
204 |
combinedKeyset.addAll( savedContent.keySet() ); |
205 |
combinedKeyset.addAll( content.keySet() ); |
206 |
|
207 |
for (Integer item : combinedKeyset ) { |
208 |
Integer savedcount = savedContent.get(item); |
209 |
Integer count = content.get(item); |
210 |
|
211 |
if (savedcount == null) |
212 |
savedcount = 0; |
213 |
if (count == null) |
214 |
count = 0; |
215 |
|
216 |
|
217 |
int diff = Math.abs( savedcount - count); |
218 |
|
219 |
if (count > savedcount) { |
220 |
String msg = player.getName() + " added " + diff + " units of " + item ; |
221 |
plugin.getLogger().info(msg); |
222 |
} |
223 |
if (count < savedcount) { |
224 |
String msg = player.getName() + " removed " + diff + " units of " + item " from "; |
225 |
plugin.getLogger().info(msg); |
226 |
} |
227 |
|
228 |
} |
229 |
|
230 |
} |
231 |
} |
232 |
|
233 |
ItemCount countItems(ItemStack[] input) { |
234 |
ItemCount output = new ItemCount(); |
235 |
for (int i=0; i<input.length; i++) { |
236 |
ItemStack current = input[i]; |
237 |
if (current == null) |
238 |
continue; |
239 |
|
240 |
int type = current.getTypeId(); |
241 |
|
242 |
Integer amount = output.get(type); |
243 |
if (amount == null) |
244 |
amount = 0; |
245 |
|
246 |
output.put(type, amount + current.getAmount() ); |
247 |
} |
248 |
return output; |
249 |
} |
250 |
/* |
251 |
ItemStack[] cloneItemStacks(ItemStack[] input) { |
252 |
ItemStack[] output = new ItemStack[ input.length ]; |
253 |
for (int i=0; i<input.length; i++) { |
254 |
if (input[i] != null) { |
255 |
output[i] = input[i].clone(); |
256 |
} else { |
257 |
output[i] = new ItemStack(0, 0); |
258 |
} |
259 |
} |
260 |
return output; |
261 |
}*/ |
262 |
|
263 |
|
264 |
} |