Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-541 Add teleport to random player features (tprp command). #546

Merged
merged 7 commits into from
Oct 7, 2023
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,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<Player> applicablePlayers = this.server.getOnlinePlayers().stream()
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
.filter(players -> this.pluginConfiguration.teleport.includeOpPlayersInRandomTeleport || !players.isOp())
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toList());
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved

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);
this.noticeService.create()
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
.player(player.getUniqueId())
.notice(translation -> translation.teleport().teleportedToRandomPlayer())
.placeholder("{PLAYER}", target.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 noPlayerToRandomTeleportFound();
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
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 noPlayerToRandomTeleportFound = Notice.chat("<red>✘ <dark_red>No player found to teleport!");
@Description({ " ", "# {PLAYER} - Player to whom he was teleported" })
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
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 noPlayerToRandomTeleportFound = Notice.chat("<red>✘ <dark_red>Nie można odnaleźć gracza do teleportacji!");
@Description({ " ", "# {PLAYER} - Player to whom he was teleported" })
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
public Notice teleportedToRandomPlayer = Notice.chat("<green>► <white>Zostałeś losowo teleportowany do <green>{PLAYER}<white>!");
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
}

@Description({
Expand Down