mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 22:16:08 +00:00
Add /edithome, use instant for timestamps, new locales
This commit is contained in:
1 parent
eddae1051c
commit
a91128ca3d
24 files changed
+793
-205
No files matched your search
@@ -13,7 +13,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* A cache used to hold persistently used data
|
||||
* A cache used to hold frequently accessed data (i.e. TAB-completed homes and warps)
|
||||
*/
|
||||
public class Cache {
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import net.william278.huskhomes.util.RegexUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -21,7 +22,7 @@ public class DelHomeCommand extends CommandBase implements TabCompletable, Conso
|
||||
switch (args.length) {
|
||||
case 0 -> plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> {
|
||||
if (homes.size() == 1) {
|
||||
homes.stream().findFirst().ifPresent(home -> plugin.getSavedPositionManager().deleteHome(onlineUser, home.meta.name));
|
||||
homes.stream().findFirst().ifPresent(home -> deletePlayerHome(onlineUser, onlineUser, home.meta.name));
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", "/delhome <name>")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
@@ -39,24 +40,49 @@ public class DelHomeCommand extends CommandBase implements TabCompletable, Conso
|
||||
return;
|
||||
}
|
||||
}
|
||||
plugin.getSavedPositionManager().deleteHome(userData.user(), homeIdentifier.homeName());
|
||||
deletePlayerHome(onlineUser, userData.user(), homeName);
|
||||
},
|
||||
() -> plugin.getLocales().getLocale("error_home_invalid_other",
|
||||
homeIdentifier.ownerName(), homeIdentifier.homeName()).ifPresent(onlineUser::sendMessage))),
|
||||
() -> plugin.getSavedPositionManager().deleteHome(onlineUser, homeName));
|
||||
() -> deletePlayerHome(onlineUser, onlineUser, homeName));
|
||||
}
|
||||
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/delhome <name>")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a player's home
|
||||
*
|
||||
* @param deleter the player who is deleting the home
|
||||
* @param homeOwner The player who owns the home
|
||||
* @param homeName The home name to delete
|
||||
*/
|
||||
private void deletePlayerHome(@NotNull OnlineUser deleter, @NotNull User homeOwner, @NotNull String homeName) {
|
||||
plugin.getSavedPositionManager().deleteHome(homeOwner, homeName).thenAccept(deleted -> {
|
||||
if (deleter.uuid.equals(homeOwner.uuid)) {
|
||||
if (deleted) {
|
||||
plugin.getLocales().getLocale("home_deleted", homeName).ifPresent(deleter::sendMessage);
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_home_invalid_other", homeName).ifPresent(deleter::sendMessage);
|
||||
}
|
||||
} else {
|
||||
if (deleted) {
|
||||
plugin.getLocales().getLocale("home_deleted_other", homeOwner.username, homeName).ifPresent(deleter::sendMessage);
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_home_invalid_other", homeOwner.username, homeName).ifPresent(deleter::sendMessage);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
return plugin.getCache().homes.get(onlineUser.uuid).stream()
|
||||
.filter(s -> s.startsWith(args.length >= 1 ? args[0] : ""))
|
||||
.sorted().collect(Collectors.toList());
|
||||
|
||||
@@ -17,7 +17,14 @@ public class DelWarpCommand extends CommandBase implements TabCompletable, Conso
|
||||
@Override
|
||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
if (args.length == 1) {
|
||||
plugin.getSavedPositionManager().deleteWarp(args[0]);
|
||||
final String warpName = args[0];
|
||||
plugin.getSavedPositionManager().deleteWarp(warpName).thenAccept(deleted -> {
|
||||
if (deleted) {
|
||||
plugin.getLocales().getLocale("warp_deleted").ifPresent(onlineUser::sendMessage);
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_warp_invalid", warpName).ifPresent(onlineUser::sendMessage);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", "/delwarp <name>")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
@@ -26,11 +33,11 @@ public class DelWarpCommand extends CommandBase implements TabCompletable, Conso
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
|
||||
//todo
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
return plugin.getCache().warps.stream()
|
||||
.filter(s -> s.startsWith(args.length >= 1 ? args[0] : ""))
|
||||
.sorted().collect(Collectors.toList());
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import de.themoep.minedown.MineDown;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.position.PositionMeta;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import net.william278.huskhomes.util.RegexUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class EditHomeCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
|
||||
|
||||
private final String[] EDIT_HOME_COMPLETIONS = {"rename", "description", "relocate", "privacy"};
|
||||
|
||||
public EditHomeCommand(@NotNull HuskHomes implementor) {
|
||||
super("edithome", Permission.COMMAND_EDIT_HOME, implementor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
if (args.length >= 1) {
|
||||
final String homeName = args[0];
|
||||
final String editOperation = args.length >= 2 ? args[1] : null;
|
||||
final String editArgs = getEditArguments(args);
|
||||
|
||||
RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse(
|
||||
homeIdentifier -> plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName())
|
||||
.thenAccept(optionalUserData -> optionalUserData.ifPresentOrElse(userData -> {
|
||||
if (!userData.getUserUuid().equals(onlineUser.uuid)) {
|
||||
if (!onlineUser.hasPermission(Permission.COMMAND_EDIT_HOME_OTHER.node)) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
}
|
||||
plugin.getDatabase().getHome(userData.user(), homeIdentifier.homeName()).thenAccept(optionalHome -> {
|
||||
if (optionalHome.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_home_invalid_other",
|
||||
homeIdentifier.ownerName(), homeIdentifier.homeName())
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
editHome(optionalHome.get(), onlineUser, editOperation, editArgs);
|
||||
});
|
||||
|
||||
},
|
||||
() -> plugin.getLocales().getLocale("error_home_invalid_other",
|
||||
homeIdentifier.ownerName(), homeIdentifier.homeName())
|
||||
.ifPresent(onlineUser::sendMessage))),
|
||||
() -> plugin.getDatabase().getHome(onlineUser, homeName).thenAccept(optionalHome -> {
|
||||
if (optionalHome.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_home_invalid", homeName)
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
editHome(optionalHome.get(), onlineUser, editOperation, editArgs);
|
||||
})
|
||||
);
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax",
|
||||
"/edithome <name> [" + String.join("|", EDIT_HOME_COMPLETIONS) + "] [args]")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the specified EditOperation on the specified home
|
||||
*
|
||||
* @param home The home to edit
|
||||
* @param editor The player who is editing the home
|
||||
* @param editOperation The edit operation to perform
|
||||
* @param editArgs Arguments for the edit operation
|
||||
*/
|
||||
private void editHome(@NotNull Home home, @NotNull OnlineUser editor,
|
||||
@Nullable String editOperation, @Nullable String editArgs) {
|
||||
if (editOperation == null) {
|
||||
try {
|
||||
boolean otherOwner = editor.uuid.equals(home.owner.uuid);
|
||||
home.getHomeEditorWindow(plugin.getLocales(), otherOwner, plugin.getSettings().crossServer,
|
||||
!otherOwner || editor.hasPermission(Permission.COMMAND_EDIT_HOME_OTHER.node))
|
||||
.forEach(editor::sendMessage);
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch (editOperation.toLowerCase()) {
|
||||
case "rename" -> {
|
||||
if (editArgs == null || editArgs.contains(Pattern.quote(" "))) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax",
|
||||
"/edithome <name> rename <new name>")
|
||||
.ifPresent(editor::sendMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
final String oldHomeName = home.meta.name;
|
||||
plugin.getSavedPositionManager().updateHomeMeta(home, new PositionMeta(editArgs, home.meta.description))
|
||||
.thenAccept(renameResult ->
|
||||
editor.sendMessage(switch (renameResult.resultType()) {
|
||||
case SUCCESS -> {
|
||||
if (home.owner.uuid.equals(editor.uuid)) {
|
||||
yield plugin.getLocales().getLocale("edit_home_update_name",
|
||||
oldHomeName, editArgs).orElse(new MineDown(""));
|
||||
} else {
|
||||
yield plugin.getLocales().getLocale("edit_home_update_name_other",
|
||||
home.owner.username, oldHomeName, editArgs).orElse(new MineDown(""));
|
||||
}
|
||||
}
|
||||
case FAILED_DUPLICATE -> plugin.getLocales().getLocale("error_set_home_name_taken")
|
||||
.orElse(new MineDown(""));
|
||||
case FAILED_NAME_LENGTH ->
|
||||
plugin.getLocales().getLocale("error_set_home_invalid_length")
|
||||
.orElse(new MineDown(""));
|
||||
case FAILED_NAME_CHARACTERS ->
|
||||
plugin.getLocales().getLocale("error_set_home_invalid_characters")
|
||||
.orElse(new MineDown(""));
|
||||
default -> new MineDown("");
|
||||
}));
|
||||
}
|
||||
case "description" -> { //todo not working
|
||||
if (editArgs == null) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax",
|
||||
"/edithome <name> description <new description>")
|
||||
.ifPresent(editor::sendMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
final String oldHomeDescription = home.meta.description;
|
||||
plugin.getSavedPositionManager().updateHomeMeta(home, new PositionMeta(home.meta.name, editArgs))
|
||||
.thenAccept(renameResult ->
|
||||
editor.sendMessage(switch (renameResult.resultType()) {
|
||||
case SUCCESS -> {
|
||||
if (home.owner.uuid.equals(editor.uuid)) {
|
||||
yield plugin.getLocales().getLocale("edit_home_update_name",
|
||||
oldHomeDescription, editArgs).orElse(new MineDown(""));
|
||||
} else {
|
||||
yield plugin.getLocales().getLocale("edit_home_update_name_other",
|
||||
home.owner.username, oldHomeDescription, editArgs).orElse(new MineDown(""));
|
||||
}
|
||||
}
|
||||
case FAILED_DESCRIPTION_LENGTH ->
|
||||
plugin.getLocales().getLocale("error_set_home_invalid_length")
|
||||
.orElse(new MineDown(""));
|
||||
case FAILED_DESCRIPTION_CHARACTERS ->
|
||||
plugin.getLocales().getLocale("error_set_home_invalid_characters")
|
||||
.orElse(new MineDown(""));
|
||||
default -> new MineDown("");
|
||||
}));
|
||||
}
|
||||
case "relocate" -> editor.getPosition().thenAccept(position ->
|
||||
plugin.getSavedPositionManager().updateHomePosition(home, position).thenRun(() -> {
|
||||
if (home.owner.uuid.equals(editor.uuid)) {
|
||||
editor.sendMessage(plugin.getLocales().getLocale("edit_home_update_location",
|
||||
home.meta.name).orElse(new MineDown("")));
|
||||
} else {
|
||||
editor.sendMessage(plugin.getLocales().getLocale("edit_home_update_location_other",
|
||||
home.owner.username, home.meta.name).orElse(new MineDown("")));
|
||||
}
|
||||
}));
|
||||
case "privacy" -> {
|
||||
boolean newIsPublic = !home.isPublic;
|
||||
if (editArgs != null) {
|
||||
if (editArgs.equalsIgnoreCase("private")) {
|
||||
newIsPublic = false;
|
||||
} else if (editArgs.equalsIgnoreCase("public")) {
|
||||
newIsPublic = true;
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax",
|
||||
"/edithome <name> privacy [private|public]")
|
||||
.ifPresent(editor::sendMessage);
|
||||
return;
|
||||
}
|
||||
}
|
||||
final String privacyKeyedString = newIsPublic ? "public" : "private";
|
||||
|
||||
if (newIsPublic == home.isPublic) {
|
||||
plugin.getLocales().getLocale(
|
||||
"error_edit_home_privacy_already_" + privacyKeyedString)
|
||||
.ifPresent(editor::sendMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
//todo economy check here
|
||||
|
||||
plugin.getSavedPositionManager().updateHomePrivacy(home, newIsPublic).thenRun(() -> {
|
||||
if (home.owner.uuid.equals(editor.uuid)) {
|
||||
editor.sendMessage(plugin.getLocales().getLocale(
|
||||
"edit_home_privacy_" + privacyKeyedString + "_success",
|
||||
home.meta.name).orElse(new MineDown("")));
|
||||
} else {
|
||||
editor.sendMessage(plugin.getLocales().getLocale(
|
||||
"edit_home_privacy_" + privacyKeyedString + "_success_other",
|
||||
home.owner.username, home.meta.name).orElse(new MineDown("")));
|
||||
}
|
||||
});
|
||||
}
|
||||
default -> plugin.getLocales().getLocale("error_invalid_syntax",
|
||||
"/edithome <name> [" + String.join("|", EDIT_HOME_COMPLETIONS) + "] [args]")
|
||||
.ifPresent(editor::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String getEditArguments(@NotNull String[] args) {
|
||||
if (args.length > 2) {
|
||||
final StringJoiner joiner = new StringJoiner(" ");
|
||||
for (int i = 2; i < args.length; i++) {
|
||||
joiner.add(args[i]);
|
||||
}
|
||||
return joiner.toString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
//todo
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
return plugin.getCache().homes.get(onlineUser.uuid).stream()
|
||||
.filter(s -> s.toLowerCase().startsWith(args.length >= 1 ? args[0].toLowerCase() : ""))
|
||||
.sorted().collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -18,19 +18,23 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
|
||||
|
||||
@Override
|
||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
|
||||
switch (args.length) {
|
||||
case 0 -> plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> {
|
||||
// Send the home list if they have homes set. If they have just one home set, teleport the player
|
||||
switch (homes.size()) {
|
||||
case 0 -> plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
|
||||
case 1 -> plugin.getTeleportManager().teleport(onlineUser, homes.get(0)).thenAccept(result ->
|
||||
plugin.getTeleportManager().finishTeleport(onlineUser, result));
|
||||
default -> {
|
||||
final PrivateHomeList homeList = new PrivateHomeList(homes, onlineUser, plugin);
|
||||
plugin.getCache().positionLists.put(onlineUser.uuid, homeList);
|
||||
homeList.getDisplay(1).forEach(onlineUser::sendMessage);
|
||||
try {
|
||||
switch (homes.size()) {
|
||||
case 0 ->
|
||||
plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
|
||||
case 1 -> plugin.getTeleportManager().teleport(onlineUser, homes.get(0)).thenAccept(result ->
|
||||
plugin.getTeleportManager().finishTeleport(onlineUser, result)).join();
|
||||
default -> {
|
||||
final PrivateHomeList homeList = new PrivateHomeList(homes, onlineUser, plugin);
|
||||
plugin.getCache().positionLists.put(onlineUser.uuid, homeList);
|
||||
homeList.getDisplay(1).forEach(onlineUser::sendMessage);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
case 1 -> {
|
||||
@@ -55,7 +59,7 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
return plugin.getCache().homes.get(onlineUser.uuid).stream()
|
||||
.filter(s -> s.toLowerCase().startsWith(args.length >= 1 ? args[0].toLowerCase() : ""))
|
||||
.sorted().collect(Collectors.toList());
|
||||
|
||||
@@ -65,7 +65,7 @@ public class PublicHomeCommand extends CommandBase implements TabCompletable, Co
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
// Return every public home name as username.home_name from the cache
|
||||
final List<String> publicHomes = new ArrayList<>();
|
||||
plugin.getCache().publicHomes.forEach((ownerName, homeNames) ->
|
||||
|
||||
@@ -8,6 +8,8 @@ import net.william278.huskhomes.position.PositionMeta;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class SetHomeCommand extends CommandBase {
|
||||
|
||||
public SetHomeCommand(@NotNull HuskHomes implementor) {
|
||||
@@ -35,20 +37,16 @@ public class SetHomeCommand extends CommandBase {
|
||||
onlineUser.getPosition().thenAccept(position -> plugin.getSavedPositionManager().setHome(new PositionMeta(homeName,
|
||||
plugin.getLocales().getRawLocale("home_default_description", user.username).orElse("")),
|
||||
user, position).thenAccept(setResult ->
|
||||
onlineUser.sendMessage(switch (setResult.resultType()) {
|
||||
(switch (setResult.resultType()) {
|
||||
case SUCCESS -> {
|
||||
assert setResult.setPosition() != null;
|
||||
yield plugin.getLocales().getLocale("set_home_success", setResult.setPosition().meta.name)
|
||||
.orElse(new MineDown(""));
|
||||
assert setResult.savedPosition().isPresent();
|
||||
yield plugin.getLocales().getLocale("set_home_success", setResult.savedPosition().get().meta.name);
|
||||
}
|
||||
case FAILED_DUPLICATE -> plugin.getLocales().getLocale("error_set_home_name_taken")
|
||||
.orElse(new MineDown(""));
|
||||
case FAILED_NAME_LENGTH -> plugin.getLocales().getLocale("error_set_home_invalid_length")
|
||||
.orElse(new MineDown(""));
|
||||
case FAILED_NAME_CHARACTERS -> plugin.getLocales().getLocale("error_set_home_invalid_characters")
|
||||
.orElse(new MineDown(""));
|
||||
default -> new MineDown("");
|
||||
})));
|
||||
case FAILED_DUPLICATE -> plugin.getLocales().getLocale("error_set_home_name_taken");
|
||||
case FAILED_NAME_LENGTH -> plugin.getLocales().getLocale("error_set_home_invalid_length");
|
||||
case FAILED_NAME_CHARACTERS -> plugin.getLocales().getLocale("error_set_home_invalid_characters");
|
||||
default -> Optional.of(new MineDown(""));
|
||||
}).ifPresent(onlineUser::sendMessage)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,8 +29,8 @@ public class SetWarpCommand extends CommandBase {
|
||||
position).thenAccept(setResult ->
|
||||
onlineUser.sendMessage(switch (setResult.resultType()) {
|
||||
case SUCCESS -> {
|
||||
assert setResult.setPosition() != null;
|
||||
yield plugin.getLocales().getLocale("set_warp_success", setResult.setPosition().meta.name)
|
||||
assert setResult.savedPosition().isPresent();
|
||||
yield plugin.getLocales().getLocale("set_warp_success", setResult.savedPosition().get().meta.name)
|
||||
.orElse(new MineDown(""));
|
||||
}
|
||||
case FAILED_DUPLICATE -> plugin.getLocales().getLocale("error_set_warp_name_taken")
|
||||
|
||||
@@ -14,9 +14,10 @@ public interface TabCompletable {
|
||||
* What should be returned when the player attempts to TAB-complete the command
|
||||
*
|
||||
* @param onlineUser {@link OnlineUser} doing the TAB completion
|
||||
* @param args Current command arguments
|
||||
* @param args Current command arguments
|
||||
* @return List of String arguments to offer TAB suggestions
|
||||
*/
|
||||
@NotNull
|
||||
List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args);
|
||||
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
return plugin.getCache().warps.stream()
|
||||
.filter(s -> s.toLowerCase().startsWith(args.length >= 1 ? args[0].toLowerCase() : ""))
|
||||
.sorted().collect(Collectors.toList());
|
||||
|
||||
@@ -3,13 +3,15 @@ package net.william278.huskhomes.config;
|
||||
import de.themoep.minedown.MineDown;
|
||||
import net.william278.annotaml.RootedMap;
|
||||
import net.william278.annotaml.YamlFile;
|
||||
import org.apache.commons.text.StringEscapeUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Loaded locales used by the plugin to display various locales
|
||||
* Loaded locales used by the plugin to display styled messages
|
||||
*/
|
||||
@YamlFile(header = """
|
||||
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
|
||||
@@ -25,26 +27,23 @@ public class Locales {
|
||||
public Map<String, String> rawLocales = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Returns an unformatted locale loaded from the locales file
|
||||
* Returns a raw, un-formatted locale loaded from the locales file
|
||||
*
|
||||
* @param localeId String identifier of the locale, corresponding to a key in the file
|
||||
* @return An {@link Optional} containing the locale corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<String> getRawLocale(String localeId) {
|
||||
if (rawLocales.containsKey(localeId)) {
|
||||
return Optional.of(rawLocales.get(localeId));
|
||||
}
|
||||
return Optional.empty();
|
||||
public Optional<String> getRawLocale(@NotNull String localeId) {
|
||||
return Optional.ofNullable(rawLocales.get(localeId)).map(StringEscapeUtils::unescapeJava);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an unformatted locale loaded from the locales file, with replacements applied
|
||||
* Returnsa raw, un-formatted locale loaded from the locales file, with replacements applied
|
||||
*
|
||||
* @param localeId String identifier of the locale, corresponding to a key in the file
|
||||
* @param localeId String identifier of the locale, corresponding to a key in the file
|
||||
* @param replacements Ordered array of replacement strings to fill in placeholders with
|
||||
* @return An {@link Optional} containing the replacement-applied locale corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<String> getRawLocale(String localeId, String... replacements) {
|
||||
public Optional<String> getRawLocale(@NotNull String localeId, @NotNull String... replacements) {
|
||||
return getRawLocale(localeId).map(locale -> applyReplacements(locale, replacements));
|
||||
}
|
||||
|
||||
@@ -54,34 +53,35 @@ public class Locales {
|
||||
* @param localeId String identifier of the locale, corresponding to a key in the file
|
||||
* @return An {@link Optional} containing the formatted locale corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<MineDown> getLocale(String localeId) {
|
||||
public Optional<MineDown> getLocale(@NotNull String localeId) {
|
||||
return getRawLocale(localeId).map(MineDown::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a MineDown-formatted locale from the locales file, with replacements applied
|
||||
*
|
||||
* @param localeId String identifier of the locale, corresponding to a key in the file
|
||||
* @param localeId String identifier of the locale, corresponding to a key in the file
|
||||
* @param replacements Ordered array of replacement strings to fill in placeholders with
|
||||
* @return An {@link Optional} containing the replacement-applied, formatted locale corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<MineDown> getLocale(String localeId, String... replacements) {
|
||||
public Optional<MineDown> getLocale(@NotNull String localeId, @NotNull String... replacements) {
|
||||
return getRawLocale(localeId, replacements).map(MineDown::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply placeholder replacements to a raw locale
|
||||
*
|
||||
* @param rawLocale The raw, unparsed locale
|
||||
* @param rawLocale The raw, unparsed locale
|
||||
* @param replacements Ordered array of replacement strings to fill in placeholders with
|
||||
* @return the raw locale, with inserted placeholders
|
||||
*/
|
||||
private String applyReplacements(String rawLocale, String... replacements) {
|
||||
@NotNull
|
||||
private String applyReplacements(@NotNull String rawLocale, @NotNull String... replacements) {
|
||||
int replacementIndexer = 1;
|
||||
for (String replacement : replacements) {
|
||||
String replacementString = "%" + replacementIndexer + "%";
|
||||
rawLocale = rawLocale.replace(replacementString, replacement);
|
||||
replacementIndexer = replacementIndexer + 1;
|
||||
replacementIndexer += 1;
|
||||
}
|
||||
return rawLocale;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.*;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -161,7 +160,7 @@ public class MySqlDatabase extends Database {
|
||||
statement.setInt(1, setPosition(position, connection));
|
||||
statement.setString(2, position.meta.name);
|
||||
statement.setString(3, position.meta.description);
|
||||
statement.setTimestamp(4, new Timestamp(Instant.ofEpochSecond(position.meta.timestamp).toEpochMilli()));
|
||||
statement.setTimestamp(4, Timestamp.from(position.meta.creationTime));
|
||||
statement.executeUpdate();
|
||||
|
||||
final ResultSet resultSet = statement.getGeneratedKeys();
|
||||
@@ -255,7 +254,7 @@ public class MySqlDatabase extends Database {
|
||||
resultSet.getString("username")),
|
||||
resultSet.getInt("home_slots"),
|
||||
resultSet.getBoolean("ignoring_requests"),
|
||||
resultSet.getTimestamp("rtp_cooldown").toInstant().getEpochSecond()));
|
||||
resultSet.getTimestamp("rtp_cooldown").toInstant()));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
@@ -283,7 +282,7 @@ public class MySqlDatabase extends Database {
|
||||
resultSet.getString("username")),
|
||||
resultSet.getInt("home_slots"),
|
||||
resultSet.getBoolean("ignoring_requests"),
|
||||
resultSet.getTimestamp("rtp_cooldown").toInstant().getEpochSecond()));
|
||||
resultSet.getTimestamp("rtp_cooldown").toInstant()));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
@@ -321,7 +320,7 @@ public class MySqlDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
user,
|
||||
resultSet.getBoolean("public")));
|
||||
@@ -358,7 +357,7 @@ public class MySqlDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("warp_uuid"))));
|
||||
}
|
||||
}
|
||||
@@ -395,7 +394,7 @@ public class MySqlDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
new User(UUID.fromString(resultSet.getString("owner_uuid")),
|
||||
resultSet.getString("owner_username")),
|
||||
@@ -436,7 +435,7 @@ public class MySqlDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
new User(UUID.fromString(resultSet.getString("owner_uuid")),
|
||||
resultSet.getString("owner_username")),
|
||||
@@ -445,6 +444,8 @@ public class MySqlDatabase extends Database {
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
getLogger().log(Level.SEVERE, "Failed to query a player's home", e);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Optional.empty();
|
||||
});
|
||||
@@ -475,7 +476,7 @@ public class MySqlDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
new User(UUID.fromString(resultSet.getString("owner_uuid")),
|
||||
resultSet.getString("owner_username")),
|
||||
@@ -513,7 +514,7 @@ public class MySqlDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("warp_uuid"))));
|
||||
}
|
||||
}
|
||||
@@ -548,7 +549,7 @@ public class MySqlDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("warp_uuid"))));
|
||||
}
|
||||
}
|
||||
@@ -601,7 +602,7 @@ public class MySqlDatabase extends Database {
|
||||
|
||||
statement.setInt(1, userData.homeSlots());
|
||||
statement.setBoolean(2, userData.ignoringTeleports());
|
||||
statement.setLong(3, userData.rtpCooldown());
|
||||
statement.setTimestamp(3, Timestamp.from(userData.rtpCooldown()));
|
||||
statement.setString(4, userData.getUserUuid().toString());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
@@ -856,6 +857,7 @@ public class MySqlDatabase extends Database {
|
||||
return CompletableFuture.runAsync(() -> getHome(home.uuid)
|
||||
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
|
||||
try (Connection connection = getConnection()) {
|
||||
// Update the home's saved position, including metadata
|
||||
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
||||
SELECT `saved_position_id` FROM `%homes_table%`
|
||||
WHERE `uuid`=?;"""))) {
|
||||
@@ -866,6 +868,16 @@ public class MySqlDatabase extends Database {
|
||||
updateSavedPosition(resultSet.getInt("saved_position_id"), home, connection);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the home privacy
|
||||
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
||||
UPDATE `%homes_table%`
|
||||
SET `public`=?
|
||||
WHERE `uuid`=?;"""))) {
|
||||
statement.setBoolean(1, home.isPublic);
|
||||
statement.setString(2, home.uuid.toString());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
getLogger().log(Level.SEVERE,
|
||||
"Failed to update a home in the database for " + home.owner.username, e);
|
||||
|
||||
@@ -14,7 +14,6 @@ import org.sqlite.SQLiteConfig;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.*;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -173,7 +172,7 @@ public class SqLiteDatabase extends Database {
|
||||
statement.setInt(1, setPosition(position, connection));
|
||||
statement.setString(2, position.meta.name);
|
||||
statement.setString(3, position.meta.description);
|
||||
statement.setTimestamp(4, new Timestamp(Instant.ofEpochSecond(position.meta.timestamp).toEpochMilli()));
|
||||
statement.setTimestamp(4, Timestamp.from(position.meta.creationTime));
|
||||
statement.executeUpdate();
|
||||
|
||||
// Return the ID of the newly inserted row
|
||||
@@ -269,7 +268,7 @@ public class SqLiteDatabase extends Database {
|
||||
resultSet.getString("username")),
|
||||
resultSet.getInt("home_slots"),
|
||||
resultSet.getBoolean("ignoring_requests"),
|
||||
resultSet.getTimestamp("rtp_cooldown").toInstant().getEpochSecond()));
|
||||
resultSet.getTimestamp("rtp_cooldown").toInstant()));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
@@ -297,7 +296,7 @@ public class SqLiteDatabase extends Database {
|
||||
resultSet.getString("username")),
|
||||
resultSet.getInt("home_slots"),
|
||||
resultSet.getBoolean("ignoring_requests"),
|
||||
resultSet.getTimestamp("rtp_cooldown").toInstant().getEpochSecond()));
|
||||
resultSet.getTimestamp("rtp_cooldown").toInstant()));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
@@ -335,7 +334,7 @@ public class SqLiteDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
user,
|
||||
resultSet.getBoolean("public")));
|
||||
@@ -372,7 +371,7 @@ public class SqLiteDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("warp_uuid"))));
|
||||
}
|
||||
}
|
||||
@@ -409,7 +408,7 @@ public class SqLiteDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
new User(UUID.fromString(resultSet.getString("owner_uuid")),
|
||||
resultSet.getString("owner_username")),
|
||||
@@ -450,7 +449,7 @@ public class SqLiteDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
new User(UUID.fromString(resultSet.getString("owner_uuid")),
|
||||
resultSet.getString("owner_username")),
|
||||
@@ -489,7 +488,7 @@ public class SqLiteDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
new User(UUID.fromString(resultSet.getString("owner_uuid")),
|
||||
resultSet.getString("owner_username")),
|
||||
@@ -527,7 +526,7 @@ public class SqLiteDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("warp_uuid"))));
|
||||
}
|
||||
}
|
||||
@@ -562,7 +561,7 @@ public class SqLiteDatabase extends Database {
|
||||
new Server(resultSet.getString("server_name")),
|
||||
new PositionMeta(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant().getEpochSecond()),
|
||||
resultSet.getTimestamp("timestamp").toInstant()),
|
||||
UUID.fromString(resultSet.getString("warp_uuid"))));
|
||||
}
|
||||
}
|
||||
@@ -615,7 +614,7 @@ public class SqLiteDatabase extends Database {
|
||||
|
||||
statement.setInt(1, userData.homeSlots());
|
||||
statement.setBoolean(2, userData.ignoringTeleports());
|
||||
statement.setLong(3, userData.rtpCooldown());
|
||||
statement.setTimestamp(3, Timestamp.from(userData.rtpCooldown()));
|
||||
statement.setString(4, userData.getUserUuid().toString());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
@@ -2,12 +2,13 @@ package net.william278.huskhomes.player;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Represents data about a player on the server
|
||||
*/
|
||||
public record UserData(@NotNull User user, int homeSlots, boolean ignoringTeleports, long rtpCooldown) {
|
||||
public record UserData(@NotNull User user, int homeSlots, boolean ignoringTeleports, @NotNull Instant rtpCooldown) {
|
||||
|
||||
@NotNull
|
||||
public UUID getUserUuid() {
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
package net.william278.huskhomes.position;
|
||||
|
||||
import de.themoep.minedown.MineDown;
|
||||
import net.william278.huskhomes.config.Locales;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Represents a home set by a {@link User}
|
||||
@@ -30,9 +37,63 @@ public class Home extends SavedPosition {
|
||||
this.isPublic = isPublic;
|
||||
}
|
||||
|
||||
public Home(@NotNull Position position, @NotNull PositionMeta meta, @NotNull User owner) {
|
||||
public Home(@NotNull Position position, @NotNull PositionMeta meta, @NotNull User owner) {
|
||||
super(position, meta);
|
||||
this.owner = owner;
|
||||
this.isPublic = false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<MineDown> getHomeEditorWindow(@NotNull Locales locales, final boolean otherViewer,
|
||||
final boolean showServerInformation, final boolean showButtons) {
|
||||
return new ArrayList<>() {{
|
||||
if (!otherViewer) {
|
||||
locales.getLocale("edit_home_menu_title", meta.name)
|
||||
.ifPresent(this::add);
|
||||
} else {
|
||||
locales.getLocale("edit_home_menu_title_other", owner.username, meta.name)
|
||||
.ifPresent(this::add);
|
||||
}
|
||||
|
||||
locales.getLocale("edit_home_menu_metadata",
|
||||
DateTimeFormatter.ofPattern("MMM dd yyyy, HH:mm")
|
||||
.format(meta.creationTime.atZone(ZoneId.systemDefault())),
|
||||
uuid.toString().split(Pattern.quote("-"))[0],
|
||||
uuid.toString())
|
||||
.ifPresent(this::add);
|
||||
|
||||
if (!isPublic) {
|
||||
locales.getLocale("edit_home_menu_privacy_private").ifPresent(this::add);
|
||||
} else {
|
||||
locales.getLocale("edit_home_menu_privacy_public").ifPresent(this::add);
|
||||
}
|
||||
|
||||
if (meta.description.length() > 0) {
|
||||
locales.getLocale("edit_home_menu_description", MineDown.escape(meta.description))
|
||||
.ifPresent(this::add);
|
||||
}
|
||||
|
||||
if (!showServerInformation) {
|
||||
locales.getLocale("edit_home_menu_world", world.name).ifPresent(this::add);
|
||||
} else {
|
||||
locales.getLocale("edit_home_menu_world_server", world.name, server.name).ifPresent(this::add);
|
||||
}
|
||||
|
||||
locales.getLocale("edit_home_menu_coordinates",
|
||||
String.format("%.1f", x), String.format("%.1f", y), String.format("%.1f", z),
|
||||
String.format("%.2f", yaw), String.format("%.2f", pitch))
|
||||
.ifPresent(this::add);
|
||||
|
||||
if (showButtons) {
|
||||
final String formattedName = owner.username + "." + meta.name;
|
||||
locales.getLocale("edit_home_menu_manage_buttons",
|
||||
formattedName)
|
||||
.ifPresent(this::add);
|
||||
locales.getLocale("edit_home_menu_meta_edit_buttons",
|
||||
formattedName)
|
||||
.ifPresent(this::add);
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,20 +22,21 @@ public class PositionMeta {
|
||||
public String description;
|
||||
|
||||
/**
|
||||
* The epoch timestamp of a position's creation
|
||||
* The time the position was created
|
||||
*/
|
||||
public long timestamp;
|
||||
@NotNull
|
||||
public Instant creationTime;
|
||||
|
||||
public PositionMeta(@NotNull String name, @NotNull String description, long timestamp) {
|
||||
public PositionMeta(@NotNull String name, @NotNull String description, @NotNull Instant creationTime) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.timestamp = timestamp;
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
|
||||
public PositionMeta(@NotNull String name, @NotNull String description) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.timestamp = Instant.now().getEpochSecond();
|
||||
this.creationTime = Instant.now();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,7 +13,7 @@ public abstract class SavedPosition extends Position {
|
||||
* Metadata about this position (name, description)
|
||||
*/
|
||||
@NotNull
|
||||
public final PositionMeta meta;
|
||||
public PositionMeta meta;
|
||||
|
||||
/**
|
||||
* A unique ID representing this position
|
||||
|
||||
@@ -5,7 +5,6 @@ import net.william278.huskhomes.database.Database;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import net.william278.huskhomes.util.RegexUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
@@ -38,23 +37,22 @@ public class SavedPositionManager {
|
||||
* @param homeMeta metadata about the home
|
||||
* @param homeOwner the owner of the home
|
||||
* @param position where the home should be set
|
||||
* @return a future supplying the {@link SetResult}; the result of setting the home
|
||||
* @return a future supplying the {@link SaveResult}; the result of setting the home
|
||||
*/
|
||||
public CompletableFuture<SetResult> setHome(@NotNull PositionMeta homeMeta, @NotNull User homeOwner,
|
||||
@NotNull Position position) {
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
validateMeta(homeMeta).map(resultType -> new SetResult(resultType, null)).orElseGet(() ->
|
||||
database.getHome(homeOwner, homeMeta.name).thenApply(optionalHome -> {
|
||||
if (optionalHome.isEmpty()) {
|
||||
final Home home = new Home(position, homeMeta, homeOwner);
|
||||
cache.homes.putIfAbsent(home.owner.uuid, new ArrayList<>());
|
||||
cache.homes.get(home.owner.uuid).add(home.meta.name);
|
||||
cache.positionLists.clear();
|
||||
return database.setHome(home).thenApply(value ->
|
||||
new SetResult(SetResult.ResultType.SUCCESS, home)).join();
|
||||
}
|
||||
return new SetResult(SetResult.ResultType.FAILED_DUPLICATE, null);
|
||||
}).join()));
|
||||
public CompletableFuture<SaveResult> setHome(@NotNull PositionMeta homeMeta, @NotNull User homeOwner,
|
||||
@NotNull Position position) {
|
||||
return database.getHome(homeOwner, homeMeta.name).thenApply(optionalHome ->
|
||||
validateMeta(homeMeta).map(resultType -> new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
|
||||
if (optionalHome.isEmpty()) {
|
||||
final Home home = new Home(position, homeMeta, homeOwner);
|
||||
cache.homes.putIfAbsent(home.owner.uuid, new ArrayList<>());
|
||||
cache.homes.get(home.owner.uuid).add(home.meta.name);
|
||||
cache.positionLists.clear();
|
||||
return database.setHome(home).thenApply(value ->
|
||||
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
|
||||
}
|
||||
return new SaveResult(SaveResult.ResultType.FAILED_DUPLICATE, Optional.empty());
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,10 +60,11 @@ public class SavedPositionManager {
|
||||
*
|
||||
* @param homeOwner the owner of the home
|
||||
* @param homeName the name of the home
|
||||
* @return a future supplying a boolean; {@code true} if the home was deleted, otherwise {@code false} if the home name was invalid.
|
||||
* @return a future supplying a boolean; {@code true} if the home was deleted, otherwise {@code false}
|
||||
* if the home name was invalid.
|
||||
*/
|
||||
public CompletableFuture<Boolean> deleteHome(@NotNull User homeOwner, @NotNull String homeName) {
|
||||
return CompletableFuture.supplyAsync(() -> database.getHome(homeOwner, homeName).thenApply(optionalHome -> {
|
||||
return database.getHome(homeOwner, homeName).thenApply(optionalHome -> {
|
||||
if (optionalHome.isPresent()) {
|
||||
final Home home = optionalHome.get();
|
||||
return database.deleteHome(home.uuid).thenApply(ignored -> {
|
||||
@@ -78,7 +77,129 @@ public class SavedPositionManager {
|
||||
}).join();
|
||||
}
|
||||
return false;
|
||||
}).join());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to update an existing home with new metadata
|
||||
*
|
||||
* @param homeOwner the owner of the home
|
||||
* @param existingHomeName the current name of the home to update
|
||||
* @param newHomeMeta the new metadata for the home
|
||||
* @return a future supplying the {@link SaveResult}; the result of updating the home
|
||||
*/
|
||||
public CompletableFuture<SaveResult> updateHomeMeta(@NotNull User homeOwner, @NotNull String existingHomeName,
|
||||
@NotNull PositionMeta newHomeMeta) {
|
||||
return database.getHome(homeOwner, existingHomeName).thenApply(optionalHome -> {
|
||||
if (optionalHome.isPresent()) {
|
||||
return updateHomeMeta(optionalHome.get(), newHomeMeta).join();
|
||||
}
|
||||
return new SaveResult(SaveResult.ResultType.FAILED_DOES_NOT_EXIST, Optional.empty());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to update an existing home with new metadata
|
||||
*
|
||||
* @param home the home to update
|
||||
* @param newHomeMeta the new metadata for the home
|
||||
* @return a future supplying the {@link SaveResult}; the result of updating the home
|
||||
*/
|
||||
public CompletableFuture<SaveResult> updateHomeMeta(@NotNull Home home, @NotNull PositionMeta newHomeMeta) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
if (database.getHome(home.owner, newHomeMeta.name).join().isPresent()) {
|
||||
return new SaveResult(SaveResult.ResultType.FAILED_DUPLICATE, Optional.empty());
|
||||
}
|
||||
final String existingHomeName = home.meta.name;
|
||||
return validateMeta(newHomeMeta).map(resultType ->
|
||||
new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
|
||||
home.meta = newHomeMeta;
|
||||
if (!existingHomeName.equals(newHomeMeta.name)) {
|
||||
cache.homes.get(home.owner.uuid).remove(existingHomeName);
|
||||
cache.homes.get(home.owner.uuid).add(newHomeMeta.name);
|
||||
}
|
||||
cache.positionLists.clear();
|
||||
return database.setHome(home).thenApply(value ->
|
||||
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tries to update the position of a {@link User}'s home
|
||||
*
|
||||
* @param homeOwner the owner of the home
|
||||
* @param homeName the name of the home
|
||||
* @param newPosition the new position for the home
|
||||
* @return a future supplying the {@link SaveResult}; the result of updating the home
|
||||
*/
|
||||
public CompletableFuture<Boolean> updateHomePosition(@NotNull User homeOwner, @NotNull String homeName,
|
||||
@NotNull Position newPosition) {
|
||||
return database.getHome(homeOwner, homeName).thenApply(optionalHome -> {
|
||||
if (optionalHome.isPresent()) {
|
||||
return updateHomePosition(optionalHome.get(), newPosition).join();
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to update the position of a {@link User}'s home
|
||||
*
|
||||
* @param home the home to update
|
||||
* @param newPosition the new position for the home
|
||||
* @return a future supplying the {@link SaveResult}; the result of updating the home
|
||||
*/
|
||||
public CompletableFuture<Boolean> updateHomePosition(@NotNull Home home, @NotNull Position newPosition) {
|
||||
home.x = newPosition.x;
|
||||
home.y = newPosition.y;
|
||||
home.z = newPosition.z;
|
||||
home.pitch = newPosition.pitch;
|
||||
home.yaw = newPosition.yaw;
|
||||
home.world = newPosition.world;
|
||||
home.server = newPosition.server;
|
||||
return database.setHome(home).thenApply(ignored -> true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to change the privacy of a {@link User}'s home by given name
|
||||
*
|
||||
* @param homeOwner the owner of the home
|
||||
* @param homeName the name of the home
|
||||
* @param isHomePublic whether the home should be public or not
|
||||
* @return a future supplying a boolean; {@code true} if the privacy was changed, otherwise {@code false}
|
||||
* if the home name was invalid.
|
||||
*/
|
||||
public CompletableFuture<Boolean> updateHomePrivacy(@NotNull User homeOwner, @NotNull String homeName,
|
||||
final boolean isHomePublic) {
|
||||
return database.getHome(homeOwner, homeName).thenApply(optionalHome -> {
|
||||
if (optionalHome.isPresent()) {
|
||||
return updateHomePrivacy(optionalHome.get(), isHomePublic).join();
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to change the privacy of a {@link User}'s home by given name
|
||||
*
|
||||
* @param home the home to update
|
||||
* @param isHomePublic whether the home should be public or not
|
||||
* @return a future supplying a boolean; {@code true} if the privacy was changed, otherwise {@code false}
|
||||
*/
|
||||
public CompletableFuture<Boolean> updateHomePrivacy(@NotNull Home home, final boolean isHomePublic) {
|
||||
home.isPublic = isHomePublic;
|
||||
return database.setHome(home).thenApply(ignored -> {
|
||||
if (isHomePublic) {
|
||||
cache.publicHomes.putIfAbsent(home.owner.username, new ArrayList<>());
|
||||
cache.publicHomes.get(home.owner.username).add(home.meta.name);
|
||||
} else if (cache.publicHomes.containsKey(home.owner.username)) {
|
||||
cache.publicHomes.get(home.owner.username).remove(home.meta.name);
|
||||
}
|
||||
cache.positionLists.clear();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,31 +208,31 @@ public class SavedPositionManager {
|
||||
*
|
||||
* @param warpMeta metadata about the warp
|
||||
* @param position where the warp should be set
|
||||
* @return a future supplying the {@link SetResult}; the result of setting the warp
|
||||
* @return a future supplying the {@link SaveResult}; the result of setting the warp
|
||||
*/
|
||||
public CompletableFuture<SetResult> setWarp(@NotNull PositionMeta warpMeta, @NotNull Position position) {
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
validateMeta(warpMeta).map(resultType -> new SetResult(resultType, null)).orElseGet(() ->
|
||||
database.getWarp(warpMeta.name).thenApply(optionalWarp -> {
|
||||
if (optionalWarp.isEmpty()) {
|
||||
final Warp warp = new Warp(position, warpMeta);
|
||||
cache.warps.add(warp.meta.name);
|
||||
cache.positionLists.clear();
|
||||
return database.setWarp(warp).thenApply(ignored ->
|
||||
new SetResult(SetResult.ResultType.SUCCESS, warp)).join();
|
||||
}
|
||||
return new SetResult(SetResult.ResultType.FAILED_DUPLICATE, null);
|
||||
}).join()));
|
||||
public CompletableFuture<SaveResult> setWarp(@NotNull PositionMeta warpMeta, @NotNull Position position) {
|
||||
return database.getWarp(warpMeta.name).thenApply(optionalWarp ->
|
||||
validateMeta(warpMeta).map(resultType -> new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
|
||||
if (optionalWarp.isEmpty()) {
|
||||
final Warp warp = new Warp(position, warpMeta);
|
||||
cache.warps.add(warp.meta.name);
|
||||
cache.positionLists.clear();
|
||||
return database.setWarp(warp).thenApply(ignored ->
|
||||
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
|
||||
}
|
||||
return new SaveResult(SaveResult.ResultType.FAILED_DUPLICATE, Optional.empty());
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to delete a warp by given name
|
||||
*
|
||||
* @param warpName the name of the warp
|
||||
* @return a future supplying a boolean; {@code true} if the warp was deleted, otherwise {@code false} if the warp name was invalid.
|
||||
* @return a future supplying a boolean; {@code true} if the warp was deleted, otherwise {@code false}
|
||||
* if the warp name was invalid.
|
||||
*/
|
||||
public CompletableFuture<Boolean> deleteWarp(@NotNull String warpName) {
|
||||
return CompletableFuture.supplyAsync(() -> database.getWarp(warpName).thenApply(optionalWarp -> {
|
||||
return database.getWarp(warpName).thenApply(optionalWarp -> {
|
||||
if (optionalWarp.isPresent()) {
|
||||
final Warp warp = optionalWarp.get();
|
||||
return database.deleteWarp(warp.uuid).thenApply(ignored -> {
|
||||
@@ -121,40 +242,120 @@ public class SavedPositionManager {
|
||||
}).join();
|
||||
}
|
||||
return false;
|
||||
}).join());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to update an existing home with new metadata
|
||||
*
|
||||
* @param existingWarpName the current name of the home to update
|
||||
* @param newWarpMeta the new metadata for the home
|
||||
* @return a future supplying the {@link SaveResult}; the result of updating the home
|
||||
*/
|
||||
public CompletableFuture<SaveResult> updateWarpMeta(@NotNull String existingWarpName,
|
||||
@NotNull PositionMeta newWarpMeta) {
|
||||
return database.getWarp(existingWarpName).thenApply(optionalWarp -> {
|
||||
if (optionalWarp.isPresent()) {
|
||||
return updateWarpMeta(optionalWarp.get(), newWarpMeta).join();
|
||||
}
|
||||
return new SaveResult(SaveResult.ResultType.FAILED_DOES_NOT_EXIST, Optional.empty());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to update an existing warp with new metadata
|
||||
*
|
||||
* @param warp the warp to update
|
||||
* @param newWarpMeta the new metadata for the home
|
||||
* @return a future supplying the {@link SaveResult}; the result of updating the warp
|
||||
*/
|
||||
public CompletableFuture<SaveResult> updateWarpMeta(@NotNull Warp warp, @NotNull PositionMeta newWarpMeta) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
if (database.getWarp(newWarpMeta.name).join().isPresent()) {
|
||||
return new SaveResult(SaveResult.ResultType.FAILED_DUPLICATE, Optional.empty());
|
||||
}
|
||||
final String existingWarpName = warp.meta.name;
|
||||
return validateMeta(newWarpMeta).map(resultType ->
|
||||
new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
|
||||
warp.meta = newWarpMeta;
|
||||
if (!existingWarpName.equals(newWarpMeta.name)) {
|
||||
cache.warps.remove(existingWarpName);
|
||||
cache.warps.add(newWarpMeta.name);
|
||||
}
|
||||
cache.positionLists.clear();
|
||||
return database.setWarp(warp).thenApply(value ->
|
||||
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tries to update the position of a warp
|
||||
*
|
||||
* @param warpName the name of the home
|
||||
* @param newPosition the new position for the home
|
||||
* @return a future supplying the {@link SaveResult}; the result of updating the home
|
||||
*/
|
||||
public CompletableFuture<Boolean> updateWarpPosition(@NotNull String warpName,
|
||||
@NotNull Position newPosition) {
|
||||
return database.getWarp(warpName).thenApply(optionalWarp -> {
|
||||
if (optionalWarp.isPresent()) {
|
||||
return updateWarpPosition(optionalWarp.get(), newPosition).join();
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to update the position of a warp
|
||||
*
|
||||
* @param warp the warp to update
|
||||
* @param newPosition the new position for the warp
|
||||
* @return a future supplying the {@link SaveResult}; the result of updating the warp
|
||||
*/
|
||||
public CompletableFuture<Boolean> updateWarpPosition(@NotNull Warp warp, @NotNull Position newPosition) {
|
||||
warp.x = newPosition.x;
|
||||
warp.y = newPosition.y;
|
||||
warp.z = newPosition.z;
|
||||
warp.pitch = newPosition.pitch;
|
||||
warp.yaw = newPosition.yaw;
|
||||
warp.world = newPosition.world;
|
||||
warp.server = newPosition.server;
|
||||
return database.setWarp(warp).thenApply(ignored -> true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates {@link PositionMeta} to ensure it can be added to the database
|
||||
*
|
||||
* @param positionMeta the {@link PositionMeta} to validate
|
||||
* @return An optional. If the {@link PositionMeta} is invalid, this will contain a {@link SetResult.ResultType}
|
||||
* @return An optional. If the {@link PositionMeta} is invalid, this will contain a {@link SaveResult.ResultType}
|
||||
* identifying which constraint is violated by the metadata
|
||||
*/
|
||||
private Optional<SetResult.ResultType> validateMeta(PositionMeta positionMeta) {
|
||||
private Optional<SaveResult.ResultType> validateMeta(@NotNull PositionMeta positionMeta) {
|
||||
//todo options for allowing unicode names / descriptions
|
||||
if (positionMeta.name.length() > 16) {
|
||||
return Optional.of(SetResult.ResultType.FAILED_NAME_LENGTH);
|
||||
return Optional.of(SaveResult.ResultType.FAILED_NAME_LENGTH);
|
||||
}
|
||||
if (!RegexUtil.NAME_PATTERN.matcher(positionMeta.name).matches()) {
|
||||
return Optional.of(SetResult.ResultType.FAILED_NAME_CHARACTERS);
|
||||
return Optional.of(SaveResult.ResultType.FAILED_NAME_CHARACTERS);
|
||||
}
|
||||
if (positionMeta.description.length() > 255) {
|
||||
return Optional.of(SetResult.ResultType.FAILED_DESCRIPTION_LENGTH);
|
||||
return Optional.of(SaveResult.ResultType.FAILED_DESCRIPTION_LENGTH);
|
||||
}
|
||||
if (!RegexUtil.DESCRIPTION_PATTERN.matcher(positionMeta.name).matches()) {
|
||||
return Optional.of(SetResult.ResultType.FAILED_DESCRIPTION_CHARACTERS);
|
||||
return Optional.of(SaveResult.ResultType.FAILED_DESCRIPTION_CHARACTERS);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds information about the result of setting a warp or home
|
||||
* A record of information about the result of setting or updating a {@link SavedPosition} in the form of a warp or home
|
||||
*
|
||||
* @param resultType
|
||||
* @param setPosition
|
||||
* @param resultType The {@link ResultType} of the result returned, indicating success
|
||||
* @param savedPosition The {@link Position} of the saved position, if the result is {@link ResultType#SUCCESS}, otherwise {@link Optional#empty()}
|
||||
*/
|
||||
public record SetResult(@NotNull ResultType resultType, @Nullable SavedPosition setPosition) {
|
||||
public record SaveResult(@NotNull ResultType resultType, @NotNull Optional<SavedPosition> savedPosition) {
|
||||
|
||||
/**
|
||||
* Indicates the result of setting a warp or home
|
||||
@@ -165,33 +366,38 @@ public class SavedPositionManager {
|
||||
*/
|
||||
SUCCESS(true),
|
||||
|
||||
/**
|
||||
* The home or warp was not set or updated because it does not exist
|
||||
*/
|
||||
FAILED_DOES_NOT_EXIST(false),
|
||||
|
||||
/**
|
||||
* The position was not set; one by this name has already been set (for the user in the case of homes)
|
||||
*/
|
||||
FAILED_DUPLICATE(false),
|
||||
|
||||
/**
|
||||
* The position was not set; the name exceeds the maximum length
|
||||
* The position was not set or updated; the name exceeds the maximum length
|
||||
*/
|
||||
FAILED_NAME_LENGTH(false),
|
||||
|
||||
/**
|
||||
* The position was not set; the description exceeds the maximum length
|
||||
* The position was not set or updated; the description exceeds the maximum length
|
||||
*/
|
||||
FAILED_DESCRIPTION_LENGTH(false),
|
||||
|
||||
/**
|
||||
* The position was not set; the name uses illegal characters
|
||||
* The position was not set or updated; the name uses illegal characters
|
||||
*/
|
||||
FAILED_NAME_CHARACTERS(false),
|
||||
|
||||
/**
|
||||
* The position was not set; the description uses illegal characters
|
||||
* The position was not set or updated; the description uses illegal characters
|
||||
*/
|
||||
FAILED_DESCRIPTION_CHARACTERS(false),
|
||||
|
||||
/**
|
||||
* The position was not set; the position is not in a valid world
|
||||
* The position was not set or updated; an exception occurred
|
||||
*/
|
||||
FAILED_ERROR(false);
|
||||
|
||||
|
||||
Reference in new issue
Block a user