Fix ender chest command too

feat/data-edit-commands
William 2 years ago
parent bc9d31abc8
commit 54553069bf

@ -4,6 +4,7 @@ import net.william278.husksync.BukkitHuskSync;
import net.william278.husksync.data.BukkitSerializer; import net.william278.husksync.data.BukkitSerializer;
import net.william278.husksync.data.DataSerializationException; import net.william278.husksync.data.DataSerializationException;
import net.william278.husksync.data.ItemData; import net.william278.husksync.data.ItemData;
import net.william278.husksync.editor.ItemEditorMenuType;
import net.william278.husksync.player.BukkitPlayer; import net.william278.husksync.player.BukkitPlayer;
import net.william278.husksync.player.OnlineUser; import net.william278.husksync.player.OnlineUser;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -24,6 +25,7 @@ import org.bukkit.event.world.WorldSaveEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -46,25 +48,29 @@ public class BukkitEventListener extends EventListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onWorldSave(@NotNull WorldSaveEvent event) { public void onWorldSave(@NotNull WorldSaveEvent event) {
super.handleWorldSave(event.getWorld().getPlayers().stream().map(BukkitPlayer::adapt) CompletableFuture.runAsync(() -> super.handleAsyncWorldSave(event.getWorld().getPlayers().stream()
.collect(Collectors.toList())); .map(BukkitPlayer::adapt).collect(Collectors.toList())));
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onInventoryClose(@NotNull InventoryCloseEvent event) { public void onInventoryClose(@NotNull InventoryCloseEvent event) {
CompletableFuture.runAsync(() -> {
if (event.getPlayer() instanceof Player player) { if (event.getPlayer() instanceof Player player) {
final OnlineUser user = BukkitPlayer.adapt(player); final OnlineUser user = BukkitPlayer.adapt(player);
if (plugin.getDataEditor().isEditingInventoryData(user)) { plugin.getDataEditor().getEditingInventoryData(user).ifPresent(menu -> {
try { try {
BukkitSerializer.serializeItemStackArray(Arrays.copyOf(event.getInventory().getContents(), BukkitSerializer.serializeItemStackArray(Arrays.copyOf(event.getInventory().getContents(),
event.getPlayer().getInventory().getSize())).thenAccept( menu.itemEditorMenuType == ItemEditorMenuType.INVENTORY_VIEWER
? player.getInventory().getSize()
: player.getEnderChest().getSize())).thenAccept(
serializedInventory -> super.handleMenuClose(user, new ItemData(serializedInventory))); serializedInventory -> super.handleMenuClose(user, new ItemData(serializedInventory)));
} catch (DataSerializationException e) { } catch (DataSerializationException e) {
plugin.getLoggingAdapter().log(Level.SEVERE, plugin.getLoggingAdapter().log(Level.SEVERE,
"Failed to serialize inventory data during menu close", e); "Failed to serialize inventory data during menu close", e);
} }
});
} }
} });
} }
/* /*

@ -449,18 +449,13 @@ public class BukkitPlayer extends OnlineUser {
@Override @Override
public void showMenu(@NotNull ItemEditorMenu menu) { public void showMenu(@NotNull ItemEditorMenu menu) {
BukkitSerializer.deserializeItemStackArray(menu.itemData.serializedItems).thenAccept(inventoryContents -> { 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())); BaseComponent.toLegacyText(menu.menuTitle.toComponent()));
inventory.setContents(inventoryContents); inventory.setContents(inventoryContents);
Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> player.openInventory(inventory)); Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> player.openInventory(inventory));
}); });
} }
@Override
public void closeOpenInventories() {
Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), player::closeInventory);
}
@Override @Override
public void sendActionBar(@NotNull MineDown mineDown) { public void sendActionBar(@NotNull MineDown mineDown) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, mineDown.replace().toComponent()); player.spigot().sendMessage(ChatMessageType.ACTION_BAR, mineDown.replace().toComponent());

@ -42,7 +42,6 @@ public class DataEditor {
*/ */
public CompletableFuture<ItemData> openItemEditorMenu(@NotNull OnlineUser user, public CompletableFuture<ItemData> openItemEditorMenu(@NotNull OnlineUser user,
@NotNull ItemEditorMenu itemEditorMenu) { @NotNull ItemEditorMenu itemEditorMenu) {
//user.closeOpenInventories();
this.openInventoryMenus.put(user.uuid, itemEditorMenu); this.openInventoryMenus.put(user.uuid, itemEditorMenu);
return itemEditorMenu.showInventory(user); return itemEditorMenu.showInventory(user);
} }
@ -55,7 +54,6 @@ public class DataEditor {
*/ */
public void closeInventoryMenu(@NotNull OnlineUser user, @NotNull ItemData itemData) { public void closeInventoryMenu(@NotNull OnlineUser user, @NotNull ItemData itemData) {
if (this.openInventoryMenus.containsKey(user.uuid)) { if (this.openInventoryMenus.containsKey(user.uuid)) {
System.out.println("Closing inventory menu for " + user.uuid);
this.openInventoryMenus.get(user.uuid).closeInventory(itemData); this.openInventoryMenus.get(user.uuid).closeInventory(itemData);
} }
this.openInventoryMenus.remove(user.uuid); this.openInventoryMenus.remove(user.uuid);
@ -179,7 +177,8 @@ public class DataEditor {
* @param user {@link OnlineUser} to check * @param user {@link OnlineUser} to check
* @return {@code true} if the user has an inventory editor open; {@code false} otherwise * @return {@code true} if the user has an inventory editor open; {@code false} otherwise
*/ */
public boolean isEditingInventoryData(@NotNull OnlineUser user) { public Optional<ItemEditorMenu> getEditingInventoryData(@NotNull OnlineUser user) {
return this.openInventoryMenus.containsKey(user.uuid); return this.openInventoryMenus.containsKey(user.uuid) ? Optional.of(this.openInventoryMenus.get(user.uuid))
: Optional.empty();
} }
} }

@ -13,17 +13,17 @@ import java.util.concurrent.CompletableFuture;
public class ItemEditorMenu { public class ItemEditorMenu {
public final ItemData itemData; public final ItemData itemData;
public final int slotCount; public final ItemEditorMenuType itemEditorMenuType;
public final MineDown menuTitle; public final MineDown menuTitle;
public boolean canEdit; public boolean canEdit;
private CompletableFuture<ItemData> inventoryDataCompletableFuture; private CompletableFuture<ItemData> inventoryDataCompletableFuture;
private ItemEditorMenu(@NotNull ItemData itemData, int slotCount, private ItemEditorMenu(@NotNull ItemData itemData, ItemEditorMenuType itemEditorMenuType,
@NotNull MineDown menuTitle, boolean canEdit) { @NotNull MineDown menuTitle, boolean canEdit) {
this.itemData = itemData; this.itemData = itemData;
this.menuTitle = menuTitle; this.menuTitle = menuTitle;
this.slotCount = slotCount; this.itemEditorMenuType = itemEditorMenuType;
this.canEdit = canEdit; this.canEdit = canEdit;
} }
@ -40,16 +40,16 @@ public class ItemEditorMenu {
public static ItemEditorMenu createInventoryMenu(@NotNull ItemData itemData, @NotNull User dataOwner, public static ItemEditorMenu createInventoryMenu(@NotNull ItemData itemData, @NotNull User dataOwner,
@NotNull OnlineUser viewer, @NotNull Locales locales, @NotNull OnlineUser viewer, @NotNull Locales locales,
boolean canEdit) { boolean canEdit) {
return new ItemEditorMenu(itemData, 45, return new ItemEditorMenu(itemData, ItemEditorMenuType.INVENTORY_VIEWER,
locales.getLocale("inventory_viewer_menu_title", dataOwner.username).orElse(new MineDown("")), locales.getLocale(ItemEditorMenuType.INVENTORY_VIEWER.localeKey, dataOwner.username).orElse(new MineDown("")),
viewer.hasPermission(Permission.COMMAND_INVENTORY_EDIT.node) && canEdit); viewer.hasPermission(Permission.COMMAND_INVENTORY_EDIT.node) && canEdit);
} }
public static ItemEditorMenu createEnderChestMenu(@NotNull ItemData itemData, @NotNull User dataOwner, public static ItemEditorMenu createEnderChestMenu(@NotNull ItemData itemData, @NotNull User dataOwner,
@NotNull OnlineUser viewer, @NotNull Locales locales, @NotNull OnlineUser viewer, @NotNull Locales locales,
boolean canEdit) { boolean canEdit) {
return new ItemEditorMenu(itemData, 27, return new ItemEditorMenu(itemData, ItemEditorMenuType.ENDER_CHEST_VIEWER,
locales.getLocale("ender_chest_viewer_menu_title", dataOwner.username).orElse(new MineDown("")), locales.getLocale(ItemEditorMenuType.ENDER_CHEST_VIEWER.localeKey, dataOwner.username).orElse(new MineDown("")),
viewer.hasPermission(Permission.COMMAND_ENDER_CHEST_EDIT.node) && canEdit); viewer.hasPermission(Permission.COMMAND_ENDER_CHEST_EDIT.node) && canEdit);
} }

@ -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;
}
}

@ -140,12 +140,12 @@ public abstract class EventListener {
usersAwaitingSync.remove(user.uuid); usersAwaitingSync.remove(user.uuid);
} }
public final void handleWorldSave(@NotNull List<OnlineUser> usersInWorld) { public final void handleAsyncWorldSave(@NotNull List<OnlineUser> usersInWorld) {
if (disabling || !plugin.getSettings().getBooleanValue(Settings.ConfigOption.SYNCHRONIZATION_SAVE_ON_WORLD_SAVE)) { if (disabling || !plugin.getSettings().getBooleanValue(Settings.ConfigOption.SYNCHRONIZATION_SAVE_ON_WORLD_SAVE)) {
return; return;
} }
CompletableFuture.runAsync(() -> usersInWorld.forEach(user -> usersInWorld.forEach(user -> plugin.getDatabase().setUserData(user, user.getUserData().join(),
plugin.getDatabase().setUserData(user, user.getUserData().join(), DataSaveCause.WORLD_SAVE).join())); DataSaveCause.WORLD_SAVE).join());
} }
public final void handlePluginDisable() { public final void handlePluginDisable() {

@ -258,11 +258,6 @@ public abstract class OnlineUser extends User {
*/ */
public abstract void showMenu(@NotNull ItemEditorMenu menu); 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} * Get the player's current {@link UserData}
* *

@ -160,9 +160,4 @@ public class DummyPlayer extends OnlineUser {
// do nothing // do nothing
} }
@Override
public void closeOpenInventories() {
// do nothing
}
} }

Loading…
Cancel
Save