Skip to content

Commit

Permalink
feat: tp player on join
Browse files Browse the repository at this point in the history
Plugin can now be configured to tp players to spawn when they join the server
  • Loading branch information
Maloschnikow committed Oct 15, 2024
1 parent 6b6c8c0 commit 0761773
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void onEnable() {
saveDefaultConfig();

SpawnCommand spawnCommand = new SpawnCommand(this);
getServer().getPluginManager().registerEvents(new TeleportToSpawnListener(spawnCommand), this);
getServer().getPluginManager().registerEvents(new TeleportToSpawnListener(this, spawnCommand), this);

LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ public SpawnCommand(Plugin plugin) {

}

public Location getSpawnLocation(Plugin plugin, World spawnWorld) {
// Determine spawn location
Location vanillaSpawnLocation = spawnWorld.getSpawnLocation();
// Read spawn location from config
// If a value is not defined in config, use the default world spawn value
double x = plugin.getConfig().getDouble("spawn-coordinates.x", vanillaSpawnLocation.getX());
double y = plugin.getConfig().getDouble("spawn-coordinates.y", vanillaSpawnLocation.getY());
double z = plugin.getConfig().getDouble("spawn-coordinates.z", vanillaSpawnLocation.getZ());
float yaw = (float) plugin.getConfig().getDouble("spawn-coordinates.yaw", (double) vanillaSpawnLocation.getYaw());
float pitch = (float) plugin.getConfig().getDouble("spawn-coordinates.pitch", (double) vanillaSpawnLocation.getPitch());

return new Location(spawnWorld, x, y, z, yaw, pitch);
}

public Dictionary<UUID, BukkitRunnable> getPlayerIssuedTeleports() {
return playerIssuedTeleports;
}
Expand Down Expand Up @@ -115,16 +129,7 @@ public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxE

// Determine spawn location
World spawnWorld = player.getServer().getWorld("world");
Location vanillaSpawnLocation = spawnWorld.getSpawnLocation();
// Read spawn location from config
// If a value is not defined in config, use the default world spawn value
double x = plugin.getConfig().getDouble("spawn-coordinates.x", vanillaSpawnLocation.getX());
double y = plugin.getConfig().getDouble("spawn-coordinates.y", vanillaSpawnLocation.getY());
double z = plugin.getConfig().getDouble("spawn-coordinates.z", vanillaSpawnLocation.getZ());
float yaw = (float) plugin.getConfig().getDouble("spawn-coordinates.yaw", (double) vanillaSpawnLocation.getYaw());
float pitch = (float) plugin.getConfig().getDouble("spawn-coordinates.pitch", (double) vanillaSpawnLocation.getPitch());

Location spawnLocation = new Location(spawnWorld, x, y, z, yaw, pitch);
Location spawnLocation = getSpawnLocation(this.plugin, spawnWorld);

// Decides on randomness if player is teleported
Random rand = new Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,38 @@

import java.util.UUID;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;

public class TeleportToSpawnListener implements Listener {

private final SpawnCommand spawnCommand;
private final Plugin plugin;

// Vars for configuration
private final boolean TP_ON_PLAYER_JOIN;

public TeleportToSpawnListener(SpawnCommand spawnCommand) {
public TeleportToSpawnListener(Plugin plugin, SpawnCommand spawnCommand) {
this.spawnCommand = spawnCommand;
this.plugin = plugin;

// Load configuration
this.TP_ON_PLAYER_JOIN = plugin.getConfig().getBoolean("tp-on-player-join", false);
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
if (!this.TP_ON_PLAYER_JOIN) { return; }
World spawnWorld = event.getPlayer().getServer().getWorld("world");
Location spawnLocation = spawnCommand.getSpawnLocation(this.plugin, spawnWorld);
event.getPlayer().teleportAsync(spawnLocation);
}

@EventHandler
Expand Down
4 changes: 4 additions & 0 deletions spawncmdplugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ spawn-coordinates:
# defaults to false
keep-rotation: false

# If this is set to true a player will be teleported to spawn right after they join
# defaults to false
tp-on-player-join: false

# Times
#
# Cooldown in seconds: How long must the player wait before being able to run the /spawn command again
Expand Down

0 comments on commit 0761773

Please sign in to comment.