1 |
package dk.thoerup.bukkit.hoeruputils; |
2 |
|
3 |
import org.bukkit.ChatColor; |
4 |
import org.bukkit.Location; |
5 |
import org.bukkit.Server; |
6 |
import org.bukkit.World; |
7 |
import org.bukkit.command.Command; |
8 |
import org.bukkit.command.CommandExecutor; |
9 |
import org.bukkit.command.CommandSender; |
10 |
import org.bukkit.entity.Player; |
11 |
import org.bukkit.plugin.Plugin; |
12 |
|
13 |
import org.bukkit.event.Listener; |
14 |
import org.bukkit.event.EventHandler; |
15 |
import org.bukkit.event.player.PlayerTeleportEvent; |
16 |
import org.bukkit.event.player.PlayerPortalEvent; |
17 |
import org.bukkit.event.player.PlayerMoveEvent; |
18 |
import org.bukkit.event.player.PlayerJoinEvent; |
19 |
|
20 |
import org.bukkit.event.block.BlockBreakEvent; |
21 |
import org.bukkit.event.block.BlockPlaceEvent; |
22 |
|
23 |
import org.bukkit.configuration.file.YamlConfiguration; |
24 |
|
25 |
import java.io.File; |
26 |
import java.io.*; |
27 |
|
28 |
import java.util.ArrayList; |
29 |
import java.util.Set; |
30 |
import java.util.TreeSet; |
31 |
|
32 |
public class Jail implements CommandExecutor, Listener { |
33 |
|
34 |
Plugin plugin; |
35 |
|
36 |
TreeSet<String> jailed = new TreeSet<String>(); |
37 |
|
38 |
Location jailLocation; |
39 |
Location releaseLocation; |
40 |
|
41 |
final double maxDist = 25.0; |
42 |
|
43 |
public Jail(Plugin plugin) { |
44 |
this.plugin = plugin; |
45 |
|
46 |
|
47 |
load(); |
48 |
} |
49 |
|
50 |
public void load() { |
51 |
File configFile = new File(plugin.getDataFolder(), "jail.yml"); |
52 |
|
53 |
if (!configFile.exists() ) { |
54 |
System.out.println("[HoerupJail] jail.yml not found"); |
55 |
} |
56 |
YamlConfiguration config = new YamlConfiguration(); |
57 |
try { |
58 |
config.load(configFile); |
59 |
} catch (Exception e) { |
60 |
System.out.println("[HoerupJail] exception loading jails : " + e.getMessage() ); |
61 |
return; |
62 |
} |
63 |
|
64 |
jailLocation = loadLocation(config, plugin.getServer(), "jailloc"); |
65 |
releaseLocation = loadLocation(config, plugin.getServer(), "releaseloc"); |
66 |
|
67 |
Set<String> tmpSet = (Set<String>) config.get("jails"); |
68 |
jailed.addAll(tmpSet); |
69 |
} |
70 |
|
71 |
public void save() { |
72 |
File configFile = new File(plugin.getDataFolder(), "jail.yml"); |
73 |
|
74 |
YamlConfiguration config = new YamlConfiguration(); |
75 |
saveLocation(config, jailLocation, "jailloc"); |
76 |
saveLocation(config, jailLocation, "releaseloc"); |
77 |
config.set("jails", jailed); |
78 |
|
79 |
try { |
80 |
config.save(configFile); |
81 |
} catch (java.io.IOException e) { |
82 |
System.out.println("[HoerupJial] exception saving jails : " + e.getMessage() ); |
83 |
} |
84 |
|
85 |
} |
86 |
|
87 |
private static Location loadLocation(YamlConfiguration config, Server server, String prefix) { |
88 |
String world = config.getString( prefix + ".world"); |
89 |
double x = config.getDouble( prefix + ".x"); |
90 |
double y = config.getDouble( prefix + ".y"); |
91 |
double z = config.getDouble( prefix + ".z"); |
92 |
float pitch = (float) config.getDouble( prefix + ".pitch"); |
93 |
float yaw = (float) config.getDouble( prefix + ".yaw"); |
94 |
|
95 |
World w = server.getWorld(world); |
96 |
|
97 |
return new Location(w,x,y,z,yaw,pitch); |
98 |
} |
99 |
|
100 |
private static void saveLocation(YamlConfiguration config, Location location, String prefix) { |
101 |
if (location == null) |
102 |
return; |
103 |
|
104 |
config.set( prefix + ".world", location.getWorld().getName() ); |
105 |
config.set( prefix + ".x", location.getX() ); |
106 |
config.set( prefix + ".y", location.getY() ); |
107 |
config.set( prefix + ".z", location.getZ() ); |
108 |
config.set( prefix + ".pitch", location.getPitch() ); |
109 |
config.set( prefix + ".yaw", location.getYaw() ); |
110 |
} |
111 |
|
112 |
@Override |
113 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
114 |
if (! (sender instanceof Player) ) { |
115 |
sender.sendMessage("only use these commands in-game"); |
116 |
return true; |
117 |
} |
118 |
|
119 |
Player p = (Player) sender; |
120 |
|
121 |
if ( !p.isOp()) { |
122 |
p.sendMessage("Only server operators may use this command"); |
123 |
return true; |
124 |
} |
125 |
|
126 |
if (args.length == 0) { |
127 |
p.sendMessage("Usage: /jail <imprison|release|list|tpjail|tprelease|setjail|setrelease>"); |
128 |
return true; |
129 |
} |
130 |
|
131 |
if (args[0].equals("imprison")) { |
132 |
if (args.length != 2) { |
133 |
return true; |
134 |
} |
135 |
|
136 |
Player inmate = sender.getServer().getPlayer( args[1] ); |
137 |
if (inmate == null) { |
138 |
p.sendMessage("Player not found"); |
139 |
return true; |
140 |
} |
141 |
|
142 |
|
143 |
inmate.sendMessage("You have been imprisoned"); |
144 |
inmate.teleport( jailLocation ); |
145 |
jailed.add( inmate.getName() ); |
146 |
save(); |
147 |
return true; |
148 |
} |
149 |
|
150 |
if (args[0].equals("release")) { |
151 |
if (args.length != 2) { |
152 |
return true; |
153 |
} |
154 |
|
155 |
Player inmate = sender.getServer().getPlayer( args[1] ); |
156 |
if (inmate == null) { |
157 |
p.sendMessage("Player not found"); |
158 |
return true; |
159 |
} |
160 |
|
161 |
|
162 |
jailed.remove( inmate.getName() ); |
163 |
inmate.sendMessage("You have been released from prison"); |
164 |
inmate.teleport( releaseLocation ); |
165 |
save(); |
166 |
return true; |
167 |
} |
168 |
|
169 |
if (args[0].equals("list") ) { |
170 |
if (jailed.size() > 0) { |
171 |
StringBuffer sb = new StringBuffer(); |
172 |
for (String j : jailed ) { |
173 |
sb.append(", "); |
174 |
sb.append(j); |
175 |
} |
176 |
sender.sendMessage("Current inmates: " + sb.substring(2) ); |
177 |
} else { |
178 |
sender.sendMessage("There are currently no inmates"); |
179 |
} |
180 |
return true; |
181 |
} |
182 |
|
183 |
if (args[0].equals("setjail")) { |
184 |
jailLocation = p.getLocation().clone(); |
185 |
p.sendMessage("New jail location has been set"); |
186 |
save(); |
187 |
return true; |
188 |
} |
189 |
if (args[0].equals("setrelease")) { |
190 |
releaseLocation = p.getLocation().clone(); |
191 |
p.sendMessage("New release location has been set"); |
192 |
save(); |
193 |
return true; |
194 |
} |
195 |
|
196 |
if (args[0].equals("tpjail")) { |
197 |
p.teleport(jailLocation); |
198 |
return true; |
199 |
} |
200 |
if (args[0].equals("tprelease")) { |
201 |
p.teleport(releaseLocation); |
202 |
return true; |
203 |
} |
204 |
|
205 |
sender.sendMessage( "Unknown command: " + args[0] ); |
206 |
|
207 |
return true; |
208 |
} |
209 |
|
210 |
@EventHandler |
211 |
public void onPlayerMove(PlayerMoveEvent event) { |
212 |
Player p = event.getPlayer(); |
213 |
if (jailed.contains(p.getName() )) { |
214 |
Location to = event.getTo(); |
215 |
double xDist = Math.abs( to.getX() - jailLocation.getX() ); |
216 |
double zDist = Math.abs( to.getZ() - jailLocation.getZ() ); |
217 |
|
218 |
if ( xDist > maxDist || zDist > maxDist) { |
219 |
//System.out.println("" + xDist + " / " + zDist ); |
220 |
event.setCancelled(true ); |
221 |
} |
222 |
} |
223 |
} |
224 |
|
225 |
@EventHandler |
226 |
public void onJoin(PlayerJoinEvent event) { |
227 |
Player p = event.getPlayer(); |
228 |
if (jailed.contains( p.getName() ) ) { |
229 |
p.teleport( jailLocation ); |
230 |
p.sendMessage("You are currently in jail and have been teleported to you cell"); |
231 |
} |
232 |
} |
233 |
|
234 |
@EventHandler |
235 |
public void onBlockBreak(BlockBreakEvent event) { |
236 |
Player p = event.getPlayer(); |
237 |
if (jailed.contains( p.getName() ) ) { |
238 |
event.setCancelled( true ); |
239 |
} |
240 |
} |
241 |
@EventHandler |
242 |
public void onBlockPlace(BlockPlaceEvent event) { |
243 |
Player p = event.getPlayer(); |
244 |
if (jailed.contains( p.getName() ) ) { |
245 |
event.setCancelled( true ); |
246 |
} |
247 |
} |
248 |
|
249 |
@EventHandler |
250 |
public void onTeleport(PlayerTeleportEvent event) { |
251 |
Player p = event.getPlayer(); |
252 |
if (jailed.contains( p.getName() ) ) { |
253 |
|
254 |
//System.out.println( "Teleport: " + event.getCause().toString() ); |
255 |
|
256 |
Location from = event.getFrom(); |
257 |
Location to = event.getTo(); |
258 |
if ( from.getWorld() != to.getWorld() ) { |
259 |
p.sendMessage("You are jailed and may not teleport"); |
260 |
event.setCancelled(true ); |
261 |
return; |
262 |
} |
263 |
|
264 |
double xDist = Math.abs( to.getX() - jailLocation.getX() ); |
265 |
double zDist = Math.abs( to.getZ() - jailLocation.getZ() ); |
266 |
|
267 |
if ( xDist > maxDist || zDist > maxDist) { |
268 |
p.sendMessage("You are jailed and may not teleport"); |
269 |
event.setCancelled(true ); |
270 |
} |
271 |
} |
272 |
} |
273 |
|
274 |
@EventHandler |
275 |
public void onPortal(PlayerPortalEvent event) { |
276 |
Player p = event.getPlayer(); |
277 |
if (jailed.contains( p.getName() ) ) { |
278 |
p.sendMessage("You are jailed and may not use portals"); |
279 |
event.setCancelled( true ); |
280 |
} |
281 |
} |
282 |
} |
283 |
|