1 |
package dk.thoerup.bukkit.hoeruputils.chests; |
2 |
|
3 |
|
4 |
import java.util.HashMap; |
5 |
import java.util.List; |
6 |
|
7 |
import org.bukkit.ChatColor; |
8 |
import org.bukkit.Location; |
9 |
import org.bukkit.Material; |
10 |
import org.bukkit.Server; |
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.Action; |
22 |
import org.bukkit.event.block.BlockBreakEvent; |
23 |
import org.bukkit.event.block.BlockPlaceEvent; |
24 |
import org.bukkit.event.player.PlayerInteractEvent; |
25 |
import org.bukkit.inventory.InventoryHolder; |
26 |
|
27 |
import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin; |
28 |
|
29 |
|
30 |
public class LockedChest implements Listener, CommandExecutor{ |
31 |
|
32 |
|
33 |
HashMap<Location,LockedChestBean> chestMap = new HashMap<Location, LockedChestBean>(); |
34 |
|
35 |
|
36 |
HoerupUtilsPlugin plugin; |
37 |
Server server; |
38 |
|
39 |
public LockedChest(HoerupUtilsPlugin plugin, Runnable r) { |
40 |
this.plugin = plugin; |
41 |
server = plugin.getServer(); |
42 |
try { |
43 |
loadChests(); |
44 |
} catch (Exception e) { |
45 |
e.printStackTrace(); |
46 |
//r.run(); |
47 |
loadChests(); |
48 |
} |
49 |
} |
50 |
|
51 |
|
52 |
@Override |
53 |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
54 |
if (! (sender instanceof Player) ) { |
55 |
sender.sendMessage("this is not a console command!"); |
56 |
return true; |
57 |
} |
58 |
|
59 |
Player player = (Player) sender; |
60 |
|
61 |
|
62 |
Block b = player.getTargetBlock(null, 30); |
63 |
Location loc = b.getLocation(); |
64 |
|
65 |
if (b.getTypeId() != 54) { |
66 |
player.sendMessage("[LockedChest] Please look at the chest you want to lock"); |
67 |
return true; |
68 |
} |
69 |
|
70 |
Location loc2 = getNeighborChest(loc); |
71 |
|
72 |
LockedChestBean chest = chestMap.get(loc); |
73 |
if (chest != null) { |
74 |
if (chest.getOwner().equals(player.getName())) { |
75 |
player.sendMessage("[LockedChest] Removing lock from chest"); |
76 |
removeChest(loc); |
77 |
} else { |
78 |
player.sendMessage("[LockedChest] Chest is already locked"); |
79 |
} |
80 |
|
81 |
return true; |
82 |
} |
83 |
|
84 |
chest = createChest(player.getName(), "", loc); |
85 |
if (loc2 != null) { |
86 |
chest.setDoublechest(true); |
87 |
} |
88 |
|
89 |
addChest(loc, chest); |
90 |
|
91 |
|
92 |
player.sendMessage("[LockedChest] Chest is now locked"); |
93 |
|
94 |
|
95 |
return true; |
96 |
} |
97 |
|
98 |
@EventHandler |
99 |
public void onBlockBreak(BlockBreakEvent event) { |
100 |
Location loc = event.getBlock().getLocation(); |
101 |
LockedChestBean chest = chestMap.get(loc); |
102 |
if (chest != null) { |
103 |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
104 |
removeChest(loc); |
105 |
event.getPlayer().sendMessage("[LockedChest] The destroyed chest was locked"); |
106 |
} else { |
107 |
event.setCancelled(true); |
108 |
event.getPlayer().sendMessage("You can't destroy that chest"); |
109 |
} |
110 |
} |
111 |
} |
112 |
public void addChest(Location loc, LockedChestBean chest) { |
113 |
chestMap.put(loc, chest); |
114 |
if (chest.isDoublechest()) { |
115 |
Location loc2 = getNeighborChest(loc); |
116 |
chestMap.put(loc2, chest); |
117 |
} |
118 |
plugin.getDatabase().save(chest); |
119 |
|
120 |
reloadChests(); |
121 |
|
122 |
} |
123 |
|
124 |
void removeChest(Location loc) { |
125 |
LockedChestBean chest = chestMap.remove(loc); |
126 |
if (chest != null) { |
127 |
if (chest.isDoublechest()){ |
128 |
Location loc2 = getNeighborChest(loc); |
129 |
chestMap.remove(loc2); |
130 |
} |
131 |
plugin.getDatabase().delete(chest); |
132 |
} |
133 |
} |
134 |
|
135 |
int loadChestsWorker() { |
136 |
List<LockedChestBean> chestlist = plugin.getDatabase().find( LockedChestBean.class).findList(); |
137 |
for (LockedChestBean chest : chestlist) { |
138 |
Location loc = getChestLocation(server, chest); |
139 |
chestMap.put(loc, chest); |
140 |
|
141 |
if (chest.isDoublechest()) { |
142 |
Location loc2 = getNeighborChest(loc); |
143 |
chestMap.put(loc2, chest); |
144 |
} |
145 |
} |
146 |
|
147 |
return chestlist.size(); |
148 |
} |
149 |
|
150 |
void reloadChests() { |
151 |
chestMap.clear(); |
152 |
loadChestsWorker(); |
153 |
} |
154 |
|
155 |
void loadChests() { |
156 |
int count = loadChestsWorker(); |
157 |
plugin.getLogger().info("[LockedChest] loaded " + count + " chests"); |
158 |
} |
159 |
|
160 |
|
161 |
public LockedChestBean createChest(String owner, String description, Location loc) { |
162 |
|
163 |
LockedChestBean chest = new LockedChestBean(); |
164 |
chest.setOwner(owner); |
165 |
chest.setDescription(description); |
166 |
setChestLocation(chest, loc); |
167 |
|
168 |
return chest; |
169 |
} |
170 |
|
171 |
|
172 |
public void setChestLocation(LockedChestBean chest, Location loc) { |
173 |
chest.setWorld( loc.getWorld().getName() ); |
174 |
chest.setX( loc.getBlockX() ); |
175 |
chest.setY( loc.getBlockY() ); |
176 |
chest.setZ( loc.getBlockZ() ); |
177 |
} |
178 |
|
179 |
public Location getChestLocation(Server server, LockedChestBean chest) { |
180 |
World wrld = server.getWorld(chest.getWorld()); |
181 |
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
182 |
} |
183 |
|
184 |
|
185 |
/* |
186 |
void saveChests() { |
187 |
|
188 |
}*/ |
189 |
|
190 |
Location getNeighborChest(Location loc) { |
191 |
World world = loc.getWorld(); |
192 |
|
193 |
Location target = new Location(world, loc.getX()+1, loc.getY(), loc.getZ() ); |
194 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
195 |
return target; |
196 |
|
197 |
target = new Location(world, loc.getX()-1, loc.getY(), loc.getZ() ); |
198 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
199 |
return target; |
200 |
|
201 |
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() +1); |
202 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
203 |
return target; |
204 |
|
205 |
target = new Location(world, loc.getX(), loc.getY(), loc.getZ() -1); |
206 |
if (world.getBlockAt(target).getType() == Material.CHEST ) |
207 |
return target; |
208 |
|
209 |
return null; |
210 |
} |
211 |
|
212 |
|
213 |
Location getChestLocation(InventoryHolder holder) { |
214 |
Location loc; |
215 |
if ( holder instanceof Chest) { |
216 |
loc = ( (Chest)holder).getLocation(); |
217 |
} else { |
218 |
loc = ( (DoubleChest)holder).getLocation(); |
219 |
} |
220 |
|
221 |
loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5 |
222 |
loc.setZ( loc.getBlockZ() ); // -- // -- |
223 |
|
224 |
return loc; |
225 |
} |
226 |
|
227 |
@EventHandler |
228 |
public void onChestPlaced(BlockPlaceEvent event) { |
229 |
Block block = event.getBlock(); |
230 |
|
231 |
if (block.getType() != Material.CHEST) { |
232 |
return; |
233 |
} |
234 |
|
235 |
Location chestloc = getNeighborChest( block.getLocation() ); |
236 |
if (chestloc != null) { |
237 |
LockedChestBean chest = chestMap.get(chestloc); |
238 |
|
239 |
if (chest != null) { //the neighbor is a locked chest |
240 |
|
241 |
|
242 |
chest.setDoublechest(true); |
243 |
addChest(chestloc, chest); |
244 |
|
245 |
|
246 |
event.getPlayer().sendMessage( "[LockedChest] Chest has been expanded" ); |
247 |
} |
248 |
|
249 |
} |
250 |
} |
251 |
|
252 |
|
253 |
// prevent a user from opening a chest |
254 |
@EventHandler |
255 |
public void onChestInteract(PlayerInteractEvent event) { |
256 |
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { |
257 |
Block b = event.getClickedBlock(); |
258 |
|
259 |
if (b.getType() == Material.CHEST) { |
260 |
|
261 |
Location loc = b.getLocation(); |
262 |
|
263 |
LockedChestBean chest = chestMap.get( loc ); |
264 |
if (chest == null) { |
265 |
return; //chest not surveyed by this plugin |
266 |
} |
267 |
|
268 |
Player player = (Player) event.getPlayer(); |
269 |
if (player.getName().equals(chest.getOwner() )) { |
270 |
return; //chest is opened by it's owner |
271 |
} |
272 |
|
273 |
if (chest.getModifyPlayers() != null && chest.getModifyPlayers().length()>0) { |
274 |
String modplayers[] = chest.getModifyPlayers().split(","); |
275 |
for (String p : modplayers) { |
276 |
if ( player.getName().equals(p) ) { |
277 |
return; //this player is on the whitelist so he may open |
278 |
} |
279 |
} |
280 |
} |
281 |
|
282 |
|
283 |
player.sendMessage( ChatColor.BLUE + "Sorry but this chest is locked !"); |
284 |
event.setCancelled(true); |
285 |
} |
286 |
} |
287 |
} |
288 |
|
289 |
} |