9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2026-01-04 15:31:37 +00:00

Echest, invsee

This commit is contained in:
William
2022-07-05 15:24:26 +01:00
parent 1829526aa7
commit fd08a3e7d0
14 changed files with 433 additions and 21 deletions

View File

@@ -3,6 +3,7 @@ package net.william278.husksync;
import net.william278.husksync.config.Locales;
import net.william278.husksync.config.Settings;
import net.william278.husksync.data.DataAdapter;
import net.william278.husksync.editor.DataEditor;
import net.william278.husksync.database.Database;
import net.william278.husksync.player.OnlineUser;
import net.william278.husksync.redis.RedisManager;
@@ -26,6 +27,8 @@ public interface HuskSync {
@NotNull DataAdapter getDataAdapter();
@NotNull DataEditor getDataEditor();
@NotNull Settings getSettings();
@NotNull Locales getLocales();

View File

@@ -0,0 +1,63 @@
package net.william278.husksync.command;
import net.william278.husksync.HuskSync;
import net.william278.husksync.data.UserData;
import net.william278.husksync.data.VersionedUserData;
import net.william278.husksync.editor.InventoryEditorMenu;
import net.william278.husksync.player.OnlineUser;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public class EchestCommand extends CommandBase {
public EchestCommand(@NotNull HuskSync implementor) {
super("echest", Permission.COMMAND_VIEW_INVENTORIES, implementor, "openechest");
}
@Override
public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
if (args.length == 0 || args.length > 2) {
plugin.getLocales().getLocale("error_invalid_syntax", "/echest <player>")
.ifPresent(player::sendMessage);
return;
}
plugin.getDatabase().getUserByName(args[0].toLowerCase()).thenAcceptAsync(optionalUser -> {
optionalUser.ifPresentOrElse(user -> {
List<VersionedUserData> userData = plugin.getDatabase().getUserData(user).join();
Optional<VersionedUserData> dataToView;
if (args.length == 2) {
try {
final UUID version = UUID.fromString(args[1]);
dataToView = userData.stream().filter(data -> data.versionUUID().equals(version)).findFirst();
} catch (IllegalArgumentException e) {
plugin.getLocales().getLocale("error_invalid_syntax",
"/echest <player> [version_uuid]").ifPresent(player::sendMessage);
return;
}
} else {
dataToView = userData.stream().sorted().findFirst();
}
dataToView.ifPresentOrElse(versionedUserData -> {
final UserData data = versionedUserData.userData();
final InventoryEditorMenu menu = InventoryEditorMenu.createEnderChestMenu(
data.getEnderChestData(), user, player);
plugin.getLocales().getLocale("viewing_ender_chest_of", user.username)
.ifPresent(player::sendMessage);
plugin.getDataEditor().openInventoryMenu(player, menu).thenAcceptAsync(inventoryDataOnClose -> {
final UserData updatedUserData = new UserData(data.getStatusData(),
data.getInventoryData(), menu.canEdit ? inventoryDataOnClose : data.getEnderChestData(),
data.getPotionEffectData(), data.getAdvancementData(),
data.getStatisticData(), data.getLocationData(),
data.getPersistentDataContainerData());
plugin.getDatabase().setUserData(user, updatedUserData).join();
});
}, () -> plugin.getLocales().getLocale(args.length == 2 ? "error_invalid_version_uuid"
: "error_no_data_to_display").ifPresent(player::sendMessage));
}, () -> plugin.getLocales().getLocale("error_invalid_player").ifPresent(player::sendMessage));
});
}
}

View File

