diff --git a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/PWCommand.java b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/PWCommand.java index 64f065d..d2dd90f 100644 --- a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/PWCommand.java +++ b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/PWCommand.java @@ -24,6 +24,7 @@ import me.lokka30.phantomworlds.commands.phantomworlds.parameters.sub.InfoCommand; import me.lokka30.phantomworlds.commands.phantomworlds.parameters.sub.ListCommand; import me.lokka30.phantomworlds.commands.phantomworlds.parameters.sub.ReloadCommand; +import me.lokka30.phantomworlds.commands.phantomworlds.parameters.sub.SetSpawnCommand; import me.lokka30.phantomworlds.commands.phantomworlds.parameters.sub.SpawnCommand; import me.lokka30.phantomworlds.commands.phantomworlds.parameters.sub.TeleportCommand; import me.lokka30.phantomworlds.commands.phantomworlds.parameters.sub.UnloadCommand; @@ -45,6 +46,12 @@ @Command({"pw", "phantomworlds"}) public class PWCommand { + @Subcommand({"backup", "archive", "bu"}) + @CommandPermission("phantomworlds.command.phantomworlds.backup") + public void backup(BukkitCommandActor actor, final World world) { + BackupCommand.onCommand(actor, world); + } + @Subcommand({"create", "+", "new"}) @CommandPermission("phantomworlds.command.phantomworlds.create") public void create(BukkitCommandActor actor) { @@ -57,18 +64,18 @@ public void compatibility(BukkitCommandActor actor) { CompatibilityCommand.onCommand(actor); } + @Subcommand({"debug"}) + @CommandPermission("phantomworlds.command.phantomworlds.debug") + public void debug(BukkitCommandActor actor, final String level) { + DebugCommand.onCommand(actor, level); + } + @Subcommand({"delete", "-", "remove", "del"}) @CommandPermission("phantomworlds.command.phantomworlds.delete") public void delete(BukkitCommandActor actor, final World world) { DeleteCommand.onCommand(actor, world); } - @Subcommand({"backup", "archive", "bu"}) - @CommandPermission("phantomworlds.command.phantomworlds.backup") - public void backup(BukkitCommandActor actor, final World world) { - BackupCommand.onCommand(actor, world); - } - @Subcommand({"create", "+", "new"}) @DefaultFor({"pw", "phantomworlds"}) @CommandPermission("phantomworlds.command.phantomworlds.list") @@ -76,12 +83,6 @@ public void list(BukkitCommandActor actor) { ListCommand.onCommand(actor); } - @Subcommand({"debug"}) - @CommandPermission("phantomworlds.command.phantomworlds.debug") - public void debug(BukkitCommandActor actor, final String level) { - DebugCommand.onCommand(actor, level); - } - @Subcommand({"gamerule", "rule"}) @CommandPermission("phantomworlds.command.phantomworlds.gamerule") public void gamerule(BukkitCommandActor actor) { @@ -102,14 +103,8 @@ public void reload(BukkitCommandActor actor) { @Subcommand({"setspawn"}) @CommandPermission("phantomworlds.command.phantomworlds.setspawn") - public void setspawn(BukkitCommandActor actor) { - //todo: port setspawn command - } - - @Subcommand({"teleport", "tp"}) - @CommandPermission("phantomworlds.command.phantomworlds.teleport") - public void tp(BukkitCommandActor actor, final World world, @Optional final Player player) { - TeleportCommand.onCommand(actor, world, player); + public void setspawn(BukkitCommandActor actor, @Optional Double x, @Optional Double y, @Optional Double z, @Optional World world, @Optional Float yaw, @Optional Float pitch) { + SetSpawnCommand.onCommand(actor, x, y, z, world, yaw, pitch); } @Subcommand({"spawn"}) @@ -118,6 +113,12 @@ public void spawn(BukkitCommandActor actor, final World world, @Optional final P SpawnCommand.onCommand(actor, world, player); } + @Subcommand({"teleport", "tp"}) + @CommandPermission("phantomworlds.command.phantomworlds.teleport") + public void tp(BukkitCommandActor actor, final World world, @Optional final Player player) { + TeleportCommand.onCommand(actor, world, player); + } + @Subcommand({"unload", "u"}) @CommandPermission("phantomworlds.command.phantomworlds.unload") public void unload(BukkitCommandActor actor, final World world) { diff --git a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/sub/ListCommand.java b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/sub/ListCommand.java index c479fb2..40aecd1 100644 --- a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/sub/ListCommand.java +++ b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/sub/ListCommand.java @@ -33,7 +33,7 @@ */ public class ListCommand { - public static void onCommand(final BukkitCommandActor actor) { + public static void onCommand(final BukkitCommandActor actor, Integer... test) { (new MultiMessage( PhantomWorlds.instance().messages.getConfig() .getStringList("command.phantomworlds.subcommands.list.header"), Arrays.asList( @@ -44,7 +44,7 @@ public static void onCommand(final BukkitCommandActor actor) { ))).send(actor.getSender()); //TODO: Unloaded, archived, last backup times. - for(World world : Bukkit.getWorlds()) { + for(final World world : Bukkit.getWorlds()) { (new MultiMessage( PhantomWorlds.instance().messages.getConfig() .getStringList("command.phantomworlds.subcommands.list.entry"), Arrays.asList( diff --git a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/sub/SetSpawnCommand.java b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/sub/SetSpawnCommand.java index c62bcab..909a5dd 100644 --- a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/sub/SetSpawnCommand.java +++ b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/sub/SetSpawnCommand.java @@ -17,8 +17,16 @@ * along with this program. If not, see . */ +import me.lokka30.microlib.messaging.MultiMessage; +import me.lokka30.phantomworlds.PhantomWorlds; +import me.lokka30.phantomworlds.misc.Utils; +import org.bukkit.Location; +import org.bukkit.World; import revxrsal.commands.bukkit.BukkitCommandActor; +import java.io.IOException; +import java.util.Arrays; + /** * SetSpawnCommand * @@ -27,7 +35,75 @@ */ public class SetSpawnCommand { - public static void onCommand(final BukkitCommandActor actor) { + public static void onCommand(final BukkitCommandActor actor, Double x, Double y, Double z, World world, Float yaw, Float pitch) { + + if(actor.isConsole() || actor.getAsPlayer() == null) { + if(x == null || y == null || z == null || world == null) { + (new MultiMessage( + PhantomWorlds.instance().messages.getConfig() + .getStringList("command.phantomworlds.subcommands.setspawn.usage-console"), + Arrays.asList( + new MultiMessage.Placeholder("prefix", PhantomWorlds.instance().messages.getConfig() + .getString("common.prefix", "&b&lPhantomWorlds: &7"), true), + new MultiMessage.Placeholder("label", "setspawn", false) + ))).send(actor.getSender()); + return; + } + } + + final World finalWorld = (world == null)? actor.getAsPlayer().getWorld() : world; + final double finalX = (x == null)? actor.getAsPlayer().getLocation().getX() : x; + final double finalY = (y == null)? actor.getAsPlayer().getLocation().getY() : y; + final double finalZ = (z == null)? actor.getAsPlayer().getLocation().getZ() : z; + float finalYaw = (yaw == null)? 0 : yaw; + float finalPitch = (pitch == null)? 0 : pitch; + + if(yaw == null && actor.getAsPlayer() != null) { + finalYaw = actor.getAsPlayer().getLocation().getYaw(); + } + + if(pitch == null && actor.getAsPlayer() != null) { + finalPitch = actor.getAsPlayer().getLocation().getPitch(); + } + + final String cfgPath = "worlds-to-load." + finalWorld.getName(); + if(PhantomWorlds.instance().data.getConfig().contains(cfgPath)) { + //PhantomWorlds manages this world so let's set the spawn here for better accuracy. + PhantomWorlds.instance().data.getConfig().set(cfgPath + ".spawn.x", finalX); + PhantomWorlds.instance().data.getConfig().set(cfgPath + ".spawn.y", finalX); + PhantomWorlds.instance().data.getConfig().set(cfgPath + ".spawn.z", finalX); + PhantomWorlds.instance().data.getConfig().set(cfgPath + ".spawn.yaw", finalX); + PhantomWorlds.instance().data.getConfig().set(cfgPath + ".spawn.pitch", finalX); + + try { + PhantomWorlds.instance().data.save(); + } catch(final IOException ex) { + throw new RuntimeException(ex); + } + + } else { + //PhantomWorlds doesn't manage the spawn here so let Mojang deal with it. + try { + finalWorld.setSpawnLocation(new Location(finalWorld, finalX, finalY, finalZ, finalYaw, finalPitch)); + } catch(NoSuchMethodError err) { + //This is dumb that the setSpawn method in spigot uses integers... great design. + finalWorld.setSpawnLocation((int)finalX, (int)finalY, (int)finalZ); + // 1.8 doesn't let us set pitch and yaw ... yawn + } + } + (new MultiMessage( + PhantomWorlds.instance().messages.getConfig() + .getStringList("command.phantomworlds.subcommands.setspawn.success"), Arrays.asList( + new MultiMessage.Placeholder("prefix", + PhantomWorlds.instance().messages.getConfig().getString("common.prefix", "&b&lPhantomWorlds: &7"), + true), + new MultiMessage.Placeholder("world", finalWorld.getName(), false), + new MultiMessage.Placeholder("x", String.valueOf(Utils.roundTwoDecimalPlaces(finalX)), false), + new MultiMessage.Placeholder("y", String.valueOf(Utils.roundTwoDecimalPlaces(finalY)), false), + new MultiMessage.Placeholder("z", String.valueOf(Utils.roundTwoDecimalPlaces(finalZ)), false), + new MultiMessage.Placeholder("yaw", String.valueOf(Utils.roundTwoDecimalPlaces(finalYaw)), false), + new MultiMessage.Placeholder("pitch", String.valueOf(Utils.roundTwoDecimalPlaces(finalPitch)), false) + ))).send(actor.getSender()); } } \ No newline at end of file diff --git a/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerChangeWorldListener.java b/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerChangeWorldListener.java index 12d3e32..9ae302a 100644 --- a/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerChangeWorldListener.java +++ b/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerChangeWorldListener.java @@ -19,6 +19,7 @@ import me.lokka30.phantomworlds.PhantomWorlds; import org.bukkit.GameMode; +import org.bukkit.Location; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerChangedWorldEvent; @@ -41,6 +42,19 @@ public PlayerChangeWorldListener(PhantomWorlds plugin) { @EventHandler public void onChangeWorld(PlayerChangedWorldEvent event) { + + //Check if this world has a PhantomWorlds managed spawn. If so, teleport the player there. + final String spawnPath = "worlds-to-load." + event.getPlayer().getWorld().getName() + ".spawn"; + if(PhantomWorlds.instance().data.getConfig().contains(spawnPath)) { + final double x = PhantomWorlds.instance().data.getConfig().getDouble(spawnPath + ".x", event.getPlayer().getWorld().getSpawnLocation().getX()); + final double y = PhantomWorlds.instance().data.getConfig().getDouble(spawnPath + ".y", event.getPlayer().getWorld().getSpawnLocation().getY()); + final double z = PhantomWorlds.instance().data.getConfig().getDouble(spawnPath + ".z", event.getPlayer().getWorld().getSpawnLocation().getZ()); + final float yaw = (float)PhantomWorlds.instance().data.getConfig().getDouble(spawnPath + ".yaw", event.getPlayer().getWorld().getSpawnLocation().getYaw()); + final float pitch = (float)PhantomWorlds.instance().data.getConfig().getDouble(spawnPath + ".pitch", event.getPlayer().getWorld().getSpawnLocation().getPitch()); + + event.getPlayer().teleport(new Location(event.getPlayer().getWorld(), x, y, z, yaw, pitch)); + } + final String cfgPath = "worlds-to-load." + event.getPlayer().getWorld().getName(); if(PhantomWorlds.instance().data.getConfig().contains(cfgPath + ".gameMode")) { final GameMode mode = GameMode.valueOf(PhantomWorlds.instance().data.getConfig().getString(cfgPath + ".gameMode")); diff --git a/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerJoinListener.java b/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerJoinListener.java index 020ef9e..809e5bf 100644 --- a/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerJoinListener.java +++ b/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerJoinListener.java @@ -19,6 +19,8 @@ import me.lokka30.phantomworlds.PhantomWorlds; import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; @@ -40,13 +42,30 @@ public PlayerJoinListener(PhantomWorlds plugin) { @EventHandler public void onJoin(PlayerJoinEvent event) { final String spawnWorld = PhantomWorlds.instance().settings.getConfig().getString("spawn-world", "world"); - if(Bukkit.getWorld(spawnWorld) == null) { + final World sWorld = Bukkit.getWorld(spawnWorld); + if(sWorld == null) { plugin.getLogger().warning("Configured spawn world doesn't exist! Not changing player spawn location."); return; } - if(!event.getPlayer().hasPlayedBefore()) { - event.getPlayer().teleport(Bukkit.getWorld(spawnWorld).getSpawnLocation()); + final World world = (event.getPlayer().hasPlayedBefore())? event.getPlayer().getWorld() : sWorld; + + //Check if we manage the spawn for the world the player needs to join in. + final String cfgPath = "worlds-to-load." + world.getName() + ".spawn"; + if(PhantomWorlds.instance().data.getConfig().contains(cfgPath)) { + final double x = PhantomWorlds.instance().data.getConfig().getDouble(cfgPath + ".x", world.getSpawnLocation().getX()); + final double y = PhantomWorlds.instance().data.getConfig().getDouble(cfgPath + ".y", world.getSpawnLocation().getY()); + final double z = PhantomWorlds.instance().data.getConfig().getDouble(cfgPath + ".z", world.getSpawnLocation().getZ()); + final float yaw = (float)PhantomWorlds.instance().data.getConfig().getDouble(cfgPath + ".yaw", world.getSpawnLocation().getYaw()); + final float pitch = (float)PhantomWorlds.instance().data.getConfig().getDouble(cfgPath + ".pitch", world.getSpawnLocation().getPitch()); + + event.getPlayer().teleport(new Location(world, x, y, z, yaw, pitch)); + } else { + + //We don't manage so send the player to the spawn world + if(!event.getPlayer().hasPlayedBefore()) { + event.getPlayer().teleport(sWorld.getSpawnLocation()); + } } } } \ No newline at end of file