From 1b4df1d4d78eed97a7045d0e9d1425764942c821 Mon Sep 17 00:00:00 2001 From: Piotr Zych <77621271+P1otrulla@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:32:59 +0100 Subject: [PATCH] GH-587 Implement teleport to spawn on first join (#588) --- .../implementation/PluginConfiguration.java | 11 ++++ .../firstjoin/FirstJoinController.java | 60 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 eternalcore-core/src/main/java/com/eternalcode/core/feature/firstjoin/FirstJoinController.java diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java b/eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java index f18daf453..f22b3fac1 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java @@ -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(); diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/firstjoin/FirstJoinController.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/firstjoin/FirstJoinController.java new file mode 100644 index 000000000..416a40a52 --- /dev/null +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/firstjoin/FirstJoinController.java @@ -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); + } +}