From 922a315885bcd39900dc648381bc092efa7be3e4 Mon Sep 17 00:00:00 2001 From: BrainStone Date: Sun, 18 Jun 2017 17:04:30 +0200 Subject: [PATCH] Non existing items now don't abort the deserialization - Fixes #18 --- .../util/serializer/InventorySerializer.java | 14 ++++++++++++-- .../invsync/util/serializer/PlayerSerializer.java | 9 ++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/world/jnc/invsync/util/serializer/InventorySerializer.java b/src/main/java/world/jnc/invsync/util/serializer/InventorySerializer.java index b1832b9..2030ba6 100644 --- a/src/main/java/world/jnc/invsync/util/serializer/InventorySerializer.java +++ b/src/main/java/world/jnc/invsync/util/serializer/InventorySerializer.java @@ -4,6 +4,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import java.util.Optional; import org.spongepowered.api.data.DataContainer; @@ -46,10 +47,11 @@ public static List serializeInventory(Inventory inventory) { return slots; } - public static void deserializeInventory(List slots, Inventory inventory) { + public static boolean deserializeInventory(List slots, Inventory inventory) { Map stacks = new HashMap<>(); int i; ItemStack stack; + boolean fail = false; for (DataView slot : slots) { i = slot.getInt(SLOT).get(); @@ -62,13 +64,21 @@ public static void deserializeInventory(List slots, Inventory inventor for (Inventory slot : inventory.slots()) { if (stacks.containsKey(i)) { - slot.set(stacks.get(i)); + try { + slot.set(stacks.get(i)); + } catch (NoSuchElementException e) { + slot.clear(); + + fail = true; + } } else { slot.clear(); } ++i; } + + return fail; } private static DataView serializeItemStack(ItemStack item) { diff --git a/src/main/java/world/jnc/invsync/util/serializer/PlayerSerializer.java b/src/main/java/world/jnc/invsync/util/serializer/PlayerSerializer.java index 0c92192..48d75c0 100644 --- a/src/main/java/world/jnc/invsync/util/serializer/PlayerSerializer.java +++ b/src/main/java/world/jnc/invsync/util/serializer/PlayerSerializer.java @@ -130,11 +130,18 @@ public static void deserializePlayer(Player player, byte[] data) throws IOExcept if (inventory.isPresent() && Config.Values.Synchronize.getEnableInventory() && player.hasPermission(PermissionRegistry.SYNC_INVENTORY)) { - InventorySerializer.deserializeInventory(inventory.get(), player.getInventory()); + boolean fail = InventorySerializer.deserializeInventory(inventory.get(), player.getInventory()); if (selectedSlot.isPresent()) { getHotbar(player).setSelectedSlotIndex(selectedSlot.get()); } + + if (fail) { + InventorySync.getLogger().error("Could not load inventory of player " + + DataSource.getPlayerString(player) + " because there where unknown item."); + InventorySync.getLogger().warn( + "Please make sure you are using the same mods on all servers you are synchronizing with."); + } } if (enderChest.isPresent() && Config.Values.Synchronize.getEnableEnderChest() && player.hasPermission(PermissionRegistry.SYNC_ENDER_CHEST)) {