Skip to content

Commit

Permalink
GH-541 Add teleport to random player features (tprp command). (#546)
Browse files Browse the repository at this point in the history
* Add teleport to random player feature

* Remove Player#getLocation from teleport method and add missing translations in PlTranslation.

* Update TeleportToRandomPlayerCommand.java

* Follow @imDMK sugggestions.

* Random class was replaced with a RandomUtil function for randomly selecting a player.

* Rename 'NoPlayerToRandomTeleport' notice to 'RandomPlayerNotFound'
  • Loading branch information
vLuckyyy authored Oct 7, 2023
1 parent f9a871b commit 7bb83fe
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 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.stream.Collectors;

@Route(name = "teleportorandomplayer", aliases = { "tprp" })
@Permission("eternalcore.tprp")
public class TeleportToRandomPlayerCommand {

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 a random player on the server, with the option to filter op players")
void execute(Player player) {
List<Player> possibleTargetPlayers = this.server.getOnlinePlayers().stream()
.filter(target -> this.pluginConfiguration.teleport.includeOpPlayersInRandomTeleport || !target.isOp())
.collect(Collectors.toList());

Option<Player> randomPlayerOption = RandomUtil.randomElement(possibleTargetPlayers);

if (randomPlayerOption.isEmpty()) {
this.noticeService.create()
.player(player.getUniqueId())
.notice(translation -> translation.teleport().randomPlayerNotFound())
.send();
return;
}

Player randomPlayer = randomPlayerOption.get();

player.teleport(randomPlayer);
this.noticeService.create()
.player(player.getUniqueId())
.notice(translation -> translation.teleport().teleportedToRandomPlayer())
.placeholder("{PLAYER}", randomPlayer.getName())
.send();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ interface TeleportSection {
Notice teleportedToLastLocation();
Notice teleportedSpecifiedPlayerLastLocation();
Notice lastLocationNoExist();

// teleport to random player command
Notice randomPlayerNotFound();
Notice teleportedToRandomPlayer();
}

// Random Teleport Section
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ public static class ENTeleportSection implements TeleportSection {
public Notice teleportedSpecifiedPlayerLastLocation = Notice.chat("<green>► <white>Teleported <green>{PLAYER} <white>to the last location!");
@Description(" ")
public Notice lastLocationNoExist = Notice.chat("<red>✘ <dark_red>Last location is not exist!");

@Description(" ")
public Notice randomPlayerNotFound = Notice.chat("<red>✘ <dark_red>No player found to teleport!");
@Description({ " ", "# {PLAYER} - The player you were teleported" })
public Notice teleportedToRandomPlayer = Notice.chat("<green>► <white>Teleported to random player <green>{PLAYER}<white>!");
}

@Description({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ public static class PLTeleportSection implements TeleportSection {
public Notice teleportedSpecifiedPlayerLastLocation = Notice.chat("<green>► <white>Przeteleportowano gracza <green>{PLAYER} <white>do ostatniej lokalizacji!");
@Description(" ")
public Notice lastLocationNoExist = Notice.chat("<red>✘ <dark_red>Nie ma zapisanej ostatniej lokalizacji!");

@Description(" ")
public Notice randomPlayerNotFound = Notice.chat("<red>✘ <dark_red>Nie można odnaleźć gracza do teleportacji!");
@Description({ " ", "# {PLAYER} - Gracz do którego cię teleportowano" })
public Notice teleportedToRandomPlayer = Notice.chat("<green>► <white>Zostałeś losowo teleportowany do <green>{PLAYER}<white>!");
}

@Description({
Expand Down

0 comments on commit 7bb83fe

Please sign in to comment.