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 42607eab..85e836c4 100644 --- a/bukkit/src/main/java/net/william278/husksync/player/BukkitPlayer.java +++ b/bukkit/src/main/java/net/william278/husksync/player/BukkitPlayer.java @@ -573,45 +573,45 @@ public class BukkitPlayer extends OnlineUser { // Deserialize the item data to be shown and show it in a triumph GUI BukkitSerializer.deserializeItemStackArray(itemData.serializedItems).thenAccept(items -> { - try { - // Build the GUI and populate with items - final int itemCount = items.length; - final StorageBuilder guiBuilder = Gui.storage() - .title(title.toComponent()) - .rows(Math.max(minimumRows, (int) Math.ceil(itemCount / 9.0))) - .disableAllInteractions() - .enableOtherActions(); - final StorageGui gui = editable ? guiBuilder.enableAllInteractions().create() : guiBuilder.create(); - for (int i = 0; i < itemCount; i++) { - if (items[i] != null) { - gui.getInventory().setItem(i, items[i]); - } + // Build the GUI and populate with items + final int itemCount = items.length; + final StorageBuilder guiBuilder = Gui.storage() + .title(title.toComponent()) + .rows(Math.max(minimumRows, (int) Math.ceil(itemCount / 9.0))) + .disableAllInteractions() + .enableOtherActions(); + final StorageGui gui = editable ? guiBuilder.enableAllInteractions().create() : guiBuilder.create(); + for (int i = 0; i < itemCount; i++) { + if (items[i] != null) { + gui.getInventory().setItem(i, items[i]); + } + } + + // Complete the future with updated data (if editable) when the GUI is closed + gui.setCloseGuiAction(event -> { + if (!editable) { + updatedData.complete(Optional.empty()); + return; } - // Complete the future with updated data (if editable) when the GUI is closed - gui.setCloseGuiAction(event -> { - if (!editable) { + // Get and save the updated items + final ItemStack[] updatedItems = Arrays.copyOf(event.getPlayer().getOpenInventory() + .getTopInventory().getContents().clone(), itemCount); + BukkitSerializer.serializeItemStackArray(updatedItems).thenAccept(serializedItems -> { + if (serializedItems.equals(itemData.serializedItems)) { updatedData.complete(Optional.empty()); return; } - - // Get and save the updated items - final ItemStack[] updatedItems = Arrays.copyOf(event.getPlayer().getOpenInventory() - .getTopInventory().getContents().clone(), itemCount); - BukkitSerializer.serializeItemStackArray(updatedItems).thenAccept(serializedItems -> { - if (serializedItems.equals(itemData.serializedItems)) { - updatedData.complete(Optional.empty()); - return; - } - updatedData.complete(Optional.of(new ItemData(serializedItems))); - }); + updatedData.complete(Optional.of(new ItemData(serializedItems))); }); + }); - // Display the GUI (synchronously; on the main server thread) - Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> gui.open(player)); - } catch (Exception e) { - e.printStackTrace(); - } + // Display the GUI (synchronously; on the main server thread) + Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> gui.open(player)); + }).exceptionally(throwable -> { + // Handle exceptions + updatedData.completeExceptionally(throwable); + return null; }); return updatedData; } diff --git a/bukkit/src/main/java/net/william278/husksync/util/BukkitLogger.java b/bukkit/src/main/java/net/william278/husksync/util/BukkitLogger.java index c87a5673..80300254 100644 --- a/bukkit/src/main/java/net/william278/husksync/util/BukkitLogger.java +++ b/bukkit/src/main/java/net/william278/husksync/util/BukkitLogger.java @@ -13,7 +13,7 @@ public class BukkitLogger extends Logger { } @Override - public void log(@NotNull Level level, @NotNull String message, @NotNull Exception e) { + public void log(@NotNull Level level, @NotNull String message, @NotNull Throwable e) { logger.log(level, message, e); } diff --git a/common/src/main/java/net/william278/husksync/command/EnderChestCommand.java b/common/src/main/java/net/william278/husksync/command/EnderChestCommand.java index b1c2a3fb..239430c0 100644 --- a/common/src/main/java/net/william278/husksync/command/EnderChestCommand.java +++ b/common/src/main/java/net/william278/husksync/command/EnderChestCommand.java @@ -12,8 +12,10 @@ import org.jetbrains.annotations.NotNull; import java.text.SimpleDateFormat; import java.util.List; +import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.logging.Level; import java.util.stream.Collectors; public class EnderChestCommand extends CommandBase implements TabCompletable { @@ -67,28 +69,34 @@ public class EnderChestCommand extends CommandBase implements TabCompletable { // Show inventory menu player.showMenu(itemData, allowEdit, 3, plugin.getLocales() - .getLocale("ender_chest_viewer_menu_title", dataOwner.username) - .orElse(new MineDown("Ender Chest Viewer"))).thenAccept(dataOnClose -> { - if (dataOnClose.isEmpty() || !allowEdit) { - return; - } + .getLocale("ender_chest_viewer_menu_title", dataOwner.username) + .orElse(new MineDown("Ender Chest Viewer"))) + .exceptionally(throwable -> { + plugin.getLoggingAdapter().log(Level.WARNING, "Exception displaying inventory menu to " + player.username, throwable); + return Optional.empty(); + }) + .thenAccept(dataOnClose -> { + if (dataOnClose.isEmpty() || !allowEdit) { + return; + } - // Create the updated data - final UserDataBuilder builder = UserData.builder(plugin.getMinecraftVersion()); - data.getStatus().ifPresent(builder::setStatus); - data.getAdvancements().ifPresent(builder::setAdvancements); - data.getLocation().ifPresent(builder::setLocation); - data.getPersistentDataContainer().ifPresent(builder::setPersistentDataContainer); - data.getStatistics().ifPresent(builder::setStatistics); - data.getPotionEffects().ifPresent(builder::setPotionEffects); - data.getInventory().ifPresent(builder::setInventory); - builder.setEnderChest(dataOnClose.get()); + // Create the updated data + final UserDataBuilder builder = UserData.builder(plugin.getMinecraftVersion()); + data.getStatus().ifPresent(builder::setStatus); + data.getAdvancements().ifPresent(builder::setAdvancements); + data.getLocation().ifPresent(builder::setLocation); + data.getPersistentDataContainer().ifPresent(builder::setPersistentDataContainer); + data.getStatistics().ifPresent(builder::setStatistics); + data.getPotionEffects().ifPresent(builder::setPotionEffects); + data.getInventory().ifPresent(builder::setInventory); + builder.setEnderChest(dataOnClose.get()); - // Set the updated data - final UserData updatedUserData = builder.build(); - plugin.getDatabase().setUserData(dataOwner, updatedUserData, DataSaveCause.INVENTORY_COMMAND).join(); - plugin.getRedisManager().sendUserDataUpdate(dataOwner, updatedUserData).join(); - }); + // Set the updated data + final UserData updatedUserData = builder.build(); + plugin.getDatabase() + .setUserData(dataOwner, updatedUserData, DataSaveCause.INVENTORY_COMMAND) + .thenRun(() -> plugin.getRedisManager().sendUserDataUpdate(dataOwner, updatedUserData)); + }); }); }); } diff --git a/common/src/main/java/net/william278/husksync/command/InventoryCommand.java b/common/src/main/java/net/william278/husksync/command/InventoryCommand.java index d3fab93d..acb23769 100644 --- a/common/src/main/java/net/william278/husksync/command/InventoryCommand.java +++ b/common/src/main/java/net/william278/husksync/command/InventoryCommand.java @@ -12,8 +12,10 @@ import org.jetbrains.annotations.NotNull; import java.text.SimpleDateFormat; import java.util.List; +import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.logging.Level; import java.util.stream.Collectors; public class InventoryCommand extends CommandBase implements TabCompletable { @@ -69,13 +71,15 @@ public class InventoryCommand extends CommandBase implements TabCompletable { player.showMenu(itemData, allowEdit, 5, plugin.getLocales() .getLocale("inventory_viewer_menu_title", dataOwner.username) .orElse(new MineDown("Inventory Viewer"))) + .exceptionally(throwable -> { + plugin.getLoggingAdapter().log(Level.WARNING, "Exception displaying inventory menu to " + player.username, throwable); + return Optional.empty(); + }) .thenAccept(dataOnClose -> { if (dataOnClose.isEmpty() || !allowEdit) { return; } - plugin.getLoggingAdapter().debug("Inventory data changed, updating user, etc!"); - // Create the updated data final UserDataBuilder builder = UserData.builder(plugin.getMinecraftVersion()); data.getStatus().ifPresent(builder::setStatus); @@ -89,8 +93,9 @@ public class InventoryCommand extends CommandBase implements TabCompletable { // Set the updated data final UserData updatedUserData = builder.build(); - plugin.getDatabase().setUserData(dataOwner, updatedUserData, DataSaveCause.INVENTORY_COMMAND).join(); - plugin.getRedisManager().sendUserDataUpdate(dataOwner, updatedUserData).join(); + plugin.getDatabase() + .setUserData(dataOwner, updatedUserData, DataSaveCause.INVENTORY_COMMAND) + .thenRun(() -> plugin.getRedisManager().sendUserDataUpdate(dataOwner, updatedUserData)); }); }); }); diff --git a/common/src/main/java/net/william278/husksync/util/Logger.java b/common/src/main/java/net/william278/husksync/util/Logger.java index d13546cc..400973f0 100644 --- a/common/src/main/java/net/william278/husksync/util/Logger.java +++ b/common/src/main/java/net/william278/husksync/util/Logger.java @@ -11,7 +11,7 @@ public abstract class Logger { private boolean debug; - public abstract void log(@NotNull Level level, @NotNull String message, @NotNull Exception e); + public abstract void log(@NotNull Level level, @NotNull String message, @NotNull Throwable e); public abstract void log(@NotNull Level level, @NotNull String message); diff --git a/common/src/test/java/net/william278/husksync/logger/DummyLogger.java b/common/src/test/java/net/william278/husksync/logger/DummyLogger.java index 25ad9f63..f1ea2c31 100644 --- a/common/src/test/java/net/william278/husksync/logger/DummyLogger.java +++ b/common/src/test/java/net/william278/husksync/logger/DummyLogger.java @@ -11,7 +11,7 @@ public class DummyLogger extends Logger { } @Override - public void log(@NotNull Level level, @NotNull String message, @NotNull Exception e) { + public void log(@NotNull Level level, @NotNull String message, @NotNull Throwable e) { System.out.println(level.getName() + ": " + message); e.printStackTrace(); }