Add /edithome, use instant for timestamps, new locales

This commit is contained in:
William committed 2022-08-16 00:58:26 +01:00
1 parent eddae1051c
commit a91128ca3d
24 files changed
+793 -205

No files matched your search

@@ -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());