Refactor event dispatch system

This commit is contained in:
William committed 2023-03-17 10:55:28 +00:00
1 parent 9ca141a58f
commit 9903503f03
27 files changed
+329 -404

No files matched your search

@@ -30,14 +30,17 @@ public class DelHomeCommand extends SavedPositionCommand<Home> {
.ifPresent(user::sendMessage);
return;
}
try {
plugin.getManager().homes().deleteHome(home);
} catch (ValidationException e) {
e.dispatchHomeError(executor, !home.getOwner().equals(executor), plugin, home.getName());
return;
}
plugin.getLocales().getLocale("home_deleted", home.getName())
.ifPresent(executor::sendMessage);
plugin.fireEvent(plugin.getHomeDeleteEvent(home), (event) -> {
try {
plugin.getManager().homes().deleteHome(home);
} catch (ValidationException e) {
e.dispatchHomeError(executor, !home.getOwner().equals(executor), plugin, home.getName());
return;
}
plugin.getLocales().getLocale("home_deleted", home.getName())
.ifPresent(executor::sendMessage);
});
}
private boolean handleDeleteAll(@NotNull OnlineUser user, @NotNull String[] args) {
@@ -50,15 +53,17 @@ public class DelHomeCommand extends SavedPositionCommand<Home> {
return true;
}
final int deleted = plugin.getManager().homes().deleteAllHomes(user);
if (deleted == 0) {
plugin.getLocales().getLocale("error_no_homes_set")
.ifPresent(user::sendMessage);
return true;
}
plugin.fireEvent(plugin.getDeleteAllHomesEvent(user), (event) -> {
final int deleted = plugin.getManager().homes().deleteAllHomes(user);
if (deleted == 0) {
plugin.getLocales().getLocale("error_no_homes_set")
.ifPresent(user::sendMessage);
return;
}
plugin.getLocales().getLocale("delete_all_homes_success", Integer.toString(deleted))
.ifPresent(user::sendMessage);
plugin.getLocales().getLocale("delete_all_homes_success", Integer.toString(deleted))
.ifPresent(user::sendMessage);
});
return true;
}
return false;
@@ -30,14 +30,18 @@ public class DelWarpCommand extends SavedPositionCommand<Warp> {
.ifPresent(user::sendMessage);
return;
}
try {
plugin.getManager().warps().deleteWarp(warp);
} catch (ValidationException e) {
e.dispatchWarpError(executor, plugin, warp.getName());
return;
}
plugin.getLocales().getLocale("warp_deleted", warp.getName())
.ifPresent(executor::sendMessage);
plugin.fireEvent(plugin.getWarpDeleteEvent(warp), (event) -> {
try {
plugin.getManager().warps().deleteWarp(warp);
} catch (ValidationException e) {
e.dispatchWarpError(executor, plugin, warp.getName());
return;
}
plugin.getLocales().getLocale("warp_deleted", warp.getName())
.ifPresent(executor::sendMessage);
});
}
private boolean handleDeleteAll(@NotNull CommandUser executor, @NotNull String[] args) {
@@ -50,15 +54,17 @@ public class DelWarpCommand extends SavedPositionCommand<Warp> {
return true;
}
final int deleted = plugin.getManager().warps().deleteAllWarps();
if (deleted == 0) {
plugin.getLocales().getLocale("error_no_warps_set")
.ifPresent(executor::sendMessage);
return true;
}
plugin.fireEvent(plugin.getDeleteAllWarpsEvent(), (event) -> {
final int deleted = plugin.getManager().warps().deleteAllWarps();
if (deleted == 0) {
plugin.getLocales().getLocale("error_no_warps_set")
.ifPresent(executor::sendMessage);
return;
}
plugin.getLocales().getLocale("delete_all_warps_success", Integer.toString(deleted))
.ifPresent(executor::sendMessage);
plugin.getLocales().getLocale("delete_all_warps_success", Integer.toString(deleted))
.ifPresent(executor::sendMessage);
});
return true;
}
return false;
@@ -7,7 +7,6 @@ import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.util.Permission;
import net.william278.huskhomes.util.ValidationException;
import org.jetbrains.annotations.NotNull;
@@ -36,8 +35,8 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
final Optional<String> operation = parseStringArg(args, 0);
if (operation.isEmpty()) {
getHomeEditorWindow(home, true, !ownerEditing,
!ownerEditing || executor.hasPermission(Permission.COMMAND_HOME_OTHER.node),
executor.hasPermission(Permission.COMMAND_EDIT_HOME_PRIVACY.node))
!ownerEditing || executor.hasPermission(getOtherPermission()),
executor.hasPermission(getPermission("privacy")))
.forEach(executor::sendMessage);
return;
}
@@ -66,21 +65,23 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
return;
}
try {
plugin.getManager().homes().setHomeName(home, newName.get());
} catch (ValidationException e) {
e.dispatchHomeError(executor, false, plugin, newName.get());
return;
}
plugin.fireEvent(plugin.getHomeSaveEvent(home), (event) -> {
try {
plugin.getManager().homes().setHomeName(home, newName.get());
} catch (ValidationException e) {
e.dispatchHomeError(executor, false, plugin, newName.get());
return;
}
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_update_name", home.getName(), newName.get())
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_update_name_other", home.getOwner().getUsername(),
home.getName(), newName.get())
.ifPresent(executor::sendMessage);
}
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_update_name", home.getName(), newName.get())
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_update_name_other", home.getOwner().getUsername(),
home.getName(), newName.get())
.ifPresent(executor::sendMessage);
}
});
}
private void setHomeDescription(@NotNull CommandUser executor, @NotNull Home home, boolean ownerEditing, @NotNull String[] args) {
@@ -92,21 +93,23 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
return;
}
try {
plugin.getManager().homes().setHomeDescription(home, newDescription.get());
} catch (ValidationException e) {
e.dispatchHomeError(executor, false, plugin, newDescription.get());
return;
}
plugin.fireEvent(plugin.getHomeSaveEvent(home), (event) -> {
try {
plugin.getManager().homes().setHomeDescription(home, newDescription.get());
} catch (ValidationException e) {
e.dispatchHomeError(executor, false, plugin, newDescription.get());
return;
}
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_update_description", home.getName(), newDescription.get())
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_update_description_other", home.getOwner().getUsername(),
home.getName(), newDescription.get())
.ifPresent(executor::sendMessage);
}
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_update_description", home.getName(), newDescription.get())
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_update_description_other", home.getOwner().getUsername(),
home.getName(), newDescription.get())
.ifPresent(executor::sendMessage);
}
});
}
private void setHomePosition(@NotNull CommandUser executor, @NotNull Home home, boolean ownerEditing) {
@@ -116,24 +119,26 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
return;
}
try {
plugin.getManager().homes().setHomePosition(home, user.getPosition());
} catch (ValidationException e) {
e.dispatchHomeError(executor, false, plugin, home.getName());
return;
}
plugin.fireEvent(plugin.getHomeDeleteEvent(home), (event) -> {
try {
plugin.getManager().homes().setHomePosition(home, user.getPosition());
} catch (ValidationException e) {
e.dispatchHomeError(executor, false, plugin, home.getName());
return;
}
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_update_location", home.getName())
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_update_location_other", home.getOwner().getUsername(), home.getName())
.ifPresent(executor::sendMessage);
}
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_update_location", home.getName())
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_update_location_other", home.getOwner().getUsername(), home.getName())
.ifPresent(executor::sendMessage);
}
});
}
private void setHomePrivacy(@NotNull CommandUser executor, @NotNull Home home, boolean ownerEditing, @NotNull String[] args) {
if (!executor.hasPermission(Permission.COMMAND_EDIT_HOME_PRIVACY.node)) {
if (!executor.hasPermission(getPermission("privacy"))) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(executor::sendMessage);
return;
@@ -152,40 +157,42 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
return;
}
try {
plugin.getManager().homes().setHomePrivacy(home, privacyArg.get().equals("public"));
} catch (ValidationException e) {
int maxHomes = executor instanceof OnlineUser user ?
user.getMaxPublicHomes(plugin.getSettings().getMaxPublicHomes(),
plugin.getSettings().doStackPermissionLimits()) :
plugin.getSettings().getMaxPublicHomes();
e.dispatchHomeError(executor, false, plugin, Integer.toString(maxHomes));
return;
}
// Perform transaction
if (executor instanceof OnlineUser user) {
plugin.performEconomyTransaction(user, EconomyHook.Action.MAKE_HOME_PUBLIC);
}
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_privacy_" + privacyArg.get() + "_success",
home.getMeta().getName())
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_privacy_" + privacyArg.get() + "_success_other",
home.getOwner().getUsername(), home.getMeta().getName())
.ifPresent(executor::sendMessage);
}
// Show the menu if the menu flag is set
parseStringArg(args, 2).ifPresent(arg -> {
if (arg.equalsIgnoreCase("-m")) {
getHomeEditorWindow(home, false, !ownerEditing,
ownerEditing || executor.hasPermission(Permission.COMMAND_HOME_OTHER.node),
executor.hasPermission(Permission.COMMAND_EDIT_HOME_PRIVACY.node))
.forEach(executor::sendMessage);
plugin.fireEvent(plugin.getHomeSaveEvent(home), (event) -> {
try {
plugin.getManager().homes().setHomePrivacy(home, privacyArg.get().equals("public"));
} catch (ValidationException e) {
int maxHomes = executor instanceof OnlineUser user ?
user.getMaxPublicHomes(plugin.getSettings().getMaxPublicHomes(),
plugin.getSettings().doStackPermissionLimits()) :
plugin.getSettings().getMaxPublicHomes();
e.dispatchHomeError(executor, false, plugin, Integer.toString(maxHomes));
return;
}
// Perform transaction
if (executor instanceof OnlineUser user) {
plugin.performEconomyTransaction(user, EconomyHook.Action.MAKE_HOME_PUBLIC);
}
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_privacy_" + privacyArg.get() + "_success",
home.getMeta().getName())
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_privacy_" + privacyArg.get() + "_success_other",
home.getOwner().getUsername(), home.getMeta().getName())
.ifPresent(executor::sendMessage);
}
// Show the menu if the menu flag is set
parseStringArg(args, 2).ifPresent(arg -> {
if (arg.equalsIgnoreCase("-m")) {
getHomeEditorWindow(home, false, !ownerEditing,
ownerEditing || executor.hasPermission(getOtherPermission()),
executor.hasPermission(getPermission("privacy")))
.forEach(executor::sendMessage);
}
});
});
}
@@ -1,114 +1,71 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.util.Permission;
import net.william278.huskhomes.user.User;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
public class HomeListCommand extends Command implements ConsoleExecutable {
public class HomeListCommand extends Command {
protected HomeListCommand(@NotNull HuskHomes implementor) {
super("homelist", Permission.COMMAND_HOME, implementor, "homes");
protected HomeListCommand(@NotNull HuskHomes plugin) {
super("homelist", List.of("homes"), "[player] [page]", plugin);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
switch (args.length) {
case 0 -> showHomeList(onlineUser, onlineUser.getUsername(), 1);
case 1 -> {
try {
int pageNumber = Integer.parseInt(args[0]);
showHomeList(onlineUser, onlineUser.getUsername(), pageNumber);
} catch (NumberFormatException e) {
showHomeList(onlineUser, args[0], 1);
}
}
case 2 -> {
try {
int pageNumber = Integer.parseInt(args[1]);
showHomeList(onlineUser, args[0], pageNumber);
} catch (NumberFormatException e) {
plugin.getLocales().getLocale("error_invalid_syntax", "/homelist [player] [page]")
.ifPresent(onlineUser::sendMessage);
}
}
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/homelist [page]")
.ifPresent(onlineUser::sendMessage);
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
final Optional<String> homeOwner = args.length == 2 ? parseStringArg(args, 0)
: executor instanceof OnlineUser user ? Optional.of(user.getUsername()) : Optional.empty();
final int pageNumber = parseIntArg(args, args.length > 0 ? args.length - 1 : 0).orElse(1);
if (homeOwner.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
.ifPresent(executor::sendMessage);
return;
}
showHomeList(executor, homeOwner.get(), pageNumber);
}
/**
* Show a (cached) list of a {@link OnlineUser}'s homes
*
* @param onlineUser the user to display the homes to
* @param homeOwner the user whose homes should be displayed
* @param pageNumber page number to display
*/
private void showHomeList(@NotNull OnlineUser onlineUser, @NotNull String homeOwner, int pageNumber) {
private void showHomeList(@NotNull CommandUser executor, @NotNull String homeOwner, int pageNumber) {
if (plugin.getCache().getPrivateHomeLists().containsKey(homeOwner)) {
onlineUser.sendMessage(plugin.getCache().getPrivateHomeLists().get(homeOwner).getNearestValidPage(pageNumber));
executor.sendMessage(plugin.getCache().getPrivateHomeLists().get(homeOwner).getNearestValidPage(pageNumber));
return;
}
plugin.getDatabase().getUserDataByName(homeOwner).thenAccept(optionalUser -> optionalUser.ifPresentOrElse(userData -> {
if (!userData.getUserUuid().equals(onlineUser.getUuid())) {
if (!onlineUser.hasPermission(Permission.COMMAND_HOME_OTHER.node)) {
plugin.getLocales().getLocale("error_no_permission").ifPresent(onlineUser::sendMessage);
return;
}
}
plugin.getDatabase().getHomes(userData.user()).thenAcceptAsync(homes -> {
if (homes.isEmpty()) {
if (!onlineUser.equals(userData.user())) {
plugin.getLocales().getLocale("error_no_homes_set_other",
userData.user().username).ifPresent(onlineUser::sendMessage);
} else {
plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
}
return;
}
plugin.getCache().getHomeList(onlineUser, userData.user(),
plugin.getLocales(), homes,
plugin.getSettings().getListItemsPerPage(), pageNumber)
.ifPresent(onlineUser::sendMessage);
});
}, () -> plugin.getLocales().getLocale("error_player_not_found", homeOwner).ifPresent(onlineUser::sendMessage)));
}
@Override
public void onConsoleExecute(@NotNull String[] args) {
if (args.length != 1) {
plugin.log(Level.WARNING, "Invalid syntax. Usage: homelist <player>");
final Optional<User> targetUser = plugin.getDatabase().getUserDataByName(homeOwner).map(SavedUser::getUser);
if (targetUser.isEmpty()) {
plugin.getLocales().getLocale("error_player_not_found", homeOwner)
.ifPresent(executor::sendMessage);
return;
}
CompletableFuture.runAsync(() -> {
final Optional<SavedUser> userData = plugin.getDatabase().getUserDataByName(args[0]).join();
if (userData.isEmpty()) {
plugin.log(Level.WARNING, "Player not found: " + args[0]);
final User user = targetUser.get();
if (executor instanceof OnlineUser onlineUser && !user.getUuid().equals(onlineUser.getUuid())) {
if (!executor.hasPermission(getPermission("other"))) {
plugin.getLocales().getLocale("error_no_permission").ifPresent(executor::sendMessage);
return;
}
final List<Home> homes = plugin.getDatabase().getHomes(userData.get().user()).join();
StringJoiner rowJoiner = new StringJoiner("\t");
}
plugin.log(Level.INFO, "List of " + userData.get().user().getUsername() + "'s "
+ homes.size() + " homes:");
for (int i = 0; i < homes.size(); i++) {
final String home = homes.get(i).getMeta().getName();
rowJoiner.add(home.length() < 16 ? home + " ".repeat(16 - home.length()) : home);
if ((i + 1) % 3 == 0) {
plugin.log(Level.INFO, rowJoiner.toString());
rowJoiner = new StringJoiner("\t");
}
final List<Home> homes = plugin.getDatabase().getHomes(user);
if (homes.isEmpty()) {
if (!executor.equals(user)) {
plugin.getLocales().getLocale("error_no_homes_set_other", user.getUsername())
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("error_no_homes_set").ifPresent(executor::sendMessage);
}
plugin.log(Level.INFO, rowJoiner.toString());
});
return;
}
plugin.getCache().getHomeList(executor, user,
plugin.getLocales(), homes,
plugin.getSettings().getListItemsPerPage(), pageNumber)
.ifPresent(executor::sendMessage);
}
}