From 54553069bf4e2200af12c8a54418cd5038efb7b1 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 11 Jul 2022 12:44:49 +0100 Subject: [PATCH] Fix ender chest command too --- .../listener/BukkitEventListener.java | 34 +++++++++++-------- .../husksync/player/BukkitPlayer.java | 7 +--- .../husksync/editor/DataEditor.java | 7 ++-- .../husksync/editor/ItemEditorMenu.java | 14 ++++---- .../husksync/editor/ItemEditorMenuType.java | 16 +++++++++ .../husksync/listener/EventListener.java | 6 ++-- .../husksync/player/OnlineUser.java | 5 --- .../husksync/player/DummyPlayer.java | 5 --- 8 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 common/src/main/java/net/william278/husksync/editor/ItemEditorMenuType.java diff --git a/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java b/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java index 7e1a4fda..a12c7fa0 100644 --- a/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java +++ b/bukkit/src/main/java/net/william278/husksync/listener/BukkitEventListener.java @@ -4,6 +4,7 @@ import net.william278.husksync.BukkitHuskSync; import net.william278.husksync.data.BukkitSerializer; import net.william278.husksync.data.DataSerializationException; import net.william278.husksync.data.ItemData; +import net.william278.husksync.editor.ItemEditorMenuType; import net.william278.husksync.player.BukkitPlayer; import net.william278.husksync.player.OnlineUser; import org.bukkit.Bukkit; @@ -24,6 +25,7 @@ import org.bukkit.event.world.WorldSaveEvent; import org.jetbrains.annotations.NotNull; import java.util.Arrays; +import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import java.util.stream.Collectors; @@ -46,25 +48,29 @@ public class BukkitEventListener extends EventListener implements Listener { @EventHandler(ignoreCancelled = true) public void onWorldSave(@NotNull WorldSaveEvent event) { - super.handleWorldSave(event.getWorld().getPlayers().stream().map(BukkitPlayer::adapt) - .collect(Collectors.toList())); + CompletableFuture.runAsync(() -> super.handleAsyncWorldSave(event.getWorld().getPlayers().stream() + .map(BukkitPlayer::adapt).collect(Collectors.toList()))); } @EventHandler(ignoreCancelled = true) public void onInventoryClose(@NotNull InventoryCloseEvent event) { - if (event.getPlayer() instanceof Player player) { - final OnlineUser user = BukkitPlayer.adapt(player); - if (plugin.getDataEditor().isEditingInventoryData(user)) { - try { - BukkitSerializer.serializeItemStackArray(Arrays.copyOf(event.getInventory().getContents(), - event.getPlayer().getInventory().getSize())).thenAccept( - serializedInventory -> super.handleMenuClose(user, new ItemData(serializedInventory))); - } catch (DataSerializationException e) { - plugin.getLoggingAdapter().log(Level.SEVERE, - "Failed to serialize inventory data during menu close", e); - } + CompletableFuture.runAsync(() -> { + if (event.getPlayer() instanceof Player player) { + final OnlineUser user = BukkitPlayer.adapt(player); + plugin.getDataEditor().getEditingInventoryData(user).ifPresent(menu -> { + try { + BukkitSerializer.serializeItemStackArray(Arrays.copyOf(event.getInventory().getContents(), + menu.itemEditorMenuType == ItemEditorMenuType.INVENTORY_VIEWER + ? player.getInventory().getSize() + : player.getEnderChest().getSize())).thenAccept( + serializedInventory -> super.handleMenuClose(user, new ItemData(serializedInventory))); + } catch (DataSerializationException e) { + plugin.getLoggingAdapter().log(Level.SEVERE, + "Failed to serialize inventory data during menu close", e); + } + }); } - } + }); } /* diff --git a/bukkit/src/main/java/net/william278/husksync/player/BukkitPlayer.java b/bukkit/src/main/java/net/william278/husksync/player/BukkitPlayer.java index c15010af..2856289c 100644 --- a/bukkit/src/main/java/net/william278/husksync/player/BukkitPlayer.java +++ b/bukkit/src/main/java/net/william278/husksync/player/BukkitPlayer.java @@ -449,18 +449,13 @@ public class BukkitPlayer extends OnlineUser { @Override public void showMenu(@NotNull ItemEditorMenu menu) { BukkitSerializer.deserializeItemStackArray(menu.itemData.serializedItems).thenAccept(inventoryContents -> { - final Inventory inventory = Bukkit.createInventory(player, menu.slotCount, + final Inventory inventory = Bukkit.createInventory(player, menu.itemEditorMenuType.slotCount, BaseComponent.toLegacyText(menu.menuTitle.toComponent())); inventory.setContents(inventoryContents); Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> player.openInventory(inventory)); }); } - @Override - public void closeOpenInventories() { - Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), player::closeInventory); - } - @Override public void sendActionBar(@NotNull MineDown mineDown) { player.spigot().sendMessage(ChatMessageType.ACTION_BAR, mineDown.replace().toComponent()); diff --git a/common/src/main/java/net/william278/husksync/editor/DataEditor.java b/common/src/main/java/net/william278/husksync/editor/DataEditor.java index 1fa8d803..54ebbade 100644 --- a/common/src/main/java/net/william278/husksync/editor/DataEditor.java +++ b/common/src/main/java/net/william278/husksync/editor/DataEditor.java @@ -42,7 +42,6 @@ public class DataEditor { */ public CompletableFuture openItemEditorMenu(@NotNull OnlineUser user, @NotNull ItemEditorMenu itemEditorMenu) { - //user.closeOpenInventories(); this.openInventoryMenus.put(user.uuid, itemEditorMenu); return itemEditorMenu.showInventory(user); } @@ -55,7 +54,6 @@ public class DataEditor { */ public void closeInventoryMenu(@NotNull OnlineUser user, @NotNull ItemData itemData) { if (this.openInventoryMenus.containsKey(user.uuid)) { - System.out.println("Closing inventory menu for " + user.uuid); this.openInventoryMenus.get(user.uuid).closeInventory(itemData); } this.openInventoryMenus.remove(user.uuid); @@ -179,7 +177,8 @@ public class DataEditor { * @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); + public Optional getEditingInventoryData(@NotNull OnlineUser user) { + return this.openInventoryMenus.containsKey(user.uuid) ? Optional.of(this.openInventoryMenus.get(user.uuid)) + : Optional.empty(); } } diff --git a/common/src/main/java/net/william278/husksync/editor/ItemEditorMenu.java b/common/src/main/java/net/william278/husksync/editor/ItemEditorMenu.java index 2b862a42..b1c08de7 100644 --- a/common/src/main/java/net/william278/husksync/editor/ItemEditorMenu.java +++ b/common/src/main/java/net/william278/husksync/editor/ItemEditorMenu.java @@ -13,17 +13,17 @@ import java.util.concurrent.CompletableFuture; public class ItemEditorMenu { public final ItemData itemData; - public final int slotCount; + public final ItemEditorMenuType itemEditorMenuType; public final MineDown menuTitle; public boolean canEdit; private CompletableFuture inventoryDataCompletableFuture; - private ItemEditorMenu(@NotNull ItemData itemData, int slotCount, + private ItemEditorMenu(@NotNull ItemData itemData, ItemEditorMenuType itemEditorMenuType, @NotNull MineDown menuTitle, boolean canEdit) { this.itemData = itemData; this.menuTitle = menuTitle; - this.slotCount = slotCount; + this.itemEditorMenuType = itemEditorMenuType; this.canEdit = canEdit; } @@ -40,16 +40,16 @@ public class ItemEditorMenu { public static ItemEditorMenu createInventoryMenu(@NotNull ItemData itemData, @NotNull User dataOwner, @NotNull OnlineUser viewer, @NotNull Locales locales, boolean canEdit) { - return new ItemEditorMenu(itemData, 45, - locales.getLocale("inventory_viewer_menu_title", dataOwner.username).orElse(new MineDown("")), + return new ItemEditorMenu(itemData, ItemEditorMenuType.INVENTORY_VIEWER, + locales.getLocale(ItemEditorMenuType.INVENTORY_VIEWER.localeKey, dataOwner.username).orElse(new MineDown("")), viewer.hasPermission(Permission.COMMAND_INVENTORY_EDIT.node) && canEdit); } public static ItemEditorMenu createEnderChestMenu(@NotNull ItemData itemData, @NotNull User dataOwner, @NotNull OnlineUser viewer, @NotNull Locales locales, boolean canEdit) { - return new ItemEditorMenu(itemData, 27, - locales.getLocale("ender_chest_viewer_menu_title", dataOwner.username).orElse(new MineDown("")), + return new ItemEditorMenu(itemData, ItemEditorMenuType.ENDER_CHEST_VIEWER, + locales.getLocale(ItemEditorMenuType.ENDER_CHEST_VIEWER.localeKey, dataOwner.username).orElse(new MineDown("")), viewer.hasPermission(Permission.COMMAND_ENDER_CHEST_EDIT.node) && canEdit); } diff --git a/common/src/main/java/net/william278/husksync/editor/ItemEditorMenuType.java b/common/src/main/java/net/william278/husksync/editor/ItemEditorMenuType.java new file mode 100644 index 00000000..95f4b5e0 --- /dev/null +++ b/common/src/main/java/net/william278/husksync/editor/ItemEditorMenuType.java @@ -0,0 +1,16 @@ +package net.william278.husksync.editor; + +import org.jetbrains.annotations.NotNull; + +public enum ItemEditorMenuType { + INVENTORY_VIEWER(45, "inventory_viewer_menu_title"), + ENDER_CHEST_VIEWER(27, "ender_chest_viewer_menu_title"); + + public final int slotCount; + final String localeKey; + + ItemEditorMenuType(int slotCount, @NotNull String localeKey) { + this.slotCount = slotCount; + this.localeKey = localeKey; + } +} diff --git a/common/src/main/java/net/william278/husksync/listener/EventListener.java b/common/src/main/java/net/william278/husksync/listener/EventListener.java index 3e9187fa..3a483b7c 100644 --- a/common/src/main/java/net/william278/husksync/listener/EventListener.java +++ b/common/src/main/java/net/william278/husksync/listener/EventListener.java @@ -140,12 +140,12 @@ public abstract class EventListener { usersAwaitingSync.remove(user.uuid); } - public final void handleWorldSave(@NotNull List usersInWorld) { + public final void handleAsyncWorldSave(@NotNull List usersInWorld) { if (disabling || !plugin.getSettings().getBooleanValue(Settings.ConfigOption.SYNCHRONIZATION_SAVE_ON_WORLD_SAVE)) { return; } - CompletableFuture.runAsync(() -> usersInWorld.forEach(user -> - plugin.getDatabase().setUserData(user, user.getUserData().join(), DataSaveCause.WORLD_SAVE).join())); + usersInWorld.forEach(user -> plugin.getDatabase().setUserData(user, user.getUserData().join(), + DataSaveCause.WORLD_SAVE).join()); } public final void handlePluginDisable() { diff --git a/common/src/main/java/net/william278/husksync/player/OnlineUser.java b/common/src/main/java/net/william278/husksync/player/OnlineUser.java index 9b3f0906..4daed578 100644 --- a/common/src/main/java/net/william278/husksync/player/OnlineUser.java +++ b/common/src/main/java/net/william278/husksync/player/OnlineUser.java @@ -258,11 +258,6 @@ public abstract class OnlineUser extends User { */ public abstract void showMenu(@NotNull ItemEditorMenu menu); - /** - * Close inventories the player currently has open - */ - public abstract void closeOpenInventories(); - /** * Get the player's current {@link UserData} * diff --git a/common/src/test/java/net/william278/husksync/player/DummyPlayer.java b/common/src/test/java/net/william278/husksync/player/DummyPlayer.java index a22f922d..5ba1eba8 100644 --- a/common/src/test/java/net/william278/husksync/player/DummyPlayer.java +++ b/common/src/test/java/net/william278/husksync/player/DummyPlayer.java @@ -160,9 +160,4 @@ public class DummyPlayer extends OnlineUser { // do nothing } - @Override - public void closeOpenInventories() { - // do nothing - } - }