--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/Jail.java 2012/10/14 11:36:45 1856 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/Jail.java 2012/10/15 07:23:59 1860 @@ -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,30 +44,92 @@ 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(); } - @Override - public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { - if (! (sender instanceof Player) ) { - sender.sendMessage("only use these commands in-game"); - return true; + 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; } - Player p = (Player) sender; + jailLocation = loadLocation(config, plugin.getServer(), "jailloc"); + releaseLocation = loadLocation(config, plugin.getServer(), "releaseloc"); - if ( !p.isOp()) { - p.sendMessage("Only server operators may use this command"); - return true; + 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 + public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { if (args.length == 0) { - p.sendMessage("Usage: /jail "); + sender.sendMessage("Usage: /jail "); return true; } + + if (sender instanceof Player ) { + Player p = (Player) sender; + if ( !p.isOp()) { + p.sendMessage("Only server operators may use this command"); + return true; + } + } else { + if ( args[0] != "imprison" && args[0] != "release" && args[0] != "list") { + sender.sendMessage("only imprison, release and list may be used from console"); + return true; + } + } + if (args[0].equals("imprison")) { if (args.length != 2) { @@ -68,7 +138,7 @@ Player inmate = sender.getServer().getPlayer( args[1] ); if (inmate == null) { - p.sendMessage("Player not found"); + sender.sendMessage("Player not found"); return true; } @@ -76,6 +146,7 @@ inmate.sendMessage("You have been imprisoned"); inmate.teleport( jailLocation ); jailed.add( inmate.getName() ); + save(); return true; } @@ -86,7 +157,7 @@ Player inmate = sender.getServer().getPlayer( args[1] ); if (inmate == null) { - p.sendMessage("Player not found"); + sender.sendMessage("Player not found"); return true; } @@ -94,6 +165,7 @@ jailed.remove( inmate.getName() ); inmate.sendMessage("You have been released from prison"); inmate.teleport( releaseLocation ); + save(); return true; } @@ -110,11 +182,29 @@ } return true; } + + if (args[0].equals("setjail")) { + Player p = (Player) sender; + jailLocation = p.getLocation().clone(); + p.sendMessage("New jail location has been set"); + save(); + return true; + } + if (args[0].equals("setrelease")) { + Player p = (Player) sender; + releaseLocation = p.getLocation().clone(); + p.sendMessage("New release location has been set"); + save(); + return true; + } + if (args[0].equals("tpjail")) { + Player p = (Player) sender; p.teleport(jailLocation); return true; } if (args[0].equals("tprelease")) { + Player p = (Player) sender; p.teleport(releaseLocation); return true; } @@ -144,6 +234,7 @@ Player p = event.getPlayer(); if (jailed.contains( p.getName() ) ) { p.teleport( jailLocation ); + p.sendMessage("You are currently in jail and have been teleported to you cell"); } } @@ -195,5 +286,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 ); + } + } }