/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/LockedChest.java
ViewVC logotype

Contents of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/LockedChest.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20