mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 14:38:53 +00:00
More command refactoring
This commit is contained in:
1 parent
3bde5305d7
commit
34eb2c1f54
17 files changed
+358
-516
No files matched your search
@@ -49,14 +49,36 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Register permissions
|
// Register permissions
|
||||||
final PluginManager manager = plugin.getServer().getPluginManager();
|
addPermission(command.getPermission(), command.getUsage(), command.isOperatorCommand());
|
||||||
manager.addPermission(new Permission(command.getPermission(), "/" + command.getName(),
|
final List<Permission> childNodes = command.getAdditionalPermissions()
|
||||||
command.isOperatorCommand() ? PermissionDefault.OP : PermissionDefault.TRUE));
|
.entrySet().stream()
|
||||||
|
.map((entry) -> addPermission(entry.getKey(), "", entry.getValue()))
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.toList();
|
||||||
|
addPermission(command.getPermission("*"), command.getUsage(), command.isOperatorCommand(),
|
||||||
|
childNodes.toArray(new Permission[0]));
|
||||||
|
}
|
||||||
|
|
||||||
// Register master permission
|
@Nullable
|
||||||
final Map<String, Boolean> childNodes = new HashMap<>();
|
private Permission addPermission(@NotNull String node, @NotNull String description, boolean operatorCommand, @NotNull Permission... children) {
|
||||||
manager.addPermission(new Permission(command.getPermission() + ".*", command.getUsage(),
|
final Map<String, Boolean> childNodes = Arrays.stream(children)
|
||||||
PermissionDefault.FALSE, childNodes));
|
.map(Permission::getName)
|
||||||
|
.collect(HashMap::new, (map, child) -> map.put(child, true), HashMap::putAll);
|
||||||
|
|
||||||
|
final PluginManager manager = plugin.getServer().getPluginManager();
|
||||||
|
if (manager.getPermission(node) != null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Permission permission;
|
||||||
|
if (description.isEmpty()) {
|
||||||
|
permission = new Permission(node, operatorCommand ? PermissionDefault.OP : PermissionDefault.TRUE, childNodes);
|
||||||
|
} else {
|
||||||
|
permission = new Permission(node, description, operatorCommand ? PermissionDefault.OP : PermissionDefault.TRUE, childNodes);
|
||||||
|
}
|
||||||
|
manager.addPermission(permission);
|
||||||
|
|
||||||
|
return permission;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,22 +1,13 @@
|
|||||||
package net.william278.huskhomes;
|
package net.william278.huskhomes;
|
||||||
|
|
||||||
import de.themoep.minedown.adventure.MineDown;
|
|
||||||
import net.william278.huskhomes.command.Command;
|
|
||||||
import net.william278.huskhomes.config.Locales;
|
|
||||||
import net.william278.huskhomes.database.Database;
|
import net.william278.huskhomes.database.Database;
|
||||||
import net.william278.huskhomes.user.CommandUser;
|
import net.william278.huskhomes.teleport.TimedTeleport;
|
||||||
import net.william278.huskhomes.user.OnlineUser;
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
import net.william278.huskhomes.user.User;
|
import net.william278.huskhomes.user.User;
|
||||||
import net.william278.huskhomes.position.Home;
|
|
||||||
import net.william278.huskhomes.position.Warp;
|
|
||||||
import net.william278.huskhomes.teleport.TimedTeleport;
|
|
||||||
import net.william278.paginedown.ListOptions;
|
|
||||||
import net.william278.paginedown.PaginatedList;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A cache used to hold frequently accessed data (i.e. TAB-completed homes and warps)
|
* A cache used to hold frequently accessed data (i.e. TAB-completed homes and warps)
|
||||||
@@ -28,9 +19,6 @@ public class Cache {
|
|||||||
private final Map<String, List<String>> publicHomes = new HashMap<>();
|
private final Map<String, List<String>> publicHomes = new HashMap<>();
|
||||||
private final List<String> warps = new ArrayList<>();
|
private final List<String> warps = new ArrayList<>();
|
||||||
private final Set<String> players = new HashSet<>();
|
private final Set<String> players = new HashSet<>();
|
||||||
private final Map<String, PaginatedList> privateHomeLists = new HashMap<>();
|
|
||||||
private final Map<UUID, PaginatedList> publicHomeLists = new HashMap<>();
|
|
||||||
private final Map<UUID, PaginatedList> warpLists = new HashMap<>();
|
|
||||||
private final Set<UUID> currentlyOnWarmup = new HashSet<>();
|
private final Set<UUID> currentlyOnWarmup = new HashSet<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,27 +107,6 @@ public class Cache {
|
|||||||
return players;
|
return players;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Cached lists of private homes for pagination, mapped to the username of the home owner
|
|
||||||
*/
|
|
||||||
public Map<String, PaginatedList> getPrivateHomeLists() {
|
|
||||||
return privateHomeLists;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cached lists of public homes for pagination
|
|
||||||
*/
|
|
||||||
public Map<UUID, PaginatedList> getPublicHomeLists() {
|
|
||||||
return publicHomeLists;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cached lists of warps for pagination
|
|
||||||
*/
|
|
||||||
public Map<UUID, PaginatedList> getWarpLists() {
|
|
||||||
return warpLists;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cached user UUIDs currently on warmup countdowns for {@link TimedTeleport}s
|
* Cached user UUIDs currently on warmup countdowns for {@link TimedTeleport}s
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -6,12 +6,14 @@ import net.william278.huskhomes.user.ConsoleUser;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public abstract class Command extends Node implements TabProvider {
|
public abstract class Command extends Node implements TabProvider {
|
||||||
|
|
||||||
private final String usage;
|
private final String usage;
|
||||||
|
|
||||||
protected Command(@NotNull String name, @NotNull List<String> aliases, @NotNull String usage, @NotNull HuskHomes plugin) {
|
protected Command(@NotNull String name, @NotNull List<String> aliases, @NotNull String usage,
|
||||||
|
@NotNull HuskHomes plugin) {
|
||||||
super(name, aliases, plugin);
|
super(name, aliases, plugin);
|
||||||
this.usage = usage;
|
this.usage = usage;
|
||||||
}
|
}
|
||||||
@@ -24,7 +26,7 @@ public abstract class Command extends Node implements TabProvider {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (executor instanceof ConsoleUser && !isConsoleExecutable()) {
|
if (executor instanceof ConsoleUser && !isConsoleExecutable()) {
|
||||||
plugin.getLocales().getLocale("error_command_in_game_only")
|
plugin.getLocales().getLocale("error_in_game_only")
|
||||||
.ifPresent(executor::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -58,4 +60,9 @@ public abstract class Command extends Node implements TabProvider {
|
|||||||
.orElse(getUsage());
|
.orElse(getUsage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Map<String, Boolean> getAdditionalPermissions() {
|
||||||
|
return Map.of();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,130 +2,101 @@ package net.william278.huskhomes.command;
|
|||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.hook.EconomyHook;
|
import net.william278.huskhomes.hook.EconomyHook;
|
||||||
import net.william278.huskhomes.user.OnlineUser;
|
|
||||||
import net.william278.huskhomes.user.SavedUser;
|
|
||||||
import net.william278.huskhomes.position.Position;
|
import net.william278.huskhomes.position.Position;
|
||||||
import net.william278.huskhomes.teleport.Teleport;
|
import net.william278.huskhomes.teleport.Teleport;
|
||||||
import net.william278.huskhomes.util.Permission;
|
import net.william278.huskhomes.teleport.TeleportBuilder;
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import net.william278.huskhomes.teleport.TeleportationException;
|
||||||
|
import net.william278.huskhomes.user.CommandUser;
|
||||||
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
|
import net.william278.huskhomes.user.SavedUser;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
public class RtpCommand extends Command implements ConsoleExecutable {
|
public class RtpCommand extends Command {
|
||||||
|
|
||||||
protected RtpCommand(@NotNull HuskHomes implementor) {
|
protected RtpCommand(@NotNull HuskHomes plugin) {
|
||||||
super("rtp", Permission.COMMAND_RTP, implementor);
|
super("rtp", List.of(), "[player]", plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||||
OnlineUser target = onlineUser;
|
final Optional<OnlineUser> optionalTeleporter = args.length >= 1 ? plugin.findOnlinePlayer(args[0])
|
||||||
if (args.length >= 1) {
|
: executor instanceof OnlineUser ? Optional.of((OnlineUser) executor) : Optional.empty();
|
||||||
if (!onlineUser.hasPermission(Permission.COMMAND_RTP_OTHER.node)) {
|
if (optionalTeleporter.isEmpty()) {
|
||||||
plugin.getLocales().getLocale("error_no_permission").ifPresent(onlineUser::sendMessage);
|
if (args.length == 0) {
|
||||||
|
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||||
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final Optional<OnlineUser> foundUser = plugin.findOnlinePlayer(args[0]);
|
|
||||||
if (foundUser.isEmpty()) {
|
|
||||||
plugin.getLocales().getLocale("error_player_not_found", args[0])
|
plugin.getLocales().getLocale("error_player_not_found", args[0])
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
target = foundUser.get();
|
|
||||||
|
final OnlineUser teleporter = optionalTeleporter.get();
|
||||||
|
if (!executor.equals(teleporter) && !executor.hasPermission(getPermission("other"))) {
|
||||||
|
plugin.getLocales().getLocale("error_no_permission")
|
||||||
|
.ifPresent(executor::sendMessage);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
final Position userPosition = target.getPosition();
|
|
||||||
final String[] rtpArguments = args.length >= 1 ? ArrayUtils.subarray(args, 1, args.length) : args;
|
this.executeRtp(teleporter, executor, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void executeRtp(@NotNull OnlineUser teleporter, @NotNull CommandUser executor, @NotNull String[] args) {
|
||||||
|
if (!plugin.validateEconomyCheck(teleporter, EconomyHook.Action.RANDOM_TELEPORT)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (plugin.getSettings().getRtpRestrictedWorlds().stream()
|
if (plugin.getSettings().getRtpRestrictedWorlds().stream()
|
||||||
.anyMatch(worldName -> worldName.equals(userPosition.getWorld().getName()))) {
|
.anyMatch(worldName -> worldName.equals(teleporter.getPosition().getWorld().getName()))) {
|
||||||
plugin.getLocales().getLocale("error_rtp_restricted_world")
|
plugin.getLocales().getLocale("error_rtp_restricted_world")
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform economy check if necessary
|
final SavedUser user = plugin.getDatabase().getUserData(teleporter.getUuid())
|
||||||
if (!plugin.validateEconomyCheck(onlineUser, EconomyHook.Action.RANDOM_TELEPORT)) {
|
.orElseThrow(() -> new IllegalStateException("No user data found for " + teleporter.getUsername()));
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final OnlineUser userToTeleport = target;
|
|
||||||
final boolean isExecutorTeleporting = userToTeleport.equals(onlineUser);
|
|
||||||
plugin.getDatabase().getUserData(onlineUser.getUuid()).thenAccept(userData -> {
|
|
||||||
// Check the user is not still on /rtp cooldown
|
|
||||||
if (userData.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final Instant currentTime = Instant.now();
|
final Instant currentTime = Instant.now();
|
||||||
if (isExecutorTeleporting && !currentTime.isAfter(userData.get().rtpCooldown())
|
if (executor.equals(teleporter) && !currentTime.isAfter(user.getRtpCooldown()) &&
|
||||||
&& !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) {
|
!executor.hasPermission(getPermission("bypass_cooldown"))) {
|
||||||
plugin.getLocales().getLocale("error_rtp_cooldown",
|
plugin.getLocales().getLocale("error_rtp_cooldown",
|
||||||
Long.toString(currentTime.until(userData.get().rtpCooldown(), ChronoUnit.MINUTES) + 1))
|
Long.toString(currentTime.until(user.getRtpCooldown(), ChronoUnit.MINUTES) + 1))
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a random position and teleport
|
|
||||||
plugin.getLocales().getLocale("teleporting_random_generation")
|
plugin.getLocales().getLocale("teleporting_random_generation")
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(teleporter::sendMessage);
|
||||||
|
final Optional<Position> position = plugin.getRandomTeleportEngine()
|
||||||
final Optional<Position> position = plugin.getRandomTeleportEngine().getRandomPosition(onlineUser.getPosition().getWorld(), rtpArguments);
|
.getRandomPosition(teleporter.getPosition().getWorld(), removeFirstArg(args));
|
||||||
if (position.isEmpty()) {
|
if (position.isEmpty()) {
|
||||||
plugin.getLocales().getLocale("error_rtp_randomization_timeout")
|
plugin.getLocales().getLocale("error_rtp_randomization_timeout")
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final TeleportBuilder builder = Teleport.builder(plugin, userToTeleport)
|
final TeleportBuilder builder = Teleport.builder(plugin)
|
||||||
.setTarget(position.get());
|
.teleporter(teleporter)
|
||||||
final CompletableFuture<? extends Teleport> teleportFuture = isExecutorTeleporting
|
.target(position.get());
|
||||||
? builder.setEconomyActions(EconomyHook.Action.RANDOM_TELEPORT).toTimedTeleport()
|
try {
|
||||||
: builder.toTeleport();
|
if (executor.equals(teleporter)) {
|
||||||
|
builder.toTimedTeleport().execute();
|
||||||
teleportFuture.thenAccept(teleport -> teleport.execute()
|
|
||||||
.thenAccept(result -> {
|
|
||||||
if (isExecutorTeleporting &&
|
|
||||||
result.successful() && !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) {
|
|
||||||
plugin.getDatabase().updateUserData(new SavedUser(onlineUser,
|
|
||||||
userData.get().homeSlots(), userData.get().ignoringTeleports(),
|
|
||||||
Instant.now().plus(plugin.getSettings().getRtpCooldownLength(), ChronoUnit.MINUTES)));
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onConsoleExecute(@NotNull String[] args) {
|
|
||||||
if (args.length == 0) {
|
|
||||||
plugin.log(Level.WARNING, "Invalid syntax. Usage: rtp [player]");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final Optional<OnlineUser> foundUser = plugin.findOnlinePlayer(args[0]);
|
|
||||||
if (foundUser.isEmpty()) {
|
|
||||||
plugin.log(Level.WARNING, "Player not found: " + args[0]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
plugin.log(Level.INFO, "Finding a random position for " + foundUser.get().getUsername() + "...");
|
|
||||||
plugin.getRandomTeleportEngine().getRandomPosition(foundUser.get().getPosition().getWorld(), ArrayUtils.subarray(args, 1, args.length)).thenAccept(position -> {
|
|
||||||
if (position.isEmpty()) {
|
|
||||||
plugin.log(Level.WARNING, "Failed to teleport " + foundUser.get().getUsername() + " to a random position; randomization timed out!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Teleport.builder(plugin, foundUser.get())
|
|
||||||
.setTarget(position.get())
|
|
||||||
.toTeleport()
|
|
||||||
.thenAccept(teleport -> teleport.execute().thenAccept(result -> {
|
|
||||||
if (result.successful()) {
|
|
||||||
plugin.log(Level.INFO, "Teleported " + foundUser.get().getUsername() + " to a random position.");
|
|
||||||
} else {
|
} else {
|
||||||
plugin.log(Level.WARNING, "Failed to teleport" + foundUser.get().getUsername() + " to a random position.");
|
builder.toTeleport().execute();
|
||||||
|
}
|
||||||
|
} catch (TeleportationException e) {
|
||||||
|
e.displayMessage(executor, plugin, args);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
|
user.setRtpCooldown(Instant.now().plus(plugin.getSettings().getRtpCooldownLength(), ChronoUnit.MINUTES));
|
||||||
|
plugin.getDatabase().updateUserData(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -114,6 +114,12 @@ public abstract class SavedPositionCommand<T extends SavedPosition> extends Comm
|
|||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@NotNull
|
||||||
|
public Map<String, Boolean> getAdditionalPermissions() {
|
||||||
|
return Map.of(getOtherPermission(), true);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public List<String> suggest(@NotNull CommandUser executor, @NotNull String[] args) {
|
public List<String> suggest(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||||
|
|||||||
@@ -1,129 +1,48 @@
|
|||||||
package net.william278.huskhomes.command;
|
package net.william278.huskhomes.command;
|
||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.hook.EconomyHook;
|
import net.william278.huskhomes.user.CommandUser;
|
||||||
import net.william278.huskhomes.user.OnlineUser;
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
import net.william278.huskhomes.user.SavedUser;
|
|
||||||
import net.william278.huskhomes.position.Home;
|
import net.william278.huskhomes.position.Home;
|
||||||
import net.william278.huskhomes.position.PositionMeta;
|
import net.william278.huskhomes.util.ValidationException;
|
||||||
import net.william278.huskhomes.util.Permission;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
public class SetHomeCommand extends Command {
|
public class SetHomeCommand extends SetPositionCommand {
|
||||||
|
|
||||||
protected SetHomeCommand(@NotNull HuskHomes implementor) {
|
protected SetHomeCommand(@NotNull HuskHomes plugin) {
|
||||||
super("sethome", Permission.COMMAND_SET_HOME, implementor);
|
super("sethome", plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||||
plugin.getDatabase().getHomes(onlineUser).thenAcceptAsync(homes -> {
|
if (executor instanceof OnlineUser user && args.length == 0 && createDefaultHome(user)) {
|
||||||
switch (args.length) {
|
|
||||||
case 0 -> {
|
|
||||||
if (homes.isEmpty()) {
|
|
||||||
setHome(onlineUser, "home", homes);
|
|
||||||
} else {
|
|
||||||
plugin.getLocales().getLocale("error_invalid_syntax", "/sethome <name>")
|
|
||||||
.ifPresent(onlineUser::sendMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case 1 -> setHome(onlineUser, args[0], homes);
|
|
||||||
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/sethome <name>")
|
|
||||||
.ifPresent(onlineUser::sendMessage);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attempts to set a home by given name for the {@link OnlineUser}.
|
|
||||||
* <p>
|
|
||||||
* A number of validation checks will take place before the home is set. If these checks fail, the home won't be set.
|
|
||||||
* <ul>
|
|
||||||
* <li>The user's currentHomes must not exceed the permissive maximum home limit</li>
|
|
||||||
* <li>If economy features are on and the user does not have enough home slots, they must have sufficient funds to buy another</li>
|
|
||||||
* <li>The home name must not already exist</li>
|
|
||||||
* <li>The home name must meet the length and character criteria</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* @param onlineUser The {@link OnlineUser} to set the home for
|
|
||||||
* @param homeName The name of the home to set
|
|
||||||
* @param currentHomes The current homes of the {@link OnlineUser}
|
|
||||||
*/
|
|
||||||
private void setHome(@NotNull OnlineUser onlineUser, @NotNull String homeName, @NotNull List<Home> currentHomes) {
|
|
||||||
// Check against maximum homes
|
|
||||||
final int maxHomes = onlineUser.getMaxHomes(plugin.getSettings().getMaxHomes(), plugin.getSettings().doStackPermissionLimits());
|
|
||||||
if (currentHomes.size() >= maxHomes) {
|
|
||||||
plugin.getLocales().getLocale("error_set_home_maximum_homes", Integer.toString(maxHomes))
|
|
||||||
.ifPresent(onlineUser::sendMessage);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
super.execute(executor, args);
|
||||||
|
}
|
||||||
|
|
||||||
// Get their user data
|
@Override
|
||||||
plugin.getDatabase().getUserData(onlineUser.getUuid()).thenAccept(fetchedData -> {
|
protected void execute(@NotNull OnlineUser setter, @NotNull String name) {
|
||||||
// Check against economy if needed
|
try {
|
||||||
final AtomicBoolean newSlotNeeded = new AtomicBoolean(false);
|
plugin.getManager().homes().createHome(setter, name, setter.getPosition());
|
||||||
final AtomicReference<SavedUser> userDataToUpdate = new AtomicReference<>(null);
|
} catch (ValidationException e) {
|
||||||
if (plugin.getSettings().doEconomy()) {
|
e.dispatchHomeError(setter, false, plugin, name);
|
||||||
final int freeHomes = onlineUser.getFreeHomes(plugin.getSettings().getFreeHomeSlots(),
|
|
||||||
plugin.getSettings().doStackPermissionLimits());
|
|
||||||
if (fetchedData.isPresent()) {
|
|
||||||
final EconomyHook.Action action = EconomyHook.Action.ADDITIONAL_HOME_SLOT;
|
|
||||||
newSlotNeeded.set((currentHomes.size() + 1) > (freeHomes + fetchedData.get().homeSlots()));
|
|
||||||
|
|
||||||
// If a new slot is needed, validate the user has enough funds to purchase one
|
|
||||||
if (newSlotNeeded.get()) {
|
|
||||||
if (!plugin.validateEconomyCheck(onlineUser, action)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
userDataToUpdate.set(new SavedUser(onlineUser, (currentHomes.size() + 1) - freeHomes,
|
|
||||||
fetchedData.get().ignoringTeleports(), fetchedData.get().rtpCooldown()));
|
|
||||||
} else {
|
|
||||||
if (currentHomes.size() == freeHomes) {
|
|
||||||
plugin.getEconomyHook()
|
|
||||||
.flatMap(economyHook -> plugin.getSettings().getEconomyCost(action)
|
|
||||||
.map(economyHook::formatCurrency))
|
|
||||||
.flatMap(formatted -> plugin.getLocales().getLocale("set_home_used_free_slots",
|
|
||||||
Integer.toString(freeHomes), formatted))
|
|
||||||
.ifPresent(onlineUser::sendMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the home in the saved position manager
|
private boolean createDefaultHome(@NotNull OnlineUser user) {
|
||||||
plugin.getManager()
|
final List<Home> homes = plugin.getDatabase().getHomes(user);
|
||||||
.setHome(new PositionMeta(homeName, ""), onlineUser, onlineUser.getPosition())
|
final Optional<String> name = homes.isEmpty() ? Optional.of("home") :
|
||||||
.thenAccept(setResult -> {
|
(homes.size() == 1 && plugin.getSettings().doOverwriteExistingHomesWarps())
|
||||||
// Display feedback of the result of the set operation
|
? Optional.of(homes.get(0).getName()) : Optional.empty();
|
||||||
(switch (setResult.resultType()) {
|
if (name.isPresent()) {
|
||||||
case SUCCESS -> {
|
this.execute(user, "home");
|
||||||
assert setResult.savedPosition().isPresent();
|
return true;
|
||||||
|
|
||||||
// If the user needed to buy a new slot, perform the transaction and update their data
|
|
||||||
if (newSlotNeeded.get()) {
|
|
||||||
plugin.performEconomyTransaction(onlineUser, EconomyHook.Action.ADDITIONAL_HOME_SLOT);
|
|
||||||
plugin.getDatabase().updateUserData(userDataToUpdate.get());
|
|
||||||
}
|
}
|
||||||
yield plugin.getLocales().getLocale("set_home_success", setResult.savedPosition().get().meta.name);
|
return false;
|
||||||
}
|
|
||||||
case SUCCESS_OVERWRITTEN -> {
|
|
||||||
assert setResult.savedPosition().isPresent();
|
|
||||||
yield plugin.getLocales().getLocale("edit_home_update_location",
|
|
||||||
setResult.savedPosition().get().meta.name);
|
|
||||||
}
|
|
||||||
case FAILED_DUPLICATE -> plugin.getLocales().getLocale("error_home_name_taken");
|
|
||||||
case FAILED_NAME_LENGTH -> plugin.getLocales().getLocale("error_home_name_length");
|
|
||||||
case FAILED_NAME_CHARACTERS -> plugin.getLocales().getLocale("error_home_name_characters");
|
|
||||||
default -> plugin.getLocales().getLocale("error_home_description_characters");
|
|
||||||
}).ifPresent(onlineUser::sendMessage);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package net.william278.huskhomes.command;
|
||||||
|
|
||||||
|
import net.william278.huskhomes.HuskHomes;
|
||||||
|
import net.william278.huskhomes.user.CommandUser;
|
||||||
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public abstract class SetPositionCommand extends Command {
|
||||||
|
|
||||||
|
protected SetPositionCommand(@NotNull String name, @NotNull HuskHomes plugin) {
|
||||||
|
super(name, List.of(), "<name>", plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||||
|
if (!(executor instanceof OnlineUser user)) {
|
||||||
|
plugin.getLocales().getLocale("error_in_game_only")
|
||||||
|
.ifPresent(executor::sendMessage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Optional<String> name = parseStringArg(args, 0);
|
||||||
|
if (name.isEmpty()) {
|
||||||
|
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||||
|
.ifPresent(user::sendMessage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.execute(user, name.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void execute(@NotNull OnlineUser setter, @NotNull String name);
|
||||||
|
}
|
||||||
@@ -1,46 +1,45 @@
|
|||||||
package net.william278.huskhomes.command;
|
package net.william278.huskhomes.command;
|
||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.user.OnlineUser;
|
|
||||||
import net.william278.huskhomes.position.Position;
|
import net.william278.huskhomes.position.Position;
|
||||||
import net.william278.huskhomes.position.PositionMeta;
|
import net.william278.huskhomes.user.CommandUser;
|
||||||
import net.william278.huskhomes.util.Permission;
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
|
import net.william278.huskhomes.util.ValidationException;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class SetSpawnCommand extends Command {
|
public class SetSpawnCommand extends Command {
|
||||||
|
|
||||||
protected SetSpawnCommand(@NotNull HuskHomes implementor) {
|
protected SetSpawnCommand(@NotNull HuskHomes plugin) {
|
||||||
super("setspawn", Permission.COMMAND_SET_SPAWN, implementor);
|
super("setspawn", List.of(), "", plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||||
if (args.length > 0) {
|
if (!(executor instanceof OnlineUser user)) {
|
||||||
plugin.getLocales().getLocale("error_invalid_syntax", "/setspawn")
|
plugin.getLocales().getLocale("error_in_game_only")
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Position position = onlineUser.getPosition();
|
final Position position = user.getPosition();
|
||||||
|
try {
|
||||||
if (plugin.getSettings().isCrossServer() && plugin.getSettings().isGlobalSpawn()) {
|
if (plugin.getSettings().isCrossServer() && plugin.getSettings().isGlobalSpawn()) {
|
||||||
plugin.getDatabase().getWarp(plugin.getSettings().getGlobalSpawnName()).thenApply(warp -> {
|
final String warpName = plugin.getSettings().getGlobalSpawnName();
|
||||||
if (warp.isPresent()) {
|
plugin.getManager().warps().createWarp(warpName, position, true);
|
||||||
return plugin.getManager().updateWarpPosition(warp.get(), position);
|
plugin.getLocales().getRawLocale("spawn_warp_default_description")
|
||||||
} else {
|
.ifPresent(description -> plugin.getManager().warps().setWarpDescription(warpName, description));
|
||||||
return plugin.getManager().setWarp(new PositionMeta(plugin.getSettings().getGlobalSpawnName(),
|
|
||||||
plugin.getLocales().getRawLocale("spawn_warp_default_description").orElse("")), position)
|
|
||||||
.thenApply(result -> result.resultType().successful);
|
|
||||||
}
|
|
||||||
}).thenAccept(result -> result.thenAccept(successful -> {
|
|
||||||
if (successful) {
|
|
||||||
plugin.getLocales().getLocale("set_spawn_success")
|
|
||||||
.ifPresent(onlineUser::sendMessage);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
} else {
|
} else {
|
||||||
plugin.setServerSpawn(position);
|
plugin.setServerSpawn(position);
|
||||||
|
}
|
||||||
|
} catch (ValidationException e) {
|
||||||
|
e.dispatchWarpError(executor, plugin, plugin.getSettings().getGlobalSpawnName());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
plugin.getLocales().getLocale("set_spawn_success")
|
plugin.getLocales().getLocale("set_spawn_success")
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,45 +2,21 @@ package net.william278.huskhomes.command;
|
|||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.user.OnlineUser;
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
import net.william278.huskhomes.position.PositionMeta;
|
import net.william278.huskhomes.util.ValidationException;
|
||||||
import net.william278.huskhomes.util.Permission;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class SetWarpCommand extends Command {
|
public class SetWarpCommand extends SetPositionCommand {
|
||||||
|
|
||||||
protected SetWarpCommand(@NotNull HuskHomes implementor) {
|
protected SetWarpCommand(@NotNull HuskHomes plugin) {
|
||||||
super("setwarp", Permission.COMMAND_SET_WARP, implementor);
|
super("setwarp", plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
protected void execute(@NotNull OnlineUser setter, @NotNull String name) {
|
||||||
if (args.length == 1) {
|
try {
|
||||||
setWarp(onlineUser, args[0]);
|
plugin.getManager().warps().createWarp(name, setter.getPosition());
|
||||||
} else {
|
} catch (ValidationException e) {
|
||||||
plugin.getLocales().getLocale("error_invalid_syntax", "/setwarp <name>")
|
e.dispatchWarpError(setter, plugin, name);
|
||||||
.ifPresent(onlineUser::sendMessage);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setWarp(@NotNull OnlineUser onlineUser, @NotNull String warpName) {
|
|
||||||
plugin.getManager().setWarp(
|
|
||||||
new PositionMeta(warpName, ""), onlineUser.getPosition()).thenAccept(setResult ->
|
|
||||||
(switch (setResult.resultType()) {
|
|
||||||
case SUCCESS -> {
|
|
||||||
assert setResult.savedPosition().isPresent();
|
|
||||||
yield plugin.getLocales().getLocale("set_warp_success",
|
|
||||||
setResult.savedPosition().get().meta.name);
|
|
||||||
}
|
|
||||||
case SUCCESS_OVERWRITTEN -> {
|
|
||||||
assert setResult.savedPosition().isPresent();
|
|
||||||
yield plugin.getLocales().getLocale("edit_warp_update_location",
|
|
||||||
setResult.savedPosition().get().meta.name);
|
|
||||||
}
|
|
||||||
case FAILED_DUPLICATE -> plugin.getLocales().getLocale("error_warp_name_taken");
|
|
||||||
case FAILED_NAME_LENGTH -> plugin.getLocales().getLocale("error_warp_name_length");
|
|
||||||
case FAILED_NAME_CHARACTERS -> plugin.getLocales().getLocale("error_warp_name_characters");
|
|
||||||
default -> plugin.getLocales().getLocale("error_warp_description_characters");
|
|
||||||
}).ifPresent(onlineUser::sendMessage));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,38 +1,62 @@
|
|||||||
package net.william278.huskhomes.command;
|
package net.william278.huskhomes.command;
|
||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.user.OnlineUser;
|
|
||||||
import net.william278.huskhomes.position.Position;
|
import net.william278.huskhomes.position.Position;
|
||||||
import net.william278.huskhomes.teleport.Teleport;
|
import net.william278.huskhomes.teleport.Teleport;
|
||||||
import net.william278.huskhomes.teleport.TimedTeleport;
|
import net.william278.huskhomes.teleport.TeleportBuilder;
|
||||||
import net.william278.huskhomes.util.Permission;
|
import net.william278.huskhomes.teleport.Teleportable;
|
||||||
|
import net.william278.huskhomes.teleport.TeleportationException;
|
||||||
|
import net.william278.huskhomes.user.CommandUser;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class SpawnCommand extends Command {
|
public class SpawnCommand extends Command {
|
||||||
|
|
||||||
protected SpawnCommand(@NotNull HuskHomes implementor) {
|
protected SpawnCommand(@NotNull HuskHomes plugin) {
|
||||||
super("spawn", Permission.COMMAND_SPAWN, implementor);
|
super("spawn", List.of(), "[player]", plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||||
if (args.length > 0) {
|
|
||||||
plugin.getLocales().getLocale("error_invalid_syntax", "/spawn")
|
|
||||||
.ifPresent(onlineUser::sendMessage);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Optional<? extends Position> spawn = plugin.getSpawn();
|
final Optional<? extends Position> spawn = plugin.getSpawn();
|
||||||
if (spawn.isEmpty()) {
|
if (spawn.isEmpty()) {
|
||||||
plugin.getLocales().getLocale("error_spawn_not_set")
|
plugin.getLocales().getLocale("error_spawn_not_set")
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Teleport.builder(plugin, onlineUser)
|
|
||||||
.setTarget(spawn.get())
|
final Optional<Teleportable> optionalTeleporter = resolveTeleportable(executor, args);
|
||||||
.toTimedTeleport()
|
if (optionalTeleporter.isEmpty()) {
|
||||||
.thenAccept(TimedTeleport::execute);
|
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||||
|
.ifPresent(executor::sendMessage);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.teleportToSpawn(optionalTeleporter.get(), executor, spawn.get(), args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void teleportToSpawn(@NotNull Teleportable teleporter, @NotNull CommandUser executor, @NotNull Position spawn,
|
||||||
|
@NotNull String[] args) {
|
||||||
|
if (!executor.equals(teleporter) && !executor.hasPermission(getPermission("other"))) {
|
||||||
|
plugin.getLocales().getLocale("error_no_permission")
|
||||||
|
.ifPresent(executor::sendMessage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final TeleportBuilder builder = Teleport.builder(plugin)
|
||||||
|
.teleporter(teleporter)
|
||||||
|
.target(spawn);
|
||||||
|
try {
|
||||||
|
if (teleporter.equals(executor)) {
|
||||||
|
builder.toTimedTeleport().execute();
|
||||||
|
} else {
|
||||||
|
builder.toTeleport().execute();
|
||||||
|
}
|
||||||
|
} catch (TeleportationException e) {
|
||||||
|
e.displayMessage(executor, plugin, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,55 +1,74 @@
|
|||||||
package net.william278.huskhomes.command;
|
package net.william278.huskhomes.command;
|
||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.user.OnlineUser;
|
import net.william278.huskhomes.position.Position;
|
||||||
import net.william278.huskhomes.teleport.Teleport;
|
import net.william278.huskhomes.teleport.Teleport;
|
||||||
import net.william278.huskhomes.util.Permission;
|
import net.william278.huskhomes.teleport.TeleportationException;
|
||||||
|
import net.william278.huskhomes.user.CommandUser;
|
||||||
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
|
import net.william278.huskhomes.user.SavedUser;
|
||||||
|
import net.william278.huskhomes.user.User;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class TpOfflineCommand extends Command implements TabProvider {
|
public class TpOfflineCommand extends Command implements TabProvider {
|
||||||
|
|
||||||
protected TpOfflineCommand(@NotNull HuskHomes implementor) {
|
protected TpOfflineCommand(@NotNull HuskHomes plugin) {
|
||||||
super("tpoffline", Permission.COMMAND_TPOFFLINE, implementor);
|
super("tpoffline", List.of(), "<player>", plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||||
if (args.length != 1) {
|
if (!(executor instanceof OnlineUser user)) {
|
||||||
plugin.getLocales().getLocale("error_invalid_syntax", "/tpoffline <player>")
|
plugin.getLocales().getLocale("error_in_game_only")
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final String targetUser = args[0];
|
|
||||||
plugin.getDatabase().getUserDataByName(targetUser).thenAccept(userData -> {
|
final Optional<String> optionalUser = parseStringArg(args, 0);
|
||||||
if (userData.isEmpty()) {
|
if (optionalUser.isEmpty()) {
|
||||||
plugin.getLocales().getLocale("error_player_not_found", targetUser)
|
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
plugin.getDatabase().getOfflinePosition(userData.get().user()).thenAccept(offlinePosition -> {
|
|
||||||
if (offlinePosition.isEmpty()) {
|
final Optional<User> targetUserData = plugin.getDatabase()
|
||||||
plugin.getLocales().getLocale("error_no_offline_position", targetUser)
|
.getUserDataByName(optionalUser.get())
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.map(SavedUser::getUser);
|
||||||
|
if (targetUserData.isEmpty()) {
|
||||||
|
plugin.getLocales().getLocale("error_player_not_found", optionalUser.get())
|
||||||
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
plugin.getLocales().getLocale("teleporting_offline_player", targetUser)
|
|
||||||
.ifPresent(onlineUser::sendMessage);
|
this.teleportToOfflinePosition(user, targetUserData.get(), args);
|
||||||
Teleport.builder(plugin, onlineUser)
|
}
|
||||||
.setTarget(offlinePosition.get())
|
|
||||||
.toTeleport()
|
private void teleportToOfflinePosition(@NotNull OnlineUser user, @NotNull User target, @NotNull String[] args) {
|
||||||
.thenAccept(teleport -> teleport.execute().thenAccept(result -> {
|
final Optional<Position> position = plugin.getDatabase().getOfflinePosition(target);
|
||||||
if (result.successful()) {
|
if (position.isEmpty()) {
|
||||||
plugin.getLocales().getLocale("teleporting_offline_complete", targetUser)
|
plugin.getLocales().getLocale("error_no_offline_position", target.getUsername())
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(user::sendMessage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin.getLocales().getLocale("teleporting_offline_player", target.getUsername())
|
||||||
|
.ifPresent(user::sendMessage);
|
||||||
|
try {
|
||||||
|
Teleport.builder(plugin)
|
||||||
|
.teleporter(user)
|
||||||
|
.target(position.get())
|
||||||
|
.toTeleport().execute();
|
||||||
|
|
||||||
|
plugin.getLocales().getLocale("teleporting_offline_complete", target.getUsername())
|
||||||
|
.ifPresent(user::sendMessage);
|
||||||
|
} catch (TeleportationException e) {
|
||||||
|
e.displayMessage(user, plugin, args);
|
||||||
}
|
}
|
||||||
}));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -59,4 +78,5 @@ public class TpOfflineCommand extends Command implements TabProvider {
|
|||||||
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
|
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
|
||||||
.sorted().collect(Collectors.toList()) : Collections.emptyList();
|
.sorted().collect(Collectors.toList()) : Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -48,6 +48,10 @@ public class HomesManager {
|
|||||||
throw new ValidationException(ValidationException.Type.NOT_ENOUGH_HOME_SLOTS);
|
throw new ValidationException(ValidationException.Type.NOT_ENOUGH_HOME_SLOTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!plugin.validateEconomyCheck(online, EconomyHook.Action.ADDITIONAL_HOME_SLOT)) {
|
||||||
|
throw new ValidationException(ValidationException.Type.NOT_ENOUGH_MONEY);
|
||||||
|
}
|
||||||
|
|
||||||
plugin.performEconomyTransaction(online, EconomyHook.Action.ADDITIONAL_HOME_SLOT);
|
plugin.performEconomyTransaction(online, EconomyHook.Action.ADDITIONAL_HOME_SLOT);
|
||||||
savedOwner.setHomeSlots(savedOwner.getHomeSlots() + 1);
|
savedOwner.setHomeSlots(savedOwner.getHomeSlots() + 1);
|
||||||
plugin.getDatabase().updateUserData(savedOwner);
|
plugin.getDatabase().updateUserData(savedOwner);
|
||||||
|
|||||||
@@ -41,6 +41,10 @@ public class WarpsManager {
|
|||||||
plugin.getDatabase().saveWarp(warp);
|
plugin.getDatabase().saveWarp(warp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void createWarp(@NotNull String name, @NotNull Position position) throws ValidationException {
|
||||||
|
this.createWarp(name, position, plugin.getSettings().doOverwriteExistingHomesWarps());
|
||||||
|
}
|
||||||
|
|
||||||
public void deleteWarp(@NotNull String name) throws ValidationException {
|
public void deleteWarp(@NotNull String name) throws ValidationException {
|
||||||
final Optional<Warp> warp = plugin.getDatabase().getWarp(name);
|
final Optional<Warp> warp = plugin.getDatabase().getWarp(name);
|
||||||
if (warp.isEmpty()) {
|
if (warp.isEmpty()) {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package net.william278.huskhomes.teleport;
|
package net.william278.huskhomes.teleport;
|
||||||
|
|
||||||
|
import net.william278.huskhomes.HuskHomes;
|
||||||
|
import net.william278.huskhomes.user.CommandUser;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class TeleportationException extends IllegalArgumentException {
|
public class TeleportationException extends IllegalArgumentException {
|
||||||
@@ -11,6 +13,26 @@ public class TeleportationException extends IllegalArgumentException {
|
|||||||
public enum Type {
|
public enum Type {
|
||||||
TELEPORTER_NOT_FOUND,
|
TELEPORTER_NOT_FOUND,
|
||||||
TARGET_NOT_FOUND,
|
TARGET_NOT_FOUND,
|
||||||
ALREADY_WARMING_UP, ECONOMY_ACTION_FAILED, WARMUP_ALREADY_MOVING, CANNOT_TELEPORT_TO_SELF
|
ALREADY_WARMING_UP,
|
||||||
|
ECONOMY_ACTION_FAILED,
|
||||||
|
WARMUP_ALREADY_MOVING,
|
||||||
|
CANNOT_TELEPORT_TO_SELF
|
||||||
|
}
|
||||||
|
|
||||||
|
public void displayMessage(@NotNull CommandUser user, @NotNull HuskHomes plugin, @NotNull String[] args) {
|
||||||
|
switch (Type.valueOf(getMessage().split(": ")[1])) {
|
||||||
|
case TELEPORTER_NOT_FOUND, TARGET_NOT_FOUND -> plugin.getLocales()
|
||||||
|
.getLocale("error_player_not_found", args)
|
||||||
|
.ifPresent(user::sendMessage);
|
||||||
|
case ALREADY_WARMING_UP -> plugin.getLocales()
|
||||||
|
.getLocale("error_already_teleporting")
|
||||||
|
.ifPresent(user::sendMessage);
|
||||||
|
case WARMUP_ALREADY_MOVING -> plugin.getLocales()
|
||||||
|
.getLocale("error_teleport_warmup_stand_still")
|
||||||
|
.ifPresent(user::sendMessage);
|
||||||
|
case CANNOT_TELEPORT_TO_SELF -> plugin.getLocales()
|
||||||
|
.getLocale("error_teleport_request_self")
|
||||||
|
.ifPresent(user::sendMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,35 +14,54 @@ public class ValidationException extends IllegalArgumentException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void dispatchHomeError(@NotNull CommandUser viewer, boolean other, @NotNull HuskHomes plugin, @NotNull String... args) {
|
public void dispatchHomeError(@NotNull CommandUser viewer, boolean other, @NotNull HuskHomes plugin, @NotNull String... args) {
|
||||||
plugin.getLocales()
|
switch (error) {
|
||||||
.getLocale(switch (error) {
|
case NOT_FOUND -> plugin.getLocales()
|
||||||
case NOT_FOUND -> other ? "error_home_invalid_other" : "error_home_invalid";
|
.getLocale(other ? "error_home_invalid_other" : "error_home_invalid", args)
|
||||||
case NAME_TAKEN -> "error_home_name_taken";
|
|
||||||
case NAME_INVALID -> "error_home_name_characters";
|
|
||||||
case NOT_ENOUGH_HOME_SLOTS, REACHED_MAX_HOMES -> "error_set_home_maximum_homes";
|
|
||||||
case REACHED_MAX_PUBLIC_HOMES -> "error_edit_home_maximum_public_homes";
|
|
||||||
case DESCRIPTION_INVALID -> "error_home_description_characters";
|
|
||||||
}, args)
|
|
||||||
.ifPresent(viewer::sendMessage);
|
.ifPresent(viewer::sendMessage);
|
||||||
|
case NAME_TAKEN -> plugin.getLocales()
|
||||||
|
.getLocale("error_home_name_taken", args)
|
||||||
|
.ifPresent(viewer::sendMessage);
|
||||||
|
case NAME_INVALID -> plugin.getLocales()
|
||||||
|
.getLocale("error_home_name_characters", args)
|
||||||
|
.ifPresent(viewer::sendMessage);
|
||||||
|
case NOT_ENOUGH_HOME_SLOTS, REACHED_MAX_HOMES -> plugin.getLocales()
|
||||||
|
.getLocale("error_set_home_maximum_homes", args)
|
||||||
|
.ifPresent(viewer::sendMessage);
|
||||||
|
case REACHED_MAX_PUBLIC_HOMES -> plugin.getLocales()
|
||||||
|
.getLocale("error_edit_home_maximum_public_homes", args)
|
||||||
|
.ifPresent(viewer::sendMessage);
|
||||||
|
case DESCRIPTION_INVALID -> plugin.getLocales()
|
||||||
|
.getLocale("error_home_description_characters", args)
|
||||||
|
.ifPresent(viewer::sendMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dispatchWarpError(@NotNull CommandUser viewer, @NotNull HuskHomes plugin, @NotNull String... args) {
|
public void dispatchWarpError(@NotNull CommandUser viewer, @NotNull HuskHomes plugin, @NotNull String... args) {
|
||||||
plugin.getLocales()
|
switch (error) {
|
||||||
.getLocale(switch (error) {
|
case NOT_FOUND -> plugin.getLocales()
|
||||||
case NOT_FOUND -> "error_warp_invalid";
|
.getLocale("error_warp_invalid", args)
|
||||||
case NAME_TAKEN -> "error_warp_name_taken";
|
|
||||||
case NAME_INVALID -> "error_warp_name_characters";
|
|
||||||
case DESCRIPTION_INVALID -> "error_warp_description_characters";
|
|
||||||
default -> throw new IllegalStateException("Unexpected value: " + error);
|
|
||||||
}, args)
|
|
||||||
.ifPresent(viewer::sendMessage);
|
.ifPresent(viewer::sendMessage);
|
||||||
|
case NAME_TAKEN -> plugin.getLocales()
|
||||||
|
.getLocale("error_warp_name_taken", args)
|
||||||
|
.ifPresent(viewer::sendMessage);
|
||||||
|
case NAME_INVALID -> plugin.getLocales()
|
||||||
|
.getLocale("error_warp_name_characters", args)
|
||||||
|
.ifPresent(viewer::sendMessage);
|
||||||
|
case DESCRIPTION_INVALID -> plugin.getLocales()
|
||||||
|
.getLocale("error_warp_description_characters", args)
|
||||||
|
.ifPresent(viewer::sendMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Type {
|
public enum Type {
|
||||||
NOT_FOUND,
|
NOT_FOUND,
|
||||||
NAME_TAKEN,
|
NAME_TAKEN,
|
||||||
NAME_INVALID,
|
NAME_INVALID,
|
||||||
REACHED_MAX_HOMES, NOT_ENOUGH_HOME_SLOTS, REACHED_MAX_PUBLIC_HOMES, DESCRIPTION_INVALID
|
REACHED_MAX_HOMES,
|
||||||
|
NOT_ENOUGH_HOME_SLOTS,
|
||||||
|
REACHED_MAX_PUBLIC_HOMES,
|
||||||
|
NOT_ENOUGH_MONEY,
|
||||||
|
DESCRIPTION_INVALID
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
# MySQL migration script.
|
|
||||||
|
|
||||||
# Clear target database.
|
|
||||||
DELETE
|
|
||||||
FROM `%target_positions_table%`;
|
|
||||||
DELETE
|
|
||||||
FROM `%target_users_table%`;
|
|
||||||
|
|
||||||
# Migrate positions to the new schema
|
|
||||||
INSERT INTO `%target_positions_table%` (`id`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`)
|
|
||||||
SELECT `location_id` AS `id`,
|
|
||||||
`x`,
|
|
||||||
`y`,
|
|
||||||
`z`,
|
|
||||||
`yaw`,
|
|
||||||
`pitch`,
|
|
||||||
`world` AS `world_name`,
|
|
||||||
'00000000-0000-0000-0000-000000000000' AS `world_uuid`,
|
|
||||||
`server` AS `server_name`
|
|
||||||
FROM `%source_database%`.`%source_positions_table%`;
|
|
||||||
|
|
||||||
# Migrate users to the new schema
|
|
||||||
INSERT INTO `%target_users_table%` (`uuid`, `username`, `last_position`, `offline_position`, `home_slots`,
|
|
||||||
`ignoring_requests`)
|
|
||||||
SELECT `user_uuid` AS `uuid`,
|
|
||||||
`username`,
|
|
||||||
`last_location_id` AS `last_position`,
|
|
||||||
`offline_location_id` AS `offline_position`,
|
|
||||||
`home_slots`,
|
|
||||||
`is_ignoring_requests` AS `ignoring_requests`
|
|
||||||
FROM `%source_database%`.`%source_users_table%`;
|
|
||||||
|
|
||||||
# Migrate homes to the new schema
|
|
||||||
INSERT INTO `%target_saved_positions%` (`position_id`, `name`, `description`, `timestamp`)
|
|
||||||
SELECT `location_id` AS `position_id`,
|
|
||||||
`name`,
|
|
||||||
`description`,
|
|
||||||
`creation_time` AS `timestamp`
|
|
||||||
FROM `%source_database%`.`%source_homes_table%`;
|
|
||||||
|
|
||||||
INSERT INTO `%target_homes_table%` (`uuid`, `saved_position_id`, `owner_uuid`, `public`)
|
|
||||||
SELECT UUID() AS `uuid`,
|
|
||||||
`id` AS `saved_position_id`,
|
|
||||||
`user_uuid` AS `owner_uuid`,
|
|
||||||
`public`
|
|
||||||
FROM `%source_database%`.`%source_homes_table%` AS `%source_homes_table%`
|
|
||||||
INNER JOIN `%target_saved_positions%`
|
|
||||||
ON `%source_homes_table%`.`location_id` = `%target_saved_positions%`.`position_id`
|
|
||||||
INNER JOIN `%source_database%`.`%source_users_table%` AS `%source_users_table%`
|
|
||||||
ON `%source_homes_table%`.`player_id` = `%source_users_table%`.`player_id`;
|
|
||||||
|
|
||||||
# Migrate warps to the new schema
|
|
||||||
INSERT INTO `%target_saved_positions%` (`position_id`, `name`, `description`, `timestamp`)
|
|
||||||
SELECT `location_id` AS `position_id`,
|
|
||||||
`name`,
|
|
||||||
`description`,
|
|
||||||
`creation_time` AS `timestamp`
|
|
||||||
FROM `%source_database%`.`%source_warps_table%`;
|
|
||||||
|
|
||||||
INSERT INTO `%target_warps_table%` (`uuid`, `saved_position_id`)
|
|
||||||
SELECT UUID() AS `uuid`,
|
|
||||||
`id` AS `saved_position_id`
|
|
||||||
FROM `%source_database%`.`%source_warps_table%` AS `%source_warps_table%`
|
|
||||||
INNER JOIN `%target_saved_positions%`
|
|
||||||
ON `%source_warps_table%`.`location_id` = `%target_saved_positions%`.`position_id`;
|
|
||||||
|
|
||||||
# Rename old tables
|
|
||||||
ALTER TABLE `%source_database%`.`%source_positions_table%` RENAME TO `old_%source_positions_table%`;
|
|
||||||
ALTER TABLE `%source_database%`.`%source_users_table%` RENAME TO `old_%source_users_table%`;
|
|
||||||
ALTER TABLE `%source_database%`.`%source_homes_table%` RENAME TO `old_%source_homes_table%`;
|
|
||||||
ALTER TABLE `%source_database%`.`%source_warps_table%` RENAME TO `old_%source_warps_table%`;
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
/* SQLite migration script. */
|
|
||||||
|
|
||||||
-- Clear target database.
|
|
||||||
DELETE
|
|
||||||
FROM `%target_positions_table%`;
|
|
||||||
DELETE
|
|
||||||
FROM `%target_users_table%`;
|
|
||||||
|
|
||||||
-- Migrate positions to the new schema
|
|
||||||
INSERT INTO `%target_positions_table%` (`id`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`)
|
|
||||||
SELECT `location_id` AS `id`,
|
|
||||||
`x`,
|
|
||||||
`y`,
|
|
||||||
`z`,
|
|
||||||
`yaw`,
|
|
||||||
`pitch`,
|
|
||||||
`world` AS `world_name`,
|
|
||||||
'00000000-0000-0000-0000-000000000000' AS `world_uuid`,
|
|
||||||
`server` AS `server_name`
|
|
||||||
FROM `%source_positions_table%`;
|
|
||||||
|
|
||||||
-- Migrate users to the new schema
|
|
||||||
INSERT INTO `%target_users_table%` (`uuid`, `username`, `last_position`, `offline_position`, `home_slots`,
|
|
||||||
`ignoring_requests`)
|
|
||||||
SELECT `user_uuid` AS `uuid`,
|
|
||||||
`username`,
|
|
||||||
`last_location_id` AS `last_position`,
|
|
||||||
`offline_location_id` AS `offline_position`,
|
|
||||||
`home_slots`,
|
|
||||||
`is_ignoring_requests` AS `ignoring_requests`
|
|
||||||
FROM `%source_users_table%`;
|
|
||||||
|
|
||||||
-- Migrate homes to the new schema
|
|
||||||
INSERT INTO `%target_saved_positions%` (`position_id`, `name`, `description`, `timestamp`)
|
|
||||||
SELECT `location_id` AS `position_id`,
|
|
||||||
`name`,
|
|
||||||
`description`,
|
|
||||||
`creation_time` AS `timestamp`
|
|
||||||
FROM `%source_homes_table%`;
|
|
||||||
|
|
||||||
INSERT INTO `%target_homes_table%` (`uuid`, `saved_position_id`, `owner_uuid`, `public`)
|
|
||||||
SELECT lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-' || '4' ||
|
|
||||||
substr(hex(randomblob(2)), 2) || '-' ||
|
|
||||||
substr('AB89', 1 + (abs(random()) % 4), 1) ||
|
|
||||||
substr(hex(randomblob(2)), 2) || '-' ||
|
|
||||||
hex(randomblob(6))) AS `uuid`,
|
|
||||||
`id` AS `saved_position_id`,
|
|
||||||
`user_uuid` AS `owner_uuid`,
|
|
||||||
`public`
|
|
||||||
FROM `%source_homes_table%`
|
|
||||||
INNER JOIN `%target_saved_positions%`
|
|
||||||
ON `%source_homes_table%`.`location_id` = `%target_saved_positions%`.`position_id`
|
|
||||||
INNER JOIN `%source_users_table%`
|
|
||||||
ON `%source_homes_table%`.`player_id` = `%source_users_table%`.`player_id`;
|
|
||||||
|
|
||||||
-- Migrate warps to the new schema
|
|
||||||
INSERT INTO `%target_saved_positions%` (`position_id`, `name`, `description`, `timestamp`)
|
|
||||||
SELECT `location_id` AS `position_id`,
|
|
||||||
`name`,
|
|
||||||
`description`,
|
|
||||||
`creation_time` AS `timestamp`
|
|
||||||
FROM `%source_warps_table%`;
|
|
||||||
|
|
||||||
INSERT INTO `%target_warps_table%` (`uuid`, `saved_position_id`)
|
|
||||||
SELECT lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-' || '4' ||
|
|
||||||
substr(hex(randomblob(2)), 2) || '-' ||
|
|
||||||
substr('AB89', 1 + (abs(random()) % 4), 1) ||
|
|
||||||
substr(hex(randomblob(2)), 2) || '-' ||
|
|
||||||
hex(randomblob(6))) AS `uuid`,
|
|
||||||
`id` AS `saved_position_id`
|
|
||||||
FROM `%source_warps_table%`
|
|
||||||
INNER JOIN `%target_saved_positions%`
|
|
||||||
ON `%source_warps_table%`.`location_id` = `%target_saved_positions%`.`position_id`;
|
|
||||||
|
|
||||||
-- Rename old tables
|
|
||||||
ALTER TABLE `%source_positions_table%`
|
|
||||||
RENAME TO `old_%source_positions_table%`;
|
|
||||||
ALTER TABLE `%source_users_table%`
|
|
||||||
RENAME TO `old_%source_users_table%`;
|
|
||||||
ALTER TABLE `%source_homes_table%`
|
|
||||||
RENAME TO `old_%source_homes_table%`;
|
|
||||||
ALTER TABLE `%source_warps_table%`
|
|
||||||
RENAME TO `old_%source_warps_table%`;
|
|
||||||
Reference in new issue
Block a user