@@ -0,0 +1,63 @@
package net.william278.husksync.command;
import net.william278.husksync.HuskSync;
import net.william278.husksync.data.UserData;
import net.william278.husksync.data.VersionedUserData;
import net.william278.husksync.editor.InventoryEditorMenu;
import net.william278.husksync.player.OnlineUser;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public class InvseeCommand extends CommandBase {
public InvseeCommand(@NotNull HuskSync implementor) {
super("invsee", Permission.COMMAND_VIEW_INVENTORIES, implementor, "openinv");
}
@Override
public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
if (args.length == 0 || args.length > 2) {
plugin.getLocales().getLocale("error_invalid_syntax", "/invsee <player>")
.ifPresent(player::sendMessage);
return;
}
plugin.getDatabase().getUserByName(args[0].toLowerCase()).thenAcceptAsync(optionalUser -> {
optionalUser.ifPresentOrElse(user -> {
List<VersionedUserData> userData = plugin.getDatabase().getUserData(user).join();
Optional<VersionedUserData> dataToView;
if (args.length == 2) {
try {
final UUID version = UUID.fromString(args[1]);
dataToView = userData.stream().filter(data -> data.versionUUID().equals(version)).findFirst();
} catch (IllegalArgumentException e) {
plugin.getLocales().getLocale("error_invalid_syntax",
"/invsee <player> [version_uuid]").ifPresent(player::sendMessage);
return;
}
} else {
dataToView = userData.stream().sorted().findFirst();
}
dataToView.ifPresentOrElse(versionedUserData -> {
final UserData data = versionedUserData.userData();
final InventoryEditorMenu menu = InventoryEditorMenu.createInventoryMenu(
data.getInventoryData(), user, player);
plugin.getLocales().getLocale("viewing_inventory_of", user.username)
.ifPresent(player::sendMessage);
plugin.getDataEditor().openInventoryMenu(player, menu).thenAcceptAsync(inventoryDataOnClose -> {
final UserData updatedUserData = new UserData(data.getStatusData(),
menu.canEdit ? inventoryDataOnClose : data.getInventoryData(),
data.getEnderChestData(), data.getPotionEffectData(), data.getAdvancementData(),
data.getStatisticData(), data.getLocationData(),
data.getPersistentDataContainerData());
plugin.getDatabase().setUserData(user, updatedUserData).join();
});
}, () -> plugin.getLocales().getLocale(args.length == 2 ? "error_invalid_version_uuid"
: "error_no_data_to_display").ifPresent(player::sendMessage));
}, () -> plugin.getLocales().getLocale("error_invalid_player").ifPresent(player::sendMessage));
});
}
}

View File

@@ -0,0 +1,90 @@
package net.william278.husksync.editor;
import net.william278.husksync.config.Locales;
import net.william278.husksync.data.InventoryData;
import net.william278.husksync.data.VersionedUserData;
import net.william278.husksync.player.OnlineUser;
import net.william278.husksync.player.User;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
/**
* Provides methods for displaying and editing user data
*/
public class DataEditor {
/**
* Map of currently open inventory and ender chest data editors
*/
@NotNull
protected final HashMap<UUID, InventoryEditorMenu> openInventoryMenus;
public DataEditor() {
this.openInventoryMenus = new HashMap<>();
}
/**
* Open an inventory or ender chest editor menu
*
* @param user The online user to open the editor for
* @param inventoryEditorMenu The {@link InventoryEditorMenu} to open
* @return The inventory editor menu
* @see InventoryEditorMenu#createInventoryMenu(InventoryData, User, OnlineUser)
* @see InventoryEditorMenu#createEnderChestMenu(InventoryData, User, OnlineUser)
*/
public CompletableFuture<InventoryData> openInventoryMenu(@NotNull OnlineUser user,
@NotNull InventoryEditorMenu inventoryEditorMenu) {
this.openInventoryMenus.put(user.uuid, inventoryEditorMenu);
return inventoryEditorMenu.showInventory(user);
}
/**
* Close an inventory or ender chest editor menu
*
* @param user The online user to close the editor for
* @param inventoryData the {@link InventoryData} contained within the menu at the time of closing
*/
public void closeInventoryMenu(@NotNull OnlineUser user, @NotNull InventoryData inventoryData) {
if (this.openInventoryMenus.containsKey(user.uuid)) {
this.openInventoryMenus.get(user.uuid).closeInventory(inventoryData);
}
this.openInventoryMenus.remove(user.uuid);
}
/**
* Returns whether edits to the inventory or ender chest menu are allowed
*
* @param user The online user with an inventory open to check
* @return {@code true} if edits to the inventory or ender chest menu are allowed; {@code false} otherwise, including if they don't have an inventory open
*/
public boolean cancelInventoryEdit(@NotNull OnlineUser user) {
if (this.openInventoryMenus.containsKey(user.uuid)) {
return !this.openInventoryMenus.get(user.uuid).canEdit;
}
return false;
}
/**
* Display a chat message detailing information about {@link VersionedUserData}
*
* @param user The online user to display the message to
* @param userData The {@link VersionedUserData} to display information about
* @param dataOwner The {@link User} who owns the {@link VersionedUserData}
*/
public void displayDataOverview(@NotNull OnlineUser user, @NotNull VersionedUserData userData,
@NotNull User dataOwner) {
//todo
}
/**
* Returns whether the user has an inventory editor menu open
* @param user {@link OnlineUser} to check
* @return {@code true} if the user has an inventory editor open; {@code false} otherwise
*/
public boolean isEditingInventoryData(@NotNull OnlineUser user) {
return this.openInventoryMenus.containsKey(user.uuid);
}
}

