forked from public-mirrors/HuskSync
Echest, invsee
parent
1829526aa7
commit
fd08a3e7d0
@ -0,0 +1,19 @@
|
||||
package net.william278.husksync.command;
|
||||
|
||||
import net.william278.husksync.BukkitHuskSync;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Commands available on the Bukkit HuskSync implementation
|
||||
*/
|
||||
public enum BukkitCommandType {
|
||||
HUSKSYNC_COMMAND(new HuskSyncCommand(BukkitHuskSync.getInstance())),
|
||||
HUSKSYNC_INVSEE(new InvseeCommand(BukkitHuskSync.getInstance())),
|
||||
HUSKSYNC_ECHEST(new EchestCommand(BukkitHuskSync.getInstance()));
|
||||
|
||||
public final CommandBase commandBase;
|
||||
|
||||
BukkitCommandType(@NotNull CommandBase commandBase) {
|
||||
this.commandBase = commandBase;
|
||||
}
|
||||
}
|
@ -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));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -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));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue