Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-7185

Can't catch item deserialization errors

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Minor Minor
    • None
    • None
    • Windows 10 Professional 22H2

      Intel Core I7 4770K

      16Gb DDR3 1600Mhz

    • 3607-Spigot-6198b5a-e7aab54 (MC: 1.19.2)
    • CustomFishing 1.2.8
    • Yes

      If a skull is corrupted and saved into the database, when trying to reload the skull and deserializing it, it will throw an error. I tried to catch the error to handle it, but it seems that it does not propagate to the call of the deserialization and so makes the error not catchable.

      Error: [[05:59:19 ERROR]: org.bukkit.configuration.serialization.ConfigurationSerializa - Pastebin.com

      Here is the code to retrieve the data and decode and then deserialize. It works with any known good item (including skulls).

      public static ItemStack[] itemStackArrayFromBase64(String data) throws IOException {
         try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
            BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
            ItemStack[] items = new ItemStack[dataInput.readInt()];
      
            for (int Index = 0; Index < items.length; Index++) {
               @SuppressWarnings("unchecked")
               Map<String, Object> stack = (Map<String, Object>) dataInput.readObject();
               try
               {
                  items[Index] = stack != null ? ItemStack.deserialize(stack) : new ItemStack(Material.AIR);
               }
               catch (Exception e)
               {
                  items[Index] = new ItemStack(Material.PLAYER_HEAD);
                  Bukkit.getLogger().warning("An item has been replaced by a player head because it couldn't be deserialized.");
                  Bukkit.getLogger().warning("Item that couldn't be deserialized had the following data: " + (stack != null ? stack.toString() : "null"));
               }
            }
      
            dataInput.close();
            return items;
         } catch (ClassNotFoundException e) {
            throw new IOException("Unable to decode class type.", e);
         }
      } 

            Unassigned Unassigned
            ZeTioZ Donato Gentile
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: