3 |
import org.bukkit.ChatColor; |
import org.bukkit.ChatColor; |
4 |
import org.bukkit.Location; |
import org.bukkit.Location; |
5 |
import org.bukkit.Server; |
import org.bukkit.Server; |
6 |
|
import org.bukkit.World; |
7 |
import org.bukkit.command.Command; |
import org.bukkit.command.Command; |
8 |
import org.bukkit.command.CommandExecutor; |
import org.bukkit.command.CommandExecutor; |
9 |
import org.bukkit.command.CommandSender; |
import org.bukkit.command.CommandSender; |
16 |
import org.bukkit.event.player.PlayerPortalEvent; |
import org.bukkit.event.player.PlayerPortalEvent; |
17 |
import org.bukkit.event.player.PlayerMoveEvent; |
import org.bukkit.event.player.PlayerMoveEvent; |
18 |
import org.bukkit.event.player.PlayerJoinEvent; |
import org.bukkit.event.player.PlayerJoinEvent; |
19 |
|
import org.bukkit.event.player.PlayerRespawnEvent; |
20 |
|
|
21 |
import org.bukkit.event.block.BlockBreakEvent; |
import org.bukkit.event.block.BlockBreakEvent; |
22 |
import org.bukkit.event.block.BlockPlaceEvent; |
import org.bukkit.event.block.BlockPlaceEvent; |
23 |
|
|
24 |
|
import org.bukkit.configuration.file.YamlConfiguration; |
25 |
|
|
26 |
|
import java.io.File; |
27 |
|
import java.io.*; |
28 |
|
|
29 |
import java.util.ArrayList; |
import java.util.ArrayList; |
30 |
|
import java.util.Set; |
31 |
import java.util.TreeSet; |
import java.util.TreeSet; |
32 |
|
|
33 |
public class Jail implements CommandExecutor, Listener { |
public class Jail implements CommandExecutor, Listener { |
44 |
public Jail(Plugin plugin) { |
public Jail(Plugin plugin) { |
45 |
this.plugin = plugin; |
this.plugin = plugin; |
46 |
|
|
|
jailLocation = new Location( plugin.getServer().getWorld("world"), 528.0, 68.0, 57); |
|
|
releaseLocation = new Location( plugin.getServer().getWorld("world"), 124.0, 68.0, 78); |
|
47 |
|
|
48 |
jailed.add("hoerup"); |
load(); |
49 |
} |
} |
50 |
|
|
51 |
@Override |
public void load() { |
52 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
File configFile = new File(plugin.getDataFolder(), "jail.yml"); |
53 |
if (! (sender instanceof Player) ) { |
|
54 |
sender.sendMessage("only use these commands in-game"); |
if (!configFile.exists() ) { |
55 |
return true; |
System.out.println("[HoerupJail] jail.yml not found"); |
56 |
|
} |
57 |
|
YamlConfiguration config = new YamlConfiguration(); |
58 |
|
try { |
59 |
|
config.load(configFile); |
60 |
|
} catch (Exception e) { |
61 |
|
System.out.println("[HoerupJail] exception loading jails : " + e.getMessage() ); |
62 |
|
return; |
63 |
} |
} |
64 |
|
|
65 |
Player p = (Player) sender; |
jailLocation = loadLocation(config, plugin.getServer(), "jailloc"); |
66 |
|
releaseLocation = loadLocation(config, plugin.getServer(), "releaseloc"); |
67 |
|
|
68 |
if ( !p.isOp()) { |
Set<String> tmpSet = (Set<String>) config.get("jails"); |
69 |
p.sendMessage("Only server operators may use this command"); |
jailed.addAll(tmpSet); |
70 |
return true; |
} |
71 |
|
|
72 |
|
public void save() { |
73 |
|
File configFile = new File(plugin.getDataFolder(), "jail.yml"); |
74 |
|
|
75 |
|
YamlConfiguration config = new YamlConfiguration(); |
76 |
|
saveLocation(config, jailLocation, "jailloc"); |
77 |
|
saveLocation(config, jailLocation, "releaseloc"); |
78 |
|
config.set("jails", jailed); |
79 |
|
|
80 |
|
try { |
81 |
|
config.save(configFile); |
82 |
|
} catch (java.io.IOException e) { |
83 |
|
System.out.println("[HoerupJial] exception saving jails : " + e.getMessage() ); |
84 |
} |
} |
85 |
|
|
86 |
|
} |
87 |
|
|
88 |
|
private static Location loadLocation(YamlConfiguration config, Server server, String prefix) { |
89 |
|
String world = config.getString( prefix + ".world"); |
90 |
|
double x = config.getDouble( prefix + ".x"); |
91 |
|
double y = config.getDouble( prefix + ".y"); |
92 |
|
double z = config.getDouble( prefix + ".z"); |
93 |
|
float pitch = (float) config.getDouble( prefix + ".pitch"); |
94 |
|
float yaw = (float) config.getDouble( prefix + ".yaw"); |
95 |
|
|
96 |
|
World w = server.getWorld(world); |
97 |
|
|
98 |
|
return new Location(w,x,y,z,yaw,pitch); |
99 |
|
} |
100 |
|
|
101 |
|
private static void saveLocation(YamlConfiguration config, Location location, String prefix) { |
102 |
|
if (location == null) |
103 |
|
return; |
104 |
|
|
105 |
|
config.set( prefix + ".world", location.getWorld().getName() ); |
106 |
|
config.set( prefix + ".x", location.getX() ); |
107 |
|
config.set( prefix + ".y", location.getY() ); |
108 |
|
config.set( prefix + ".z", location.getZ() ); |
109 |
|
config.set( prefix + ".pitch", location.getPitch() ); |
110 |
|
config.set( prefix + ".yaw", location.getYaw() ); |
111 |
|
} |
112 |
|
|
113 |
|
@Override |
114 |
|
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
115 |
if (args.length == 0) { |
if (args.length == 0) { |
116 |
p.sendMessage("Usage: /jail <imprison|release|list|tpjail|tprelease>"); |
sender.sendMessage("Usage: /jail <imprison|release|list|tpjail|tprelease|setjail|setrelease>"); |
117 |
return true; |
return true; |
118 |
} |
} |
119 |
|
|
120 |
|
if (sender instanceof Player ) { |
121 |
|
Player p = (Player) sender; |
122 |
|
if ( !p.isOp()) { |
123 |
|
p.sendMessage("Only server operators may use this command"); |
124 |
|
return true; |
125 |
|
} |
126 |
|
} else { |
127 |
|
if ( args[0].equals("imprison") || args[0].equals("release") || args[0].equals("list") ) { |
128 |
|
//do nothing |
129 |
|
} else { |
130 |
|
sender.sendMessage("only imprison, release and list may be used from console"); |
131 |
|
return true; |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
if (args[0].equals("imprison")) { |
if (args[0].equals("imprison")) { |
137 |
if (args.length != 2) { |
if (args.length != 2) { |
138 |
|
sender.sendMessage("You must specify the player"); |
139 |
return true; |
return true; |
140 |
} |
} |
141 |
|
|
142 |
Player inmate = sender.getServer().getPlayer( args[1] ); |
Player inmate = sender.getServer().getPlayer( args[1] ); |
143 |
if (inmate == null) { |
if (inmate == null) { |
144 |
p.sendMessage("Player not found"); |
sender.sendMessage("Player not found"); |
145 |
return true; |
return true; |
146 |
} |
} |
147 |
|
|
149 |
inmate.sendMessage("You have been imprisoned"); |
inmate.sendMessage("You have been imprisoned"); |
150 |
inmate.teleport( jailLocation ); |
inmate.teleport( jailLocation ); |
151 |
jailed.add( inmate.getName() ); |
jailed.add( inmate.getName() ); |
152 |
|
save(); |
153 |
return true; |
return true; |
154 |
} |
} |
155 |
|
|
156 |
if (args[0].equals("release")) { |
if (args[0].equals("release")) { |
157 |
if (args.length != 2) { |
if (args.length != 2) { |
158 |
|
sender.sendMessage("You must specify the player"); |
159 |
return true; |
return true; |
160 |
} |
} |
161 |
|
|
162 |
Player inmate = sender.getServer().getPlayer( args[1] ); |
Player inmate = sender.getServer().getPlayer( args[1] ); |
163 |
if (inmate == null) { |
if (inmate == null) { |
164 |
p.sendMessage("Player not found"); |
sender.sendMessage("Player not found"); |
165 |
return true; |
return true; |
166 |
} |
} |
167 |
|
|
169 |
jailed.remove( inmate.getName() ); |
jailed.remove( inmate.getName() ); |
170 |
inmate.sendMessage("You have been released from prison"); |
inmate.sendMessage("You have been released from prison"); |
171 |
inmate.teleport( releaseLocation ); |
inmate.teleport( releaseLocation ); |
172 |
|
save(); |
173 |
return true; |
return true; |
174 |
} |
} |
175 |
|
|
186 |
} |
} |
187 |
return true; |
return true; |
188 |
} |
} |
189 |
|
|
190 |
|
if (args[0].equals("setjail")) { |
191 |
|
Player p = (Player) sender; |
192 |
|
jailLocation = p.getLocation().clone(); |
193 |
|
p.sendMessage("New jail location has been set"); |
194 |
|
save(); |
195 |
|
return true; |
196 |
|
} |
197 |
|
if (args[0].equals("setrelease")) { |
198 |
|
Player p = (Player) sender; |
199 |
|
releaseLocation = p.getLocation().clone(); |
200 |
|
p.sendMessage("New release location has been set"); |
201 |
|
save(); |
202 |
|
return true; |
203 |
|
} |
204 |
|
|
205 |
if (args[0].equals("tpjail")) { |
if (args[0].equals("tpjail")) { |
206 |
|
Player p = (Player) sender; |
207 |
p.teleport(jailLocation); |
p.teleport(jailLocation); |
208 |
return true; |
return true; |
209 |
} |
} |
210 |
if (args[0].equals("tprelease")) { |
if (args[0].equals("tprelease")) { |
211 |
|
Player p = (Player) sender; |
212 |
p.teleport(releaseLocation); |
p.teleport(releaseLocation); |
213 |
return true; |
return true; |
214 |
} |
} |
238 |
Player p = event.getPlayer(); |
Player p = event.getPlayer(); |
239 |
if (jailed.contains( p.getName() ) ) { |
if (jailed.contains( p.getName() ) ) { |
240 |
p.teleport( jailLocation ); |
p.teleport( jailLocation ); |
241 |
|
p.sendMessage("You are currently in jail and have been teleported to you cell"); |
242 |
} |
} |
243 |
} |
} |
244 |
|
|
290 |
event.setCancelled( true ); |
event.setCancelled( true ); |
291 |
} |
} |
292 |
} |
} |
293 |
|
|
294 |
|
@EventHandler |
295 |
|
public void onRespawn(PlayerRespawnEvent event) { |
296 |
|
Player p = event.getPlayer(); |
297 |
|
if (jailed.contains( p.getName() ) ) { |
298 |
|
event.setRespawnLocation( jailLocation ); |
299 |
|
p.teleport( jailLocation ); |
300 |
|
} |
301 |
|
} |
302 |
} |
} |
303 |
|
|