Skip to content

Commit

Permalink
build: update locales, adjust tp perm logic, close #575
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Mar 31, 2024
1 parent f871f9e commit 27713c7
Show file tree
Hide file tree
Showing 20 changed files with 66 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@

package net.william278.huskhomes.command;

import com.google.common.collect.Lists;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.*;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.OnlineUser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

public class TpCommand extends Command implements TabProvider {
Expand Down Expand Up @@ -81,44 +80,47 @@ public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
}

// Execute a teleport
private void execute(@NotNull CommandUser executor, @NotNull Teleportable teleportable, @NotNull Target target,
private void execute(@NotNull CommandUser executor, @NotNull Teleportable teleporter, @NotNull Target target,
@NotNull String[] args) {
// Build and execute the teleport
final TeleportBuilder builder = Teleport.builder(plugin)
.teleporter(teleportable)
.teleporter(teleporter)
.target(target);

if (executor instanceof OnlineUser onlineUser) {
if (target instanceof Teleportable teleportableTarget) {
if (!onlineUser.hasPermission(getPermission("other"))) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(executor::sendMessage);
return;
}

if (onlineUser.getUsername().equals(teleportableTarget.getUsername())) {
// Determine teleporter and target names, validate permissions
final @Nullable String targetName = target instanceof Username username ? username.name()
: target instanceof OnlineUser online ? online.getUsername() : null;
if (executor instanceof OnlineUser online) {
if (online.equals(teleporter)) {
if (teleporter.getUsername().equalsIgnoreCase(targetName)) {
plugin.getLocales().getLocale("error_cannot_teleport_self")
.ifPresent(onlineUser::sendMessage);
.ifPresent(executor::sendMessage);
return;
}
} else if (!executor.hasPermission(getPermission("other"))) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(executor::sendMessage);
return;
}
builder.executor(onlineUser);
builder.executor(online);
}

builder.buildAndComplete(false, args);
// Execute teleport
if (!builder.buildAndComplete(false, args)) {
return;
}

// Display a teleport completion message
final String teleporterName = teleportable instanceof OnlineUser user
? user.getUsername() : ((Username) teleportable).name();
// Display the teleport completion message
if (target instanceof Position position) {
plugin.getLocales().getLocale("teleporting_other_complete_position", teleporterName,
plugin.getLocales().getLocale("teleporting_other_complete_position", teleporter.getUsername(),
Integer.toString((int) position.getX()), Integer.toString((int) position.getY()),
Integer.toString((int) position.getZ()))
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("teleporting_other_complete", teleporterName, ((Username) target).name())
.ifPresent(executor::sendMessage);
return;
}
plugin.getLocales().getLocale("teleporting_other_complete",
teleporter.getUsername(), Objects.requireNonNull(targetName))
.ifPresent(executor::sendMessage);
}

@Override
Expand All @@ -129,8 +131,7 @@ public final List<String> suggest(@NotNull CommandUser user, @NotNull String[] a
final boolean servePlayerCompletions = user.hasPermission(getPermission("other"));
switch (args.length) {
case 0, 1 -> {
final ArrayList<String> completions = new ArrayList<>();
completions.addAll(serveCoordinateCompletions
final ArrayList<String> completions = Lists.newArrayList(serveCoordinateCompletions
? List.of("~", "~ ~", "~ ~ ~",
Integer.toString((int) relative.getX()),
((int) relative.getX() + " " + (int) relative.getY()),
Expand All @@ -149,13 +150,12 @@ public final List<String> suggest(@NotNull CommandUser user, @NotNull String[] a
completions.addAll(List.of("~", Integer.toString((int) relative.getY())));
completions.addAll(List.of("~ ~", (int) relative.getY() + " " + (int) relative.getZ()));
} else {
completions.addAll(
serveCoordinateCompletions
? List.of("~", "~ ~", "~ ~ ~",
Integer.toString((int) relative.getX()),
((int) relative.getX() + " " + (int) relative.getY()),
((int) relative.getX() + " " + (int) relative.getY() + " " + (int) relative.getZ()))
: List.of()
completions.addAll(serveCoordinateCompletions
? List.of("~", "~ ~", "~ ~ ~",
Integer.toString((int) relative.getX()),
((int) relative.getX() + " " + (int) relative.getY()),
((int) relative.getX() + " " + (int) relative.getY() + " " + (int) relative.getZ()))
: List.of()
);
if (servePlayerCompletions) {
completions.addAll(plugin.getPlayerList(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jetbrains.annotations.Nullable;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -146,6 +147,11 @@ public Instant getCreationTime() {
return creationTime;
}

@NotNull
public OffsetDateTime getCreationTimestamp() {
return OffsetDateTime.ofInstant(creationTime, OffsetDateTime.now().getOffset());
}

public void setCreationTime(Instant creationTime) {
this.creationTime = creationTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public interface Completable {
*
* @param args Optional args to pass to the teleport exception handler
*/
default void complete(@NotNull String... args) {
default boolean complete(@NotNull String... args) {
try {
execute();
} catch (TeleportationException e) {
e.displayMessage(getExecutor(), args);
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ public TimedTeleport toTimedTeleport() throws IllegalStateException {
);
}

public void buildAndComplete(boolean timed, @NotNull String... args) {
public boolean buildAndComplete(boolean timed, @NotNull String... args) {
try {
(timed ? toTimedTeleport() : toTeleport()).complete(args);
return (timed ? toTimedTeleport() : toTeleport()).complete(args);
} catch (TeleportationException e) {
e.displayMessage(executor);
}
return false;
}

private void validateTeleport() throws TeleportationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ public TeleportationException(@NotNull Type cause, @NotNull HuskHomes plugin) {

public void displayMessage(@NotNull CommandUser user, @NotNull String... args) {
switch (type) {
case TELEPORTER_NOT_FOUND, TARGET_NOT_FOUND -> plugin.getLocales()
case TELEPORTER_NOT_FOUND -> plugin.getLocales()
.getLocale("error_player_not_found", args)
.ifPresent(user::sendMessage);
case TARGET_NOT_FOUND -> plugin.getLocales()
.getLocale("error_target_not_found")
.ifPresent(user::sendMessage);
case ALREADY_WARMING_UP -> plugin.getLocales()
.getLocale("error_already_teleporting")
.ifPresent(user::sendMessage);
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/locales/bg-bg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ locales:
error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring teleport requests.](#ff7e5e) [[Stop Ignoring…]](#ff7e5e show_text=&#ff7e5e&Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)'
error_invalid_teleport_request: '[Error:](#ff3300) [You do not have a pending teleport request from %1%.](#ff7e5e)'
error_teleport_request_self: '[Error:](#ff3300) [You cannot send a teleport request to yourself.](#ff7e5e)'
error_target_not_found: '[Error:](#ff3300) [Could not find the target player.](#ff7e5e)'
error_player_not_found: '[Грешка:](#ff3300) [Не можахме да открием играча %1%.](#ff7e5e)'
error_rtp_restricted_world: '[Error:](#ff3300) [You cannot randomly teleport in this world.](#ff7e5e)'
error_on_cooldown: '[Error:](#ff3300) [You must wait %1% before doing that again.](#ff7e5e)'
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/locales/de-de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ locales:
error_ignoring_teleport_requests: '[Fehler:](#ff3300) [Du ignorierst derzeit Teleportanfragen.](#ff7e5e) [[Nich mehr ignorieren…]](#ff7e5e show_text=&#ff7e5e&Klicke, um eingehende Teleportanfragen wieder zu erhalten run_command=/huskhomes:tpignore)'
error_invalid_teleport_request: '[Fehler:](#ff3300) [Du hast keine ausstehende Teleportanfrage von %1%.](#ff7e5e)'
error_teleport_request_self: '[Fehler:](#ff3300) [Du kannst dir selbst keine Teleportanfrage stellen.](#ff7e5e)'
error_target_not_found: '[Error:](#ff3300) [Could not find the target player.](#ff7e5e)'
error_player_not_found: '[Fehler:](#ff3300) [Der Spieler %1% konnte nicht gefunden werden.](#ff7e5e)'
error_rtp_restricted_world: '[Fehler:](#ff3300) [Du kannst dich in dieser Welt nicht zufällig teleportieren.](#ff7e5e)'
error_on_cooldown: '[Fehler:](#ff3300) [Du musst %1% warten, bevor du dies wieder tun kannst.](#ff7e5e)'
Expand Down
3 changes: 2 additions & 1 deletion common/src/main/resources/locales/en-gb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ locales:
error_warp_description_length: '[Error:](#ff3300) [Warp descriptions must be less than 255 characters in length.](#ff7e5e)'
error_warp_description_characters: '[Error:](#ff3300) [Warp descriptions may only contain alphanumeric (A-Z, 0-9) characters, spaces, hyphens (-) and underscores (_)](#ff7e5e)'
error_no_permission: '[Error:](#ff3300) [You do not have permission to execute that command.](#ff7e5e)'
error_invalid_syntax: '[Error:](#ff3300) [Incorrect syntax. Usage:](#ff7e5e) [%1%](#ff7e5e italic show_text=&#ff7e5e&Click to suggest suggest_command=%1%)'
error_invalid_syntax: '[Error:](#ff3300) [Invalid syntax. Usage:](#ff7e5e) [%1%](#ff7e5e italic show_text=&#ff7e5e&Click to suggest suggest_command=%1%)'
error_already_teleporting: '[Error:](#ff3300) [Please wait for the current teleport to finish!](#ff7e5e)'
error_no_last_position: '[Error:](#ff3300) [You have no last position to return to!](#ff7e5e)'
error_in_game_only: 'Error: That command can only be run in-game.'
Expand All @@ -152,6 +152,7 @@ locales:
error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring teleport requests.](#ff7e5e) [[Stop Ignoring…]](#ff7e5e show_text=&#ff7e5e&Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)'
error_invalid_teleport_request: '[Error:](#ff3300) [You do not have a pending teleport request from %1%.](#ff7e5e)'
error_teleport_request_self: '[Error:](#ff3300) [You cannot send a teleport request to yourself.](#ff7e5e)'
error_target_not_found: '[Error:](#ff3300) [Could not find the target player.](#ff7e5e)'
error_player_not_found: '[Error:](#ff3300) [Could not find the player %1%.](#ff7e5e)'
error_rtp_restricted_world: '[Error:](#ff3300) [You cannot randomly teleport in this world.](#ff7e5e)'
error_on_cooldown: '[Error:](#ff3300) [You must wait %1% before doing that again.](#ff7e5e)'
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/locales/es-es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ locales:
error_ignoring_teleport_requests: '[Error:](#ff3300) [Actualmente está ignorando las solicitudes de teletransporte.](#ff7e5e) [[Dejar de ignorar…]](#ff7e5e show_text=&#ff7e5e&Clic para aceptar solicitudes de teletransporte run_command=/huskhomes:tpignore)'
error_invalid_teleport_request: '[Error:](#ff3300) [No tienes una solicitud de teletransporte pendiente de %1%..](#ff7e5e)'
error_teleport_request_self: '[Error:](#ff3300) [No puedes enviar una solicitud de teletransporte a ti mismo.](#ff7e5e)'
error_target_not_found: '[Error:](#ff3300) [Could not find the target player.](#ff7e5e)'
error_player_not_found: '[Error:](#ff3300) [No se ha podido encontrar el jugador %1%.](#ff7e5e)'
error_rtp_restricted_world: '[Error:](#ff3300) [No puedes teletransportarte al azar en este mundo.](#ff7e5e)'
error_on_cooldown: '[Error:](#ff3300) [You must wait %1% before doing that again.](#ff7e5e)'
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/locales/fr-fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ locales:
error_ignoring_teleport_requests: '[Erreur :](#ff3300) [Vous ingnorer actuellement les demandes de téléportations.](#ff7e5e) [[Arreter d''ignorer…]](#ff7e5e show_text=&#ff7e5e&Cliquez pour accepter les requêtes de téléportations run_command=/huskhomes:tpignore)'
error_invalid_teleport_request: '[Erreur :](#ff3300) [Vous n''avez recu aucune requêtes de téléportation de %1%.](#ff7e5e)'
error_teleport_request_self: '[Erreur :](#ff3300) [Vous ne pouvez pas vou téléportez à vous même.](#ff7e5e)'
error_target_not_found: '[Error:](#ff3300) [Could not find the target player.](#ff7e5e)'
error_player_not_found: '[Erreur :](#ff3300) [Le joueur %1% est introuvable.](#ff7e5e)'
error_rtp_restricted_world: '[Erreur :](#ff3300) [Vous ne pouvez pas vous téléporter aléatoirement dans ce monde.](#ff7e5e)'
error_on_cooldown: '[Erreur :](#ff3300) [Vous devez attendre %1% avant de refaire ça.](#ff7e5e)'
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/locales/it-it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ locales:
error_ignoring_teleport_requests: '[Errore:](#ff3300) [Stai ignorando le richieste di teletrasporto.](#ff7e5e) [[Riattiva…]](#ff7e5e show_text=&#ff7e5e&Clicca per attivare le richieste di teletrasporto run_command=/huskhomes:tpignore)'
error_invalid_teleport_request: '[Errore:](#ff3300) [Non hai richieste di teletrasporto da %1%.](#ff7e5e)'
error_teleport_request_self: '[Errore:](#ff3300) [Non puoi teletrasportarti da te stesso!](#ff7e5e)'
error_target_not_found: '[Error:](#ff3300) [Could not find the target player.](#ff7e5e)'
error_player_not_found: '[Errore:](#ff3300) [Non trovo il giocatore: %1%.](#ff7e5e)'
error_rtp_restricted_world: '[Errore:](#ff3300) [Non puoi teletrasportarti randomicamente in questo mondo.](#ff7e5e)'
error_on_cooldown: '[Error:](#ff3300) [You must wait %1% before doing that again.](#ff7e5e)'
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/locales/ja-jp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ locales:
error_ignoring_teleport_requests: '[エラー:](#ff3300) [現在、テレポートリクエストを無視しています。](#ff7e5e) [[無視を停止…]](#ff7e5e show_text=&#ff7e5e&クリックすると、テレポートリクエストを表示できます。 run_command=/huskhomes:tpignore)'
error_invalid_teleport_request: '[エラー:](#ff3300) [%1%からのテレポートリクエストは保留になっていません。](#ff7e5e)'
error_teleport_request_self: '[エラー:](#ff3300) [自分自身にテレポートリクエストを送ることはできません。](#ff7e5e)'
error_target_not_found: '[Error:](#ff3300) [Could not find the target player.](#ff7e5e)'
error_player_not_found: '[エラー:](#ff3300) [プレイヤー %1% が見つかりませんでした。](#ff7e5e)'
error_rtp_restricted_world: '[エラー:](#ff3300) [このワールドでは、ランダムテレポートはできません。](#ff7e5e)'
error_on_cooldown: '[Error:](#ff3300) [You must wait %1% before doing that again.](#ff7e5e)'
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/locales/ko-kr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ locales:
error_ignoring_teleport_requests: '[오류:](#ff3300) [당신은 이동 요청을 무시 중입니다.](#ff7e5e) [[전송 받기…]](#ff7e5e show_text=&#ff7e5e&클릭하여 이동 요청을 전송 받을 수 있습니다 run_command=/huskhomes:tpignore)'
error_invalid_teleport_request: '[오류:](#ff3300) [%1%님에게서 온 요청이 없습니다.](#ff7e5e)'
error_teleport_request_self: '[오류:](#ff3300) [자신에게 이동 요청을 보낼 수 없습니다.](#ff7e5e)'
error_target_not_found: '[Error:](#ff3300) [Could not find the target player.](#ff7e5e)'
error_player_not_found: '[오류:](#ff3300) [%1%님을 찾을 수 없습니다.](#ff7e5e)'
error_rtp_restricted_world: '[오류:](#ff3300) [이 세계에서는 랜덤 이동할 수 없습니다.](#ff7e5e)'
error_on_cooldown: '[오류:](#ff3300) [다시 하기 위해서는 %1%를 기다려야 합니다.](#ff7e5e)'
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/locales/pl-pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ locales:
error_ignoring_teleport_requests: '[Błąd:](#ff3300) [Aktualnie ignorujesz prośby o teleportację.](#ff7e5e) [[Przestań Ignorować…]](#ff7e5e show_text=&#ff7e5e&Kliknij, aby akceptować przychodzące prośby o teleportację run_command=/huskhomes:tpignore)'
error_invalid_teleport_request: '[Błąd:](#ff3300) [Nie masz oczekującej prośby o teleportację od %1%.](#ff7e5e)'
error_teleport_request_self: '[Błąd:](#ff3300) [Nie możesz wysłać prośby o teleportację do siebie.](#ff7e5e)'
error_target_not_found: '[Error:](#ff3300) [Could not find the target player.](#ff7e5e)'
error_player_not_found: '[Błąd:](#ff3300) [Nie można znaleźć gracza %1%.](#ff7e5e)'
error_rtp_restricted_world: '[Błąd:](#ff3300) [Nie możesz losowo teleportować się w tej krainie.](#ff7e5e)'
error_on_cooldown: '[Błąd:](#ff3300) [Musisz poczekać %1% przed kolejnym próbą.](#ff7e5e)'
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/locales/pt-br.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ locales:
error_ignoring_teleport_requests: '[Erro:](#ff3300) [Você está atualmente ignorando pedidos de teleporte.](#ff7e5e) [[Parar de Ignorar…]](#ff7e5e show_text=&#ff7e5e&Clique para escutar pedidos de teleporte recebidos run_command=/huskhomes:tpignore)'
error_invalid_teleport_request: '[Erro:](#ff3300) [Você não tem um pedido de teleporte pendente de %1%.](#ff7e5e)'
error_teleport_request_self: '[Erro:](#ff3300) [Você não pode enviar um pedido de teleporte para si mesmo.](#ff7e5e)'
error_target_not_found: '[Error:](#ff3300) [Could not find the target player.](#ff7e5e)'
error_player_not_found: '[Erro:](#ff3300) [Não foi possível encontrar o jogador %1%.](#ff7e5e)'
error_rtp_restricted_world: '[Erro:](#ff3300) [Você não pode se teleportar aleatoriamente neste mundo.](#ff7e5e)'
error_on_cooldown: '[Erro:](#ff3300) [Você deve esperar %1% antes de fazer isso novamente.](#ff7e5e)'
Expand Down
Loading

0 comments on commit 27713c7

Please sign in to comment.