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.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);
}
});
}
}
});
}
/*

@ -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());

@ -42,7 +42,6 @@ public class DataEditor {
*/
public CompletableFuture<ItemData> 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<ItemEditorMenu> getEditingInventoryData(@NotNull OnlineUser user) {
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 final ItemData itemData;
public final int slotCount;
public final ItemEditorMenuType itemEditorMenuType;
public final MineDown menuTitle;
public boolean canEdit;
private CompletableFuture<ItemData> 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);
}

@ -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);
}
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)) {
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() {

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

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

Loading…
Cancel
Save