Skip to content

Commit

Permalink
GH-587 Implement teleport to spawn on first join (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
P1otrulla authored Dec 12, 2023
1 parent af154d6 commit 1b4df1d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public static class Database {
public int port = 3306;
}

@Description({ "", "# Join settings" })
public Join join = new Join();

@Contextual
public static class Join {

@Description("# Teleport to spawn on first join")
public boolean teleportToSpawnOnFirstJoin = true;

}

@Description({ " ", "# Teleport request section" })
public TeleportAsk teleportAsk = new TeleportAsk();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.eternalcode.core.feature.firstjoin;

import com.eternalcode.annotations.scan.feature.FeatureDocs;
import com.eternalcode.core.configuration.implementation.LocationsConfiguration;
import com.eternalcode.core.configuration.implementation.PluginConfiguration;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Controller;
import com.eternalcode.core.shared.Position;
import com.eternalcode.core.shared.PositionAdapter;
import io.papermc.lib.PaperLib;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@FeatureDocs(
description = "Teleport to spawn on first join",
name = "Player First Join"
)
@Controller
public class FirstJoinController implements Listener {

private static final Logger LOGGER = LoggerFactory.getLogger(FirstJoinController.class);

private final LocationsConfiguration locationsConfiguration;
private final PluginConfiguration pluginConfiguration;

@Inject
public FirstJoinController(LocationsConfiguration locationsConfiguration, PluginConfiguration pluginConfiguration) {
this.locationsConfiguration = locationsConfiguration;
this.pluginConfiguration = pluginConfiguration;
}

@EventHandler
void onFirstJoin(PlayerJoinEvent event) {
if (!this.pluginConfiguration.join.teleportToSpawnOnFirstJoin) {
return;
}

Player player = event.getPlayer();

if (player.hasPlayedBefore()) {
return;
}

Position spawnPosition = this.locationsConfiguration.spawn;

if (spawnPosition == null || spawnPosition.isNoneWorld()) {
LOGGER.warn("In the configuration, the teleportToSpawnOnFirstJoin function is enabled but spawn is not set! Set it using the /setspawn command");

return;
}

Location spawnLocation = PositionAdapter.convert(spawnPosition);
PaperLib.teleportAsync(player, spawnLocation);
}
}

0 comments on commit 1b4df1d

Please sign in to comment.