Skip to content

Commit

Permalink
Non existing items now don't abort the deserialization
Browse files Browse the repository at this point in the history
- Fixes #18
  • Loading branch information
BrainStone committed Jun 18, 2017
1 parent 22c2c9b commit 922a315
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,10 +47,11 @@ public static List<DataView> serializeInventory(Inventory inventory) {
return slots;
}

public static void deserializeInventory(List<DataView> slots, Inventory inventory) {
public static boolean deserializeInventory(List<DataView> slots, Inventory inventory) {
Map<Integer, ItemStack> stacks = new HashMap<>();
int i;
ItemStack stack;
boolean fail = false;

for (DataView slot : slots) {
i = slot.getInt(SLOT).get();
Expand All @@ -62,13 +64,21 @@ public static void deserializeInventory(List<DataView> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 922a315

Please sign in to comment.