--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/Jail.java 2012/10/14 11:48:43 1857 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/Jail.java 2012/10/15 07:01:57 1859 @@ -3,6 +3,7 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Server; +import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -15,11 +16,18 @@ import org.bukkit.event.player.PlayerPortalEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.*; + import java.util.ArrayList; +import java.util.Set; import java.util.TreeSet; public class Jail implements CommandExecutor, Listener { @@ -36,13 +44,70 @@ public Jail(Plugin plugin) { this.plugin = plugin; - jailLocation = new Location( plugin.getServer().getWorld("world"), 528.0, 68.0, 57); - releaseLocation = new Location( plugin.getServer().getWorld("world"), 124.0, 68.0, 78); - jailed.add("hoerup"); + load(); + } + + public void load() { + File configFile = new File(plugin.getDataFolder(), "jail.yml"); + + if (!configFile.exists() ) { + System.out.println("[HoerupJail] jail.yml not found"); + } + YamlConfiguration config = new YamlConfiguration(); + try { + config.load(configFile); + } catch (Exception e) { + System.out.println("[HoerupJail] exception loading jails : " + e.getMessage() ); + return; + } + + jailLocation = loadLocation(config, plugin.getServer(), "jailloc"); + releaseLocation = loadLocation(config, plugin.getServer(), "releaseloc"); + + Set tmpSet = (Set) config.get("jails"); + jailed.addAll(tmpSet); } public void save() { + File configFile = new File(plugin.getDataFolder(), "jail.yml"); + + YamlConfiguration config = new YamlConfiguration(); + saveLocation(config, jailLocation, "jailloc"); + saveLocation(config, jailLocation, "releaseloc"); + config.set("jails", jailed); + + try { + config.save(configFile); + } catch (java.io.IOException e) { + System.out.println("[HoerupJial] exception saving jails : " + e.getMessage() ); + } + + } + + private static Location loadLocation(YamlConfiguration config, Server server, String prefix) { + String world = config.getString( prefix + ".world"); + double x = config.getDouble( prefix + ".x"); + double y = config.getDouble( prefix + ".y"); + double z = config.getDouble( prefix + ".z"); + float pitch = (float) config.getDouble( prefix + ".pitch"); + float yaw = (float) config.getDouble( prefix + ".yaw"); + + World w = server.getWorld(world); + + return new Location(w,x,y,z,yaw,pitch); + } + + private static void saveLocation(YamlConfiguration config, Location location, String prefix) { + if (location == null) + return; + + config.set( prefix + ".world", location.getWorld().getName() ); + config.set( prefix + ".x", location.getX() ); + config.set( prefix + ".y", location.getY() ); + config.set( prefix + ".z", location.getZ() ); + config.set( prefix + ".pitch", location.getPitch() ); + config.set( prefix + ".yaw", location.getYaw() ); } @Override @@ -215,5 +280,14 @@ event.setCancelled( true ); } } + + @EventHandler + public void onRespawn(PlayerRespawnEvent event) { + Player p = event.getPlayer(); + if (jailed.contains( p.getName() ) ) { + event.setRespawnLocation( jailLocation ); + p.teleport( jailLocation ); + } + } }