Skip to content

Commit

Permalink
Transfer setspawn command. Handle spawn TP internally to prevent spig…
Browse files Browse the repository at this point in the history
…ot from rounding to integers.
  • Loading branch information
creatorfromhell committed Nov 26, 2023
1 parent f89809e commit 72840fc
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -57,31 +64,25 @@ 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")
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) {
Expand All @@ -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"})
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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
*
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));

This comment has been minimized.

Copy link
@lokka30

lokka30 Nov 28, 2023

just thought of something whilst checking the implementation - would this also teleport players to spawn each time they change world via a portal or something? or if they are teleporting to a player in a different world, for example?

This comment has been minimized.

Copy link
@creatorfromhell

creatorfromhell Nov 28, 2023

Author Member

just thought of something whilst checking the implementation - would this also teleport players to spawn each time they change world via a portal or something? or if they are teleporting to a player in a different world, for example?

Funny enough I was just thinking that today so I'll have to move this to the death event or something.

}

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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
}
}

0 comments on commit 72840fc

Please sign in to comment.