build: update locales, adjust tp perm logic, close #575

This commit is contained in:
William committed 2024-03-31 16:22:48 +01:00
1 parent f871f9e1f2
commit 27713c7dd0
20 files changed
+66 -39

No files matched your search

@@ -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 {
@@ -81,44 +80,47 @@ public class TpCommand extends Command implements TabProvider {
}
// 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")
// 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(executor::sendMessage);
return;
}
if (onlineUser.getUsername().equals(teleportableTarget.getUsername())) {
plugin.getLocales().getLocale("error_cannot_teleport_self")
.ifPresent(onlineUser::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
@@ -129,8 +131,7 @@ public class TpCommand extends Command implements TabProvider {
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()),
@@ -149,13 +150,12 @@ public class TpCommand extends Command implements TabProvider {
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));
@@ -25,6 +25,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;
@@ -146,6 +147,11 @@ public class PositionMeta {
return creationTime;
}
@NotNull
public OffsetDateTime getCreationTimestamp() {
return OffsetDateTime.ofInstant(creationTime, OffsetDateTime.now().getOffset());
}
public void setCreationTime(Instant creationTime) {
this.creationTime = creationTime;
}
@@ -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;
}
}
@@ -71,12 +71,13 @@ public class TeleportBuilder {
);
}
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 {
@@ -38,9 +38,12 @@ public class TeleportationException extends IllegalStateException {
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);