2 |
|
|
3 |
|
|
4 |
import java.util.HashMap; |
import java.util.HashMap; |
5 |
|
import java.util.List; |
6 |
import java.util.Set; |
import java.util.Set; |
7 |
import java.util.TreeMap; |
import java.util.TreeMap; |
8 |
import java.util.TreeSet; |
import java.util.TreeSet; |
9 |
|
|
10 |
import org.bukkit.Location; |
import org.bukkit.Location; |
11 |
import org.bukkit.Material; |
import org.bukkit.Material; |
12 |
|
import org.bukkit.OfflinePlayer; |
13 |
|
import org.bukkit.Server; |
14 |
import org.bukkit.World; |
import org.bukkit.World; |
15 |
import org.bukkit.block.Block; |
import org.bukkit.block.Block; |
16 |
import org.bukkit.block.Chest; |
import org.bukkit.block.Chest; |
21 |
import org.bukkit.entity.Player; |
import org.bukkit.entity.Player; |
22 |
import org.bukkit.event.EventHandler; |
import org.bukkit.event.EventHandler; |
23 |
import org.bukkit.event.Listener; |
import org.bukkit.event.Listener; |
24 |
|
import org.bukkit.event.block.Action; |
25 |
import org.bukkit.event.block.BlockBreakEvent; |
import org.bukkit.event.block.BlockBreakEvent; |
26 |
|
import org.bukkit.event.block.BlockPlaceEvent; |
27 |
import org.bukkit.event.inventory.InventoryCloseEvent; |
import org.bukkit.event.inventory.InventoryCloseEvent; |
28 |
import org.bukkit.event.inventory.InventoryOpenEvent; |
import org.bukkit.event.inventory.InventoryOpenEvent; |
29 |
|
import org.bukkit.event.player.PlayerInteractEvent; |
30 |
import org.bukkit.inventory.InventoryHolder; |
import org.bukkit.inventory.InventoryHolder; |
31 |
import org.bukkit.inventory.ItemStack; |
import org.bukkit.inventory.ItemStack; |
32 |
import org.bukkit.plugin.Plugin; |
|
33 |
|
|
34 |
public class SnitchingChest implements Listener, CommandExecutor{ |
public class SnitchingChest implements Listener, CommandExecutor{ |
35 |
|
|
39 |
|
|
40 |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
41 |
|
|
42 |
HashMap<Location,String> chestMap = new HashMap<Location, String>(); |
HashMap<Location,SnitchingChestBean> chestMap = new HashMap<Location, SnitchingChestBean>(); |
43 |
|
|
44 |
|
|
45 |
Plugin plugin; |
HoerupUtilsPlugin plugin; |
46 |
|
Server server; |
47 |
|
|
48 |
public SnitchingChest(Plugin plugin) { |
public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) { |
49 |
this.plugin = plugin; |
this.plugin = plugin; |
50 |
|
server = plugin.getServer(); |
51 |
loadChests(); |
try { |
52 |
|
loadChests(); |
53 |
|
} catch (Exception e) { |
54 |
|
e.printStackTrace(); |
55 |
|
//r.run(); |
56 |
|
loadChests(); |
57 |
|
} |
58 |
} |
} |
59 |
|
|
60 |
|
|
78 |
|
|
79 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
80 |
|
|
81 |
String owner = chestMap.get(loc); |
SnitchingChestBean chest = chestMap.get(loc); |
82 |
if (owner != null) { |
if (chest != null) { |
83 |
if (owner.equals(player.getName())) { |
if (chest.getOwner().equals(player.getName())) { |
84 |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
85 |
chestMap.remove(loc); |
removeChest(loc); |
|
if (loc2 != null) { |
|
|
chestMap.remove(loc2); |
|
|
} |
|
|
saveChests(); |
|
86 |
} else { |
} else { |
87 |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
88 |
} |
} |
90 |
return true; |
return true; |
91 |
} |
} |
92 |
|
|
93 |
chestMap.put(loc, player.getName() ); |
chest = createChest(player.getName(), "", loc); |
94 |
if (loc2 != null) { |
if (loc2 != null) { |
95 |
chestMap.put(loc2, player.getName() ); |
chest.setDoublechest(true); |
96 |
} |
} |
97 |
|
addChest(loc, chest); |
98 |
|
|
99 |
|
|
100 |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
101 |
saveChests(); |
|
102 |
|
|
103 |
return true; |
return true; |
104 |
} |
} |
106 |
@EventHandler |
@EventHandler |
107 |
public void onBlockBreak(BlockBreakEvent event) { |
public void onBlockBreak(BlockBreakEvent event) { |
108 |
Location loc = event.getBlock().getLocation(); |
Location loc = event.getBlock().getLocation(); |
109 |
String owner = chestMap.get(loc); |
SnitchingChestBean chest = chestMap.get(loc); |
110 |
if (owner != null) { |
if (chest != null) { |
111 |
if (owner.equals(event.getPlayer().getName())) { |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
112 |
chestMap.remove(loc); |
removeChest(loc); |
|
saveChests(); |
|
113 |
event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance"); |
event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance"); |
114 |
} else { |
} else { |
115 |
event.setCancelled(true); |
event.setCancelled(true); |
117 |
} |
} |
118 |
} |
} |
119 |
} |
} |
120 |
|
public void addChest(Location loc, SnitchingChestBean chest) { |
121 |
|
chestMap.put(loc, chest); |
122 |
|
if (chest.isDoublechest()) { |
123 |
|
Location loc2 = getNeighborChest(loc); |
124 |
|
chestMap.put(loc2, chest); |
125 |
|
} |
126 |
|
plugin.getDatabase().save(chest); |
127 |
|
|
128 |
|
reloadChests(); |
129 |
|
|
130 |
|
} |
131 |
|
|
132 |
|
void removeChest(Location loc) { |
133 |
|
SnitchingChestBean chest = chestMap.remove(loc); |
134 |
|
if (chest != null) { |
135 |
|
if (chest.isDoublechest()){ |
136 |
|
Location loc2 = getNeighborChest(loc); |
137 |
|
chestMap.remove(loc2); |
138 |
|
} |
139 |
|
plugin.getDatabase().delete(chest); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
int loadChestsWorker() { |
144 |
|
List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList(); |
145 |
|
for (SnitchingChestBean chest : chestlist) { |
146 |
|
Location loc = getChestLocation(server, chest); |
147 |
|
chestMap.put(loc, chest); |
148 |
|
|
149 |
|
if (chest.isDoublechest()) { |
150 |
|
Location loc2 = getNeighborChest(loc); |
151 |
|
chestMap.put(loc2, chest); |
152 |
|
} |
153 |
|
} |
154 |
|
|
155 |
|
return chestlist.size(); |
156 |
|
} |
157 |
|
|
158 |
|
void reloadChests() { |
159 |
|
chestMap.clear(); |
160 |
|
loadChestsWorker(); |
161 |
|
} |
162 |
|
|
163 |
|
void loadChests() { |
164 |
|
int count = loadChestsWorker(); |
165 |
|
plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests"); |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
void loadChests() { |
public SnitchingChestBean createChest(String owner, String description, Location loc) { |
170 |
|
|
171 |
|
SnitchingChestBean chest = new SnitchingChestBean(); |
172 |
|
chest.setOwner(owner); |
173 |
|
chest.setDescription(description); |
174 |
|
setChestLocation(chest, loc); |
175 |
|
|
176 |
|
return chest; |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
public void setChestLocation(SnitchingChestBean chest, Location loc) { |
181 |
|
chest.setWorld( loc.getWorld().getName() ); |
182 |
|
chest.setX( loc.getBlockX() ); |
183 |
|
chest.setY( loc.getBlockY() ); |
184 |
|
chest.setZ( loc.getBlockZ() ); |
185 |
} |
} |
186 |
|
|
187 |
|
public Location getChestLocation(Server server, SnitchingChestBean chest) { |
188 |
|
World wrld = server.getWorld(chest.getWorld()); |
189 |
|
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
/* |
194 |
void saveChests() { |
void saveChests() { |
195 |
|
|
196 |
} |
}*/ |
197 |
|
|
198 |
Location getNeighborChest(Location loc) { |
Location getNeighborChest(Location loc) { |
199 |
World world = loc.getWorld(); |
World world = loc.getWorld(); |
225 |
} else { |
} else { |
226 |
loc = ( (DoubleChest)holder).getLocation(); |
loc = ( (DoubleChest)holder).getLocation(); |
227 |
} |
} |
228 |
|
|
229 |
|
loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5 |
230 |
|
loc.setZ( loc.getBlockZ() ); // -- // -- |
231 |
|
|
232 |
return loc; |
return loc; |
233 |
} |
} |
234 |
|
|
235 |
@EventHandler |
@EventHandler |
236 |
|
public void onChestPlaced(BlockPlaceEvent event) { |
237 |
|
Block block = event.getBlock(); |
238 |
|
|
239 |
|
if (block.getType() != Material.CHEST) { |
240 |
|
return; |
241 |
|
} |
242 |
|
|
243 |
|
Location chestloc = getNeighborChest( block.getLocation() ); |
244 |
|
if (chestloc != null) { |
245 |
|
SnitchingChestBean chest = chestMap.get(chestloc); |
246 |
|
|
247 |
|
if (chest != null) { //the neighbor is a snitching chest |
248 |
|
//SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc); |
249 |
|
//plugin.getDatabase().save(chest); |
250 |
|
|
251 |
|
chest.setDoublechest(true); |
252 |
|
addChest(chestloc, chest); |
253 |
|
|
254 |
|
|
255 |
|
event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" ); |
256 |
|
} |
257 |
|
|
258 |
|
} |
259 |
|
} |
260 |
|
|
261 |
|
/* |
262 |
|
* how to prevent a user from opening a chest - usefull if SnitchingChest should morph into a LockedChest |
263 |
|
@EventHandler |
264 |
|
public void onChestInteract(PlayerInteractEvent event) { |
265 |
|
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { |
266 |
|
Block b = event.getClickedBlock(); |
267 |
|
|
268 |
|
if (b.getType() == Material.CHEST) { |
269 |
|
event.setCancelled(true); |
270 |
|
} |
271 |
|
} |
272 |
|
} |
273 |
|
*/ |
274 |
|
|
275 |
|
@EventHandler |
276 |
public void onChestOpen(InventoryOpenEvent event) { |
public void onChestOpen(InventoryOpenEvent event) { |
277 |
|
|
278 |
if (! (event.getPlayer() instanceof Player)) { |
if (! (event.getPlayer() instanceof Player)) { |
279 |
return; |
return; |
280 |
} |
} |
281 |
|
|
282 |
|
|
283 |
|
|
284 |
InventoryHolder holder = event.getInventory().getHolder(); |
InventoryHolder holder = event.getInventory().getHolder(); |
285 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
286 |
Location loc = getChestLocation(holder); |
Location loc = getChestLocation(holder); |
287 |
|
|
288 |
String owner = chestMap.get( loc ); |
SnitchingChestBean chest = chestMap.get( loc ); |
289 |
if (owner == null) { |
if (chest == null) { |
290 |
return; //chest not surveyed by this plugin |
return; //chest not surveyed by this plugin |
291 |
} |
} |
292 |
|
|
293 |
|
|
294 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
295 |
if (player.getName().equals(owner)) { |
if (player.getName().equals(chest.getOwner() )) { |
296 |
return; //chest is owned by it's own player |
return; //chest is owned by it's own player |
297 |
} |
} |
298 |
|
|
312 |
InventoryHolder holder = event.getInventory().getHolder(); |
InventoryHolder holder = event.getInventory().getHolder(); |
313 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
314 |
Location loc = getChestLocation(holder); |
Location loc = getChestLocation(holder); |
315 |
String owner = chestMap.get(loc); |
SnitchingChestBean chest = chestMap.get(loc); |
316 |
|
|
317 |
|
if (chest == null) { //chest was not a snitching chest |
318 |
|
return; |
319 |
|
} |
320 |
|
|
321 |
|
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
322 |
|
|
323 |
|
|
324 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
348 |
|
|
349 |
int diff = Math.abs( savedcount - count); |
int diff = Math.abs( savedcount - count); |
350 |
|
|
351 |
if (count > savedcount) { |
if (diff > 0) { |
352 |
String msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest"; |
String material = Material.getMaterial(item).name(); |
353 |
plugin.getLogger().info(msg); |
String msg = null; |
354 |
} |
|
355 |
if (count < savedcount) { |
if (count > savedcount) { |
356 |
String msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest"; |
msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
357 |
|
} else { //(count < savedcount) |
358 |
|
msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
plugin.getLogger().info(msg); |
plugin.getLogger().info(msg); |
363 |
|
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
364 |
} |
} |
365 |
|
|
366 |
} |
} |