diff --git a/AuxProtect_Core/src/main/java/dev/heliosares/auxprotect/utils/InvSerialization.java b/AuxProtect_Core/src/main/java/dev/heliosares/auxprotect/utils/InvSerialization.java index c558c40..135ffe3 100644 --- a/AuxProtect_Core/src/main/java/dev/heliosares/auxprotect/utils/InvSerialization.java +++ b/AuxProtect_Core/src/main/java/dev/heliosares/auxprotect/utils/InvSerialization.java @@ -1,5 +1,6 @@ package dev.heliosares.auxprotect.utils; +import dev.heliosares.auxprotect.api.AuxProtectAPI; import dev.heliosares.auxprotect.spigot.AuxProtectSpigot; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -13,6 +14,8 @@ import org.bukkit.util.io.BukkitObjectOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; public class InvSerialization { @@ -114,10 +117,10 @@ public class InvSerialization { } return playerToByteArray(new PlayerInventoryRecord(player.getInventory().getStorageContents(), player.getInventory().getArmorContents(), player.getInventory().getExtraContents(), - player.getEnderChest().getContents(), Experience.getTotalExp(player))); + player.getEnderChest().getContents(), Experience.getTotalExp(player)), player.getName()); } - public static byte[] playerToByteArray(PlayerInventoryRecord record) throws IOException { + public static byte[] playerToByteArray(PlayerInventoryRecord record, String playerName) throws IOException { if (record == null) { return null; } @@ -126,22 +129,22 @@ public class InvSerialization { stream.writeInt(record.storage().length); for (ItemStack item : record.storage()) { - stream.writeObject(item); + write(stream, item, playerName); } stream.writeInt(record.armor().length); for (ItemStack item : record.armor()) { - stream.writeObject(item); + write(stream, item, playerName); } stream.writeInt(record.extra().length); for (ItemStack item : record.extra()) { - stream.writeObject(item); + write(stream, item, playerName); } stream.writeInt(record.ender().length); for (ItemStack item : record.ender()) { - stream.writeObject(item); + write(stream, item, playerName); } stream.writeInt(record.exp()); @@ -151,6 +154,22 @@ public class InvSerialization { } } + private static final Map spamMap = new HashMap<>(); + + private static void write(BukkitObjectOutputStream stream, ItemStack item, String playerName) throws IOException { + try { + stream.writeObject(item); + } catch (IOException e) { + String msg = "Failed to serialize " + item + " for " + playerName + ". This is not an issue with AuxProtect, it is Bukkit failing to serialize the item correctly. The item will be logged as an empty slot. (" + e.getMessage() + ")"; + spamMap.values().removeIf(l -> System.currentTimeMillis() - l > 60000L); + if (!spamMap.containsKey(msg)) { + spamMap.put(msg, System.currentTimeMillis()); + AuxProtectAPI.warning(msg); + } + stream.writeObject(null); + } + } + public static void debug(byte[] bytes) { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); System.out.println("Debug Byte[] Dump:"); diff --git a/pom.xml b/pom.xml index d89bb94..c826b1b 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,6 @@ ${env.USERPROFILE}/eclipse-workspace/jars UTF8 - 1.3.2 + 1.3.3-pre1 \ No newline at end of file