--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/dk/thoerup/bukkit/hoeruputils/HomeCommand.java 2011/08/24 18:45:40 1592 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/HomeCommand.java 2012/09/23 18:56:24 1851 @@ -1,6 +1,7 @@ package dk.thoerup.bukkit.hoeruputils; import java.io.File; +import java.io.IOException; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -10,21 +11,32 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -import org.bukkit.util.config.Configuration; +import org.bukkit.configuration.Configuration; +import org.bukkit.configuration.file.YamlConfiguration; public class HomeCommand implements CommandExecutor { Plugin plugin; - Configuration config; + YamlConfiguration config; + + File file; public HomeCommand(Plugin plugin) { this.plugin = plugin; - File file = new File( plugin.getDataFolder(), "homes.yml"); + file = new File( plugin.getDataFolder(), "homes.yml"); - config = new Configuration( file ); - config.load(); + config = new YamlConfiguration(); + try { + config.load(file); + } catch (Exception e) { + System.out.println("[HoerupUtils] exception loading config file: " + e.getMessage() ); + } + } + + Configuration getConfig() { + return config; } @@ -34,44 +46,73 @@ if (!(sender instanceof Player)) { return false; } - Player p = (Player) sender; + final Player p = (Player) sender; String name = p.getName(); if ( command.getName().equals("home") ) { - config.load(); - - String worldName = config.getString( name + ".world"); - if (worldName != null) { - double yaw = config.getDouble( name + ".yaw", 0.0); - double pitch = config.getDouble( name + ".pitch", 0.0); - double x = config.getDouble( name + ".x", 0.0); - double y = config.getDouble( name + ".y", 0.0); - double z = config.getDouble( name + ".z", 0.0); - - World world = p.getServer().getWorld(worldName); - - Location loc = new Location( world, x, y, z, (float)yaw, (float)pitch); - - p.teleport(loc); + tpUser(p, name); + } + if ( command.getName().equals("ophome") ) { + if (args.length != 1) { + p.sendMessage("Usage: /ophome "); + return true; + } + if ( p.isOp() ) { + tpUser(p, args[0]); } else { - p.sendMessage(ChatColor.YELLOW + "You haven't set a home yet!"); + p.sendMessage("Only operators may use this command"); } } if (command.getName().equals("sethome") ) { Location loc = p.getLocation(); - config.setProperty( name + ".yaw", loc.getYaw() ); - config.setProperty( name + ".pitch", loc.getPitch() ); - config.setProperty( name + ".world", loc.getWorld().getName() ); - config.setProperty( name + ".x", loc.getX() ); - config.setProperty( name + ".y", loc.getY() ); - config.setProperty( name + ".z", loc.getZ() ); - - config.save(); - p.sendMessage(ChatColor.YELLOW + "home is set"); + config.set( name + ".yaw", loc.getYaw() ); + config.set( name + ".pitch", loc.getPitch() ); + config.set( name + ".world", loc.getWorld().getName() ); + config.set( name + ".x", loc.getX() ); + config.set( name + ".y", loc.getY() ); + config.set( name + ".z", loc.getZ() ); + + try { + config.save(file); + p.sendMessage(ChatColor.YELLOW + "home is set"); + } catch (IOException e) { + System.out.println("[HoerupUtils] : /sethome save threw an IO exception: " + e.getMessage() ); + } } return true; } + + private void tpUser(final Player p, String name) { + String worldName = config.getString( name + ".world"); + if (worldName != null) { + double yaw = config.getDouble( name + ".yaw", 0.0); + double pitch = config.getDouble( name + ".pitch", 0.0); + double x = config.getDouble( name + ".x", 0.0); + double y = config.getDouble( name + ".y", 0.0); + double z = config.getDouble( name + ".z", 0.0); + + World world = p.getServer().getWorld(worldName); + int blockX = (int) Math.floor(x); + int blockZ = (int) Math.floor(z); + world.loadChunk(blockX, blockZ); + + final Location loc = new Location( world, x, y, z, (float)yaw, (float)pitch); + + Runnable r = new Runnable() { + public void run() { + p.teleport(loc); + } + }; + int tickCount = 2; // 2 ticks = 100ms + + p.getServer().getScheduler().scheduleSyncDelayedTask(plugin, r, tickCount); + + } else { + p.sendMessage(ChatColor.YELLOW + "You haven't set a home yet!"); + } + + } }