View File

@@ -0,0 +1,53 @@
package net.william278.husksync.editor;
import de.themoep.minedown.MineDown;
import net.william278.husksync.command.Permission;
import net.william278.husksync.data.InventoryData;
import net.william278.husksync.player.OnlineUser;
import net.william278.husksync.player.User;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
public class InventoryEditorMenu {
public final InventoryData inventoryData;
public final int slotCount;
public final MineDown menuTitle;
public final boolean canEdit;
private CompletableFuture<InventoryData> inventoryDataCompletableFuture;
private InventoryEditorMenu(@NotNull InventoryData inventoryData, int slotCount,
@NotNull MineDown menuTitle, boolean canEdit) {
this.inventoryData = inventoryData;
this.menuTitle = menuTitle;
this.slotCount = slotCount;
this.canEdit = canEdit;
}
public CompletableFuture<InventoryData> showInventory(@NotNull OnlineUser user) {
inventoryDataCompletableFuture = new CompletableFuture<>();
user.showMenu(this);
return inventoryDataCompletableFuture;
}
public void closeInventory(@NotNull InventoryData inventoryData) {
inventoryDataCompletableFuture.completeAsync(() -> inventoryData);
}
public static InventoryEditorMenu createInventoryMenu(@NotNull InventoryData inventoryData, @NotNull User dataOwner,
@NotNull OnlineUser viewer) {
return new InventoryEditorMenu(inventoryData, 45,
new MineDown(dataOwner.username + "'s Inventory"),
viewer.hasPermission(Permission.COMMAND_EDIT_INVENTORIES.node));
}
public static InventoryEditorMenu createEnderChestMenu(@NotNull InventoryData inventoryData, @NotNull User dataOwner,
@NotNull OnlineUser viewer) {
return new InventoryEditorMenu(inventoryData, 27,
new MineDown(dataOwner.username + "'s Ender Chest"),
viewer.hasPermission(Permission.COMMAND_EDIT_ENDER_CHESTS.node));
}
}

View File

@@ -2,6 +2,7 @@ package net.william278.husksync.listener;
import net.william278.husksync.HuskSync;
import net.william278.husksync.config.Settings;
import net.william278.husksync.data.InventoryData;
import net.william278.husksync.player.OnlineUser;
import org.jetbrains.annotations.NotNull;
@@ -19,7 +20,7 @@ public abstract class EventListener {
/**
* The plugin instance
*/
private final HuskSync huskSync;
protected final HuskSync huskSync;
/**
* Set of UUIDs current awaiting item synchronization. Events will be cancelled for these users
@@ -84,6 +85,7 @@ public abstract class EventListener {
}
public final void handlePlayerQuit(@NotNull OnlineUser user) {
// Players quitting have their data manually saved by the plugin disable hook
if (disabling) {
return;
}
@@ -110,8 +112,22 @@ public abstract class EventListener {
huskSync.getRedisManager().close();
}
public final void handleMenuClose(@NotNull OnlineUser user, @NotNull InventoryData menuInventory) {
if (disabling) {
return;
}
huskSync.getDataEditor().closeInventoryMenu(user, menuInventory);
}
public final boolean cancelMenuClick(@NotNull OnlineUser user) {
if (disabling) {
return true;
}
return huskSync.getDataEditor().cancelInventoryEdit(user);
}
public final boolean cancelPlayerEvent(@NotNull OnlineUser user) {
return usersAwaitingSync.contains(user.uuid);
return disabling || usersAwaitingSync.contains(user.uuid);
}
}

View File

@@ -3,6 +3,7 @@ package net.william278.husksync.player;
import de.themoep.minedown.MineDown;
import net.william278.husksync.config.Settings;
import net.william278.husksync.data.*;
import net.william278.husksync.editor.InventoryEditorMenu;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -236,6 +237,13 @@ public abstract class OnlineUser extends User {
*/
public abstract boolean hasPermission(@NotNull String node);
/**
* Show the player a {@link InventoryEditorMenu} GUI
*
* @param menu The {@link InventoryEditorMenu} interface to show
*/
public abstract void showMenu(@NotNull InventoryEditorMenu menu);
/**
* Get the player's current {@link UserData}
*