Skip to content

Commit

Permalink
Move spawn control settings under shared node, add config to force sp…
Browse files Browse the repository at this point in the history
…awn on world change.
  • Loading branch information
creatorfromhell committed Nov 29, 2023
1 parent d7d0e20 commit b584b40
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ 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)) {
if(PhantomWorlds.instance().settings.getConfig().getBoolean("spawning.change", false) && 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/

import me.lokka30.phantomworlds.PhantomWorlds;
import me.lokka30.phantomworlds.misc.Utils;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
Expand All @@ -38,12 +40,13 @@ public PlayerDeathListener(PhantomWorlds plugin) {

@EventHandler
public void onDeath(PlayerDeathEvent event) {
if(!PhantomWorlds.instance().settings.getConfig().getBoolean("respawn-world", false)) {
if(!PhantomWorlds.instance().settings.getConfig().getBoolean("spawning.respawn-world", false)) {
return;
}

if(event.getEntity().getBedSpawnLocation() == null) {
event.getEntity().teleport(event.getEntity().getWorld().getSpawnLocation());

event.getEntity().teleport(Utils.spawn(event.getEntity().getWorld()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import me.lokka30.phantomworlds.PhantomWorlds;
import me.lokka30.phantomworlds.misc.Utils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
Expand All @@ -41,31 +42,16 @@ public PlayerJoinListener(PhantomWorlds plugin) {

@EventHandler
public void onJoin(PlayerJoinEvent event) {
final String spawnWorld = PhantomWorlds.instance().settings.getConfig().getString("spawn-world", "world");
final String spawnWorld = PhantomWorlds.instance().settings.getConfig().getString("spawning.default-world", "world");
final World sWorld = Bukkit.getWorld(spawnWorld);
if(sWorld == null) {
plugin.getLogger().warning("Configured spawn world doesn't exist! Not changing player spawn location.");
return;
}

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());
}
//We don't manage so send the player to the spawn world
if(!event.getPlayer().hasPlayedBefore()) {
event.getPlayer().teleport(Utils.spawn(sWorld));
}
}
}
22 changes: 18 additions & 4 deletions src/main/java/me/lokka30/phantomworlds/misc/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.lokka30.microlib.messaging.MultiMessage;
import me.lokka30.phantomworlds.PhantomWorlds;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -294,7 +295,8 @@ public static void teleportToWorld(@NotNull CommandSender sender, @NotNull Strin
worldName = targetPlayer.getWorld().getName();
}

if(Bukkit.getWorld(worldName) == null) {
final World world = Bukkit.getWorld(worldName);
if(world == null) {
(new MultiMessage(
PhantomWorlds.instance().messages.getConfig()
.getStringList("command.phantomworlds.subcommands.common.invalid-world"),
Expand All @@ -305,9 +307,7 @@ public static void teleportToWorld(@NotNull CommandSender sender, @NotNull Strin
))).send(sender);
return;
}

//noinspection ConstantConditions
targetPlayer.teleport(Bukkit.getWorld(worldName).getSpawnLocation());
targetPlayer.teleport(spawn(world));

(new MultiMessage(
PhantomWorlds.instance().messages.getConfig()
Expand All @@ -320,4 +320,18 @@ public static void teleportToWorld(@NotNull CommandSender sender, @NotNull Strin
new MultiMessage.Placeholder("world", worldName, false)
))).send(sender);
}

public static Location spawn(final World world) {
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());

return new Location(world, x, y, z, yaw, pitch);
}
return world.getSpawnLocation();
}
}
16 changes: 11 additions & 5 deletions src/main/resources/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
# Should PW run an update check from the Spigot page on startup?
run-update-checker: true

#What is the spawn world for the server? If the player hasn't played before this is the world that
#they will go to.
spawn-world: world
#Configurations relating to spawn controls.
spawning:

#Should players respawn at the world spawn if they don't have a bed?
respawn-world: true
#What is the spawn world for the server? If the player hasn't played before this is the world that
#they will go to.
default-world: world

#Should players respawn at the world spawn if they don't have a bed?
respawn-world: true

#Should players be sent to the world spawn each time they change worlds?
change: false

#Should worlds deleted be moved to an archive folder in deletedworlds?
delete-archive: true
Expand Down

0 comments on commit b584b40

Please sign in to comment.