From 48491c6b608a2c4a3a97530bd18fa290fe40d309 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Sun, 24 Sep 2023 01:44:39 +0200 Subject: [PATCH 1/6] Add teleport to random player feature --- .../implementation/PluginConfiguration.java | 3 + .../TeleportToRandomPlayerCommand.java | 56 +++++++++++++++++++ .../core/translation/Translation.java | 4 ++ .../implementation/ENTranslation.java | 5 ++ 4 files changed, 68 insertions(+) create mode 100644 eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.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 4f7fd03d1..4bb563b77 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 @@ -94,6 +94,9 @@ public static class Teleport implements SpawnSettings { @Description("# Time of teleportation to spawn") public Duration teleportTimeToSpawn = Duration.ofSeconds(5); + @Description("# Include players with op in teleport to random player") + public boolean includeOpPlayersInRandomTeleport = false; + @Override public Duration teleportationTimeToSpawn() { return this.teleportTimeToSpawn; diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java new file mode 100644 index 000000000..1aa628f2a --- /dev/null +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java @@ -0,0 +1,56 @@ +package com.eternalcode.core.feature.teleport.command; + +import com.eternalcode.annotations.scan.command.DescriptionDocs; +import com.eternalcode.core.configuration.implementation.PluginConfiguration; +import com.eternalcode.core.notice.NoticeService; +import dev.rollczi.litecommands.command.execute.Execute; +import dev.rollczi.litecommands.command.permission.Permission; +import dev.rollczi.litecommands.command.route.Route; +import org.bukkit.Server; +import org.bukkit.entity.Player; + +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; + +@Route(name = "teleport-to-random-player", aliases = {"tprp"}) +@Permission("eternalcore.tprp") +public class TeleportToRandomPlayerCommand { + + private static final Random RANDOM = new Random(); + + private final Server server; + private final PluginConfiguration pluginConfiguration; + private final NoticeService noticeService; + + public TeleportToRandomPlayerCommand(Server server, PluginConfiguration pluginConfiguration, NoticeService noticeService) { + this.server = server; + this.pluginConfiguration = pluginConfiguration; + this.noticeService = noticeService; + } + + @Execute + @DescriptionDocs(description = "Teleport to random player on server") + void execute(Player player) { + List applicablePlayers = this.server.getOnlinePlayers().stream() + .filter(players -> this.pluginConfiguration.teleport.includeOpPlayersInRandomTeleport || !players.isOp()) + .collect(Collectors.toList()); + + if (applicablePlayers.isEmpty()) { + this.noticeService.create() + .player(player.getUniqueId()) + .notice(translation -> translation.teleport().noPlayerToRandomTeleportFound()) + .send(); + return; + } + + Player target = applicablePlayers.get(RANDOM.nextInt(applicablePlayers.size())); + + player.teleport(target.getLocation()); + this.noticeService.create() + .player(player.getUniqueId()) + .notice(translation -> translation.teleport().teleportedToRandomPlayer()) + .placeholder("{PLAYER}", target.getName()) + .send(); + } +} diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java index 2ed0dd1d8..ff86161a3 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java @@ -86,6 +86,10 @@ interface TeleportSection { Notice teleportedToLastLocation(); Notice teleportedSpecifiedPlayerLastLocation(); Notice lastLocationNoExist(); + + // teleport to random player command + Notice noPlayerToRandomTeleportFound(); + Notice teleportedToRandomPlayer(); } // Random Teleport Section diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java index b6136d8ff..1f02e00bc 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java @@ -209,6 +209,11 @@ public static class ENTeleportSection implements TeleportSection { public Notice teleportedSpecifiedPlayerLastLocation = Notice.chat("Teleported {PLAYER} to the last location!"); @Description(" ") public Notice lastLocationNoExist = Notice.chat("Last location is not exist!"); + + @Description(" ") + public Notice noPlayerToRandomTeleportFound = Notice.chat("No player found to teleport!"); + @Description({ " ", "# {PLAYER} - Player to whom he was teleported" }) + public Notice teleportedToRandomPlayer = Notice.chat("Teleported to random player {PLAYER}!"); } @Description({ From 6da55ec29515315ed55dfc237be4e43b213cd363 Mon Sep 17 00:00:00 2001 From: vluckyyy Date: Fri, 6 Oct 2023 13:53:52 +0200 Subject: [PATCH 2/6] Remove Player#getLocation from teleport method and add missing translations in PlTranslation. --- .../teleport/command/TeleportToRandomPlayerCommand.java | 4 ++-- .../core/translation/implementation/PLTranslation.java | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java index 1aa628f2a..580fd34a3 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java @@ -13,7 +13,7 @@ import java.util.Random; import java.util.stream.Collectors; -@Route(name = "teleport-to-random-player", aliases = {"tprp"}) +@Route(name = "teleport-to-random-player", aliases = { "tprp" }) @Permission("eternalcore.tprp") public class TeleportToRandomPlayerCommand { @@ -46,7 +46,7 @@ void execute(Player player) { Player target = applicablePlayers.get(RANDOM.nextInt(applicablePlayers.size())); - player.teleport(target.getLocation()); + player.teleport(target); this.noticeService.create() .player(player.getUniqueId()) .notice(translation -> translation.teleport().teleportedToRandomPlayer()) diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java index 81a66d3c5..5a7ae48cf 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java @@ -209,6 +209,11 @@ public static class PLTeleportSection implements TeleportSection { public Notice teleportedSpecifiedPlayerLastLocation = Notice.chat("Przeteleportowano gracza {PLAYER} do ostatniej lokalizacji!"); @Description(" ") public Notice lastLocationNoExist = Notice.chat("Nie ma zapisanej ostatniej lokalizacji!"); + + @Description(" ") + public Notice noPlayerToRandomTeleportFound = Notice.chat("Nie można odnaleźć gracza do teleportacji!"); + @Description({ " ", "# {PLAYER} - Player to whom he was teleported" }) + public Notice teleportedToRandomPlayer = Notice.chat("Zostałeś losowo teleportowany do {PLAYER}!"); } @Description({ From 5336e39f631a9d44813f4831f36e38e9516c22d3 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Fri, 6 Oct 2023 15:41:43 +0200 Subject: [PATCH 3/6] Update TeleportToRandomPlayerCommand.java --- .../feature/teleport/command/TeleportToRandomPlayerCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java index 580fd34a3..0ab90089b 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java @@ -46,7 +46,7 @@ void execute(Player player) { Player target = applicablePlayers.get(RANDOM.nextInt(applicablePlayers.size())); - player.teleport(target); + player.teleport(target.getLocation()); this.noticeService.create() .player(player.getUniqueId()) .notice(translation -> translation.teleport().teleportedToRandomPlayer()) From ac9b6cf53150ca422ad7bfed2b6b9e75da41659c Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Fri, 6 Oct 2023 15:56:47 +0200 Subject: [PATCH 4/6] Follow @imDMK sugggestions. --- .../command/TeleportToRandomPlayerCommand.java | 16 ++++++++-------- .../implementation/ENTranslation.java | 2 +- .../implementation/PLTranslation.java | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java index 0ab90089b..a00d97fd0 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java @@ -13,7 +13,7 @@ import java.util.Random; import java.util.stream.Collectors; -@Route(name = "teleport-to-random-player", aliases = { "tprp" }) +@Route(name = "teleport-to-random-player", aliases = {"tprp"}) @Permission("eternalcore.tprp") public class TeleportToRandomPlayerCommand { @@ -30,13 +30,13 @@ public TeleportToRandomPlayerCommand(Server server, PluginConfiguration pluginCo } @Execute - @DescriptionDocs(description = "Teleport to random player on server") + @DescriptionDocs(description = "Teleport to random player on server, with filter op players option") void execute(Player player) { - List applicablePlayers = this.server.getOnlinePlayers().stream() - .filter(players -> this.pluginConfiguration.teleport.includeOpPlayersInRandomTeleport || !players.isOp()) + List possibleTargetPlayers = this.server.getOnlinePlayers().stream() + .filter(target -> this.pluginConfiguration.teleport.includeOpPlayersInRandomTeleport || !target.isOp()) .collect(Collectors.toList()); - if (applicablePlayers.isEmpty()) { + if (possibleTargetPlayers.isEmpty()) { this.noticeService.create() .player(player.getUniqueId()) .notice(translation -> translation.teleport().noPlayerToRandomTeleportFound()) @@ -44,13 +44,13 @@ void execute(Player player) { return; } - Player target = applicablePlayers.get(RANDOM.nextInt(applicablePlayers.size())); + Player randomPlayer = possibleTargetPlayers.get(RANDOM.nextInt(possibleTargetPlayers.size())); - player.teleport(target.getLocation()); + player.teleport(randomPlayer); this.noticeService.create() .player(player.getUniqueId()) .notice(translation -> translation.teleport().teleportedToRandomPlayer()) - .placeholder("{PLAYER}", target.getName()) + .placeholder("{PLAYER}", randomPlayer.getName()) .send(); } } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java index 1f02e00bc..6957ab647 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java @@ -212,7 +212,7 @@ public static class ENTeleportSection implements TeleportSection { @Description(" ") public Notice noPlayerToRandomTeleportFound = Notice.chat("No player found to teleport!"); - @Description({ " ", "# {PLAYER} - Player to whom he was teleported" }) + @Description({ " ", "# {PLAYER} - The player you were teleported" }) public Notice teleportedToRandomPlayer = Notice.chat("Teleported to random player {PLAYER}!"); } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java index 5a7ae48cf..fdda8c4cc 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java @@ -212,7 +212,7 @@ public static class PLTeleportSection implements TeleportSection { @Description(" ") public Notice noPlayerToRandomTeleportFound = Notice.chat("Nie można odnaleźć gracza do teleportacji!"); - @Description({ " ", "# {PLAYER} - Player to whom he was teleported" }) + @Description({ " ", "# {PLAYER} - Gracz do którego cię teleportowano" }) public Notice teleportedToRandomPlayer = Notice.chat("Zostałeś losowo teleportowany do {PLAYER}!"); } From cd90b24b1170ddb1732ef51808cc8a9d647aff01 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Sat, 7 Oct 2023 18:57:34 +0200 Subject: [PATCH 5/6] Random class was replaced with a RandomUtil function for randomly selecting a player. --- .../command/TeleportToRandomPlayerCommand.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java index a00d97fd0..045888a24 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java @@ -3,22 +3,21 @@ import com.eternalcode.annotations.scan.command.DescriptionDocs; import com.eternalcode.core.configuration.implementation.PluginConfiguration; import com.eternalcode.core.notice.NoticeService; +import com.eternalcode.core.util.RandomUtil; import dev.rollczi.litecommands.command.execute.Execute; import dev.rollczi.litecommands.command.permission.Permission; import dev.rollczi.litecommands.command.route.Route; import org.bukkit.Server; import org.bukkit.entity.Player; +import panda.std.Option; import java.util.List; -import java.util.Random; import java.util.stream.Collectors; -@Route(name = "teleport-to-random-player", aliases = {"tprp"}) +@Route(name = "teleportorandomplayer", aliases = { "tprp" }) @Permission("eternalcore.tprp") public class TeleportToRandomPlayerCommand { - private static final Random RANDOM = new Random(); - private final Server server; private final PluginConfiguration pluginConfiguration; private final NoticeService noticeService; @@ -29,14 +28,17 @@ public TeleportToRandomPlayerCommand(Server server, PluginConfiguration pluginCo this.noticeService = noticeService; } + @Execute - @DescriptionDocs(description = "Teleport to random player on server, with filter op players option") + @DescriptionDocs(description = "Teleport to a random player on the server, with the option to filter op players") void execute(Player player) { List possibleTargetPlayers = this.server.getOnlinePlayers().stream() .filter(target -> this.pluginConfiguration.teleport.includeOpPlayersInRandomTeleport || !target.isOp()) .collect(Collectors.toList()); - if (possibleTargetPlayers.isEmpty()) { + Option randomPlayerOption = RandomUtil.randomElement(possibleTargetPlayers); + + if (randomPlayerOption.isEmpty()) { this.noticeService.create() .player(player.getUniqueId()) .notice(translation -> translation.teleport().noPlayerToRandomTeleportFound()) @@ -44,7 +46,7 @@ void execute(Player player) { return; } - Player randomPlayer = possibleTargetPlayers.get(RANDOM.nextInt(possibleTargetPlayers.size())); + Player randomPlayer = randomPlayerOption.get(); player.teleport(randomPlayer); this.noticeService.create() From dd0a08b1dd609219d352a9ad7902f001deb9fe88 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Sat, 7 Oct 2023 19:00:57 +0200 Subject: [PATCH 6/6] Rename 'NoPlayerToRandomTeleport' notice to 'RandomPlayerNotFound' --- .../feature/teleport/command/TeleportToRandomPlayerCommand.java | 2 +- .../main/java/com/eternalcode/core/translation/Translation.java | 2 +- .../core/translation/implementation/ENTranslation.java | 2 +- .../core/translation/implementation/PLTranslation.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java index 045888a24..5101afe12 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/command/TeleportToRandomPlayerCommand.java @@ -41,7 +41,7 @@ void execute(Player player) { if (randomPlayerOption.isEmpty()) { this.noticeService.create() .player(player.getUniqueId()) - .notice(translation -> translation.teleport().noPlayerToRandomTeleportFound()) + .notice(translation -> translation.teleport().randomPlayerNotFound()) .send(); return; } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java index ff86161a3..1344e10ed 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java @@ -88,7 +88,7 @@ interface TeleportSection { Notice lastLocationNoExist(); // teleport to random player command - Notice noPlayerToRandomTeleportFound(); + Notice randomPlayerNotFound(); Notice teleportedToRandomPlayer(); } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java index 6957ab647..e7790e38e 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java @@ -211,7 +211,7 @@ public static class ENTeleportSection implements TeleportSection { public Notice lastLocationNoExist = Notice.chat("Last location is not exist!"); @Description(" ") - public Notice noPlayerToRandomTeleportFound = Notice.chat("No player found to teleport!"); + public Notice randomPlayerNotFound = Notice.chat("No player found to teleport!"); @Description({ " ", "# {PLAYER} - The player you were teleported" }) public Notice teleportedToRandomPlayer = Notice.chat("Teleported to random player {PLAYER}!"); } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java index fdda8c4cc..686796b35 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java @@ -211,7 +211,7 @@ public static class PLTeleportSection implements TeleportSection { public Notice lastLocationNoExist = Notice.chat("Nie ma zapisanej ostatniej lokalizacji!"); @Description(" ") - public Notice noPlayerToRandomTeleportFound = Notice.chat("Nie można odnaleźć gracza do teleportacji!"); + public Notice randomPlayerNotFound = Notice.chat("Nie można odnaleźć gracza do teleportacji!"); @Description({ " ", "# {PLAYER} - Gracz do którego cię teleportowano" }) public Notice teleportedToRandomPlayer = Notice.chat("Zostałeś losowo teleportowany do {PLAYER}!"); }