1 |
package dk.thoerup.bukkit.hoeruputils; |
package dk.thoerup.bukkit.hoeruputils.chests; |
2 |
|
|
3 |
|
|
4 |
import java.util.HashMap; |
import java.util.HashMap; |
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; |
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 |
|
|
33 |
|
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
34 |
|
|
35 |
|
|
36 |
public class SnitchingChest implements Listener, CommandExecutor{ |
public class SnitchingChest implements Listener, CommandExecutor{ |
37 |
|
|
85 |
if (chest.getOwner().equals(player.getName())) { |
if (chest.getOwner().equals(player.getName())) { |
86 |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
87 |
removeChest(loc); |
removeChest(loc); |
|
if (loc2 != null) { |
|
|
removeChest(loc2); |
|
|
} |
|
88 |
} else { |
} else { |
89 |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
90 |
} |
} |
92 |
return true; |
return true; |
93 |
} |
} |
94 |
|
|
95 |
SnitchingChestBean chest1 = createChest(player.getName(), "", loc); |
chest = createChest(player.getName(), "", loc); |
|
addChest(loc, chest1); |
|
96 |
if (loc2 != null) { |
if (loc2 != null) { |
97 |
|
chest.setDoublechest(true); |
|
SnitchingChestBean chest2 = createChest (player.getName(), "", loc2); |
|
|
addChest(loc, chest2); |
|
98 |
} |
} |
99 |
|
addChest(loc, chest); |
100 |
|
|
101 |
|
|
102 |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
103 |
|
|
121 |
} |
} |
122 |
public void addChest(Location loc, SnitchingChestBean chest) { |
public void addChest(Location loc, SnitchingChestBean chest) { |
123 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
124 |
|
if (chest.isDoublechest()) { |
125 |
|
Location loc2 = getNeighborChest(loc); |
126 |
|
chestMap.put(loc2, chest); |
127 |
|
} |
128 |
plugin.getDatabase().save(chest); |
plugin.getDatabase().save(chest); |
129 |
|
|
130 |
|
reloadChests(); |
131 |
|
|
132 |
} |
} |
133 |
|
|
134 |
void removeChest(Location loc) { |
void removeChest(Location loc) { |
135 |
SnitchingChestBean chest = chestMap.remove(loc); |
SnitchingChestBean chest = chestMap.remove(loc); |
136 |
if (chest != null) { |
if (chest != null) { |
137 |
|
if (chest.isDoublechest()){ |
138 |
|
Location loc2 = getNeighborChest(loc); |
139 |
|
chestMap.remove(loc2); |
140 |
|
} |
141 |
plugin.getDatabase().delete(chest); |
plugin.getDatabase().delete(chest); |
142 |
} |
} |
143 |
} |
} |
144 |
|
|
145 |
void loadChests() { |
int loadChestsWorker() { |
146 |
List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList(); |
List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList(); |
147 |
for (SnitchingChestBean chest : chestlist) { |
for (SnitchingChestBean chest : chestlist) { |
148 |
Location loc = getChestLocation(server, chest); |
Location loc = getChestLocation(server, chest); |
149 |
chestMap.put(loc, chest); |
chestMap.put(loc, chest); |
150 |
|
|
151 |
|
if (chest.isDoublechest()) { |
152 |
|
Location loc2 = getNeighborChest(loc); |
153 |
|
chestMap.put(loc2, chest); |
154 |
|
} |
155 |
} |
} |
156 |
|
|
157 |
plugin.getLogger().info("[SnitchingChest] loaded " + chestMap.size() + " chests"); |
return chestlist.size(); |
158 |
|
} |
159 |
|
|
160 |
|
void reloadChests() { |
161 |
|
chestMap.clear(); |
162 |
|
loadChestsWorker(); |
163 |
|
} |
164 |
|
|
165 |
|
void loadChests() { |
166 |
|
int count = loadChestsWorker(); |
167 |
|
plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests"); |
168 |
} |
} |
169 |
|
|
170 |
|
|
247 |
SnitchingChestBean chest = chestMap.get(chestloc); |
SnitchingChestBean chest = chestMap.get(chestloc); |
248 |
|
|
249 |
if (chest != null) { //the neighbor is a snitching chest |
if (chest != null) { //the neighbor is a snitching chest |
250 |
SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc); |
//SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc); |
251 |
addChest(chestloc, newchest); |
//plugin.getDatabase().save(chest); |
252 |
|
|
253 |
|
chest.setDoublechest(true); |
254 |
|
addChest(chestloc, chest); |
255 |
|
|
256 |
|
|
257 |
event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" ); |
event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" ); |
258 |
} |
} |
259 |
|
|
260 |
} |
} |
261 |
} |
} |
262 |
|
|
263 |
|
/* |
264 |
|
* how to prevent a user from opening a chest - usefull if SnitchingChest should morph into a LockedChest |
265 |
|
@EventHandler |
266 |
|
public void onChestInteract(PlayerInteractEvent event) { |
267 |
|
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { |
268 |
|
Block b = event.getClickedBlock(); |
269 |
|
|
270 |
|
if (b.getType() == Material.CHEST) { |
271 |
|
event.setCancelled(true); |
272 |
|
} |
273 |
|
} |
274 |
|
} |
275 |
|
*/ |
276 |
|
|
277 |
@EventHandler |
@EventHandler |
278 |
public void onChestOpen(InventoryOpenEvent event) { |
public void onChestOpen(InventoryOpenEvent event) { |
279 |
|
|
281 |
return; |
return; |
282 |
} |
} |
283 |
|
|
284 |
|
|
285 |
|
|
286 |
InventoryHolder holder = event.getInventory().getHolder(); |
InventoryHolder holder = event.getInventory().getHolder(); |
287 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
355 |
String msg = null; |
String msg = null; |
356 |
|
|
357 |
if (count > savedcount) { |
if (count > savedcount) { |
358 |
msg = player.getName() + " added " + diff + " units of " + material + "(" +item + ") to " + owner.getName() + "'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(); |
359 |
} else { //(count < savedcount) |
} else { //(count < savedcount) |
360 |
msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest"; |
msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ(); |
361 |
} |
} |
362 |
|
|
363 |
|
|
364 |
plugin.getLogger().info(msg); |
plugin.getLogger().info(msg); |
365 |
plugin.getMessageWrapper().sendMessage(owner, msg); |
plugin.getMessageWrapper().sendMessage("system", owner, msg); |
366 |
} |
} |
367 |
|
|
368 |
} |
} |