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.BlockBreakEvent; |
import org.bukkit.event.block.BlockBreakEvent; |
25 |
|
import org.bukkit.event.block.BlockPlaceEvent; |
26 |
import org.bukkit.event.inventory.InventoryCloseEvent; |
import org.bukkit.event.inventory.InventoryCloseEvent; |
27 |
import org.bukkit.event.inventory.InventoryOpenEvent; |
import org.bukkit.event.inventory.InventoryOpenEvent; |
28 |
import org.bukkit.inventory.InventoryHolder; |
import org.bukkit.inventory.InventoryHolder; |
81 |
if (chest.getOwner().equals(player.getName())) { |
if (chest.getOwner().equals(player.getName())) { |
82 |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
83 |
removeChest(loc); |
removeChest(loc); |
|
if (loc2 != null) { |
|
|
removeChest(loc2); |
|
|
} |
|
84 |
} else { |
} else { |
85 |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
86 |
} |
} |
88 |
return true; |
return true; |
89 |
} |
} |
90 |
|
|
91 |
SnitchingChestBean chest1 = createChest(player.getName(), "", loc); |
chest = createChest(player.getName(), "", loc); |
|
//chestMap.put(loc, chest1); |
|
|
addChest(loc, chest1); |
|
92 |
if (loc2 != null) { |
if (loc2 != null) { |
93 |
|
chest.setDoublechest(true); |
|
SnitchingChestBean chest2 = createChest (player.getName(), "", loc2); |
|
|
addChest(loc, chest2); |
|
|
//chestMap.put(loc2, chest2 ); |
|
94 |
} |
} |
95 |
|
addChest(loc, chest); |
96 |
|
|
97 |
|
|
98 |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
99 |
|
|
117 |
} |
} |
118 |
public void addChest(Location loc, SnitchingChestBean chest) { |
public void addChest(Location loc, SnitchingChestBean chest) { |
119 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
120 |
|
if (chest.isDoublechest()) { |
121 |
|
Location loc2 = getNeighborChest(loc); |
122 |
|
chestMap.put(loc2, chest); |
123 |
|
} |
124 |
plugin.getDatabase().save(chest); |
plugin.getDatabase().save(chest); |
125 |
|
|
126 |
|
reloadChests(); |
127 |
|
|
128 |
} |
} |
129 |
|
|
130 |
void removeChest(Location loc) { |
void removeChest(Location loc) { |
131 |
SnitchingChestBean chest = chestMap.remove(loc); |
SnitchingChestBean chest = chestMap.remove(loc); |
132 |
plugin.getDatabase().delete(chest); |
if (chest != null) { |
133 |
|
if (chest.isDoublechest()){ |
134 |
|
Location loc2 = getNeighborChest(loc); |
135 |
|
chestMap.remove(loc2); |
136 |
|
} |
137 |
|
plugin.getDatabase().delete(chest); |
138 |
|
} |
139 |
} |
} |
140 |
|
|
141 |
void loadChests() { |
int loadChestsWorker() { |
142 |
List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList(); |
List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList(); |
143 |
for (SnitchingChestBean chest : chestlist) { |
for (SnitchingChestBean chest : chestlist) { |
144 |
Location loc = getChestLocation(server, chest); |
Location loc = getChestLocation(server, chest); |
145 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
146 |
|
|
147 |
|
if (chest.isDoublechest()) { |
148 |
|
Location loc2 = getNeighborChest(loc); |
149 |
|
chestMap.put(loc2, chest); |
150 |
|
} |
151 |
} |
} |
152 |
|
|
153 |
|
return chestlist.size(); |
154 |
|
} |
155 |
|
|
156 |
|
void reloadChests() { |
157 |
|
chestMap.clear(); |
158 |
|
loadChestsWorker(); |
159 |
|
} |
160 |
|
|
161 |
|
void loadChests() { |
162 |
|
int count = loadChestsWorker(); |
163 |
|
plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests"); |
164 |
} |
} |
165 |
|
|
166 |
|
|
223 |
} else { |
} else { |
224 |
loc = ( (DoubleChest)holder).getLocation(); |
loc = ( (DoubleChest)holder).getLocation(); |
225 |
} |
} |
226 |
|
|
227 |
|
loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5 |
228 |
|
loc.setZ( loc.getBlockZ() ); // -- // -- |
229 |
|
|
230 |
return loc; |
return loc; |
231 |
} |
} |
232 |
|
|
233 |
@EventHandler |
@EventHandler |
234 |
|
public void onChestPlaced(BlockPlaceEvent event) { |
235 |
|
Block block = event.getBlock(); |
236 |
|
|
237 |
|
if (block.getType() != Material.CHEST) { |
238 |
|
return; |
239 |
|
} |
240 |
|
|
241 |
|
Location chestloc = getNeighborChest( block.getLocation() ); |
242 |
|
if (chestloc != null) { |
243 |
|
SnitchingChestBean chest = chestMap.get(chestloc); |
244 |
|
|
245 |
|
if (chest != null) { //the neighbor is a snitching chest |
246 |
|
//SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc); |
247 |
|
//plugin.getDatabase().save(chest); |
248 |
|
|
249 |
|
chest.setDoublechest(true); |
250 |
|
addChest(chestloc, chest); |
251 |
|
|
252 |
|
|
253 |
|
event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" ); |
254 |
|
} |
255 |
|
|
256 |
|
} |
257 |
|
} |
258 |
|
|
259 |
|
@EventHandler |
260 |
public void onChestOpen(InventoryOpenEvent event) { |
public void onChestOpen(InventoryOpenEvent event) { |
261 |
|
|
262 |
if (! (event.getPlayer() instanceof Player)) { |
if (! (event.getPlayer() instanceof Player)) { |
330 |
|
|
331 |
|
|
332 |
int diff = Math.abs( savedcount - count); |
int diff = Math.abs( savedcount - count); |
|
String msg = null; |
|
|
if (count > savedcount) { |
|
|
msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest"; |
|
|
} |
|
|
if (count < savedcount) { |
|
|
msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest"; |
|
|
} |
|
333 |
|
|
334 |
if (msg != null) { |
if (diff > 0) { |
335 |
|
String material = Material.getMaterial(item).name(); |
336 |
|
String msg = null; |
337 |
|
|
338 |
|
if (count > savedcount) { |
339 |
|
msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
340 |
|
} else { //(count < savedcount) |
341 |
|
msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
plugin.getLogger().info(msg); |
plugin.getLogger().info(msg); |
346 |
plugin.getMessageWrapper().sendMessage(owner, msg); |
plugin.getMessageWrapper().sendMessage(owner, msg); |
347 |
} |
} |