|
| 1 | +package com.skyblockexp.ezauction.storage.yaml; |
| 2 | + |
| 3 | +import org.bukkit.configuration.InvalidConfigurationException; |
| 4 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 5 | +import org.bukkit.inventory.ItemStack; |
| 6 | +import org.bukkit.util.io.BukkitObjectInputStream; |
| 7 | +import org.bukkit.util.io.BukkitObjectOutputStream; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.util.Base64; |
| 15 | + |
| 16 | +/** |
| 17 | + * Encodes and decodes {@link ItemStack} instances to and from a Base64 byte string |
| 18 | + * using {@link BukkitObjectOutputStream}. |
| 19 | + * |
| 20 | + * <p>Unlike storing items directly in a {@link org.bukkit.configuration.file.YamlConfiguration} |
| 21 | + * via {@code section.set("item", itemStack)}, this format is stored as a plain YAML string |
| 22 | + * scalar. Bukkit's {@code YamlConstructor} therefore does <em>not</em> attempt to |
| 23 | + * auto-deserialize it during file loading, which eliminates the |
| 24 | + * {@code Material cannot be null} {@code ERROR} log entries that occur when item data was |
| 25 | + * written on a Paper server and later loaded on a Spigot server (or vice-versa). |
| 26 | + * |
| 27 | + * <p>Requires a running Bukkit server (i.e. must not be called before {@code onEnable}). |
| 28 | + */ |
| 29 | +final class ItemStackSerializer { |
| 30 | + |
| 31 | + private ItemStackSerializer() {} |
| 32 | + |
| 33 | + /** |
| 34 | + * Serialises {@code item} to a Base64 string. |
| 35 | + * |
| 36 | + * @param item the item to serialise; must not be {@code null} |
| 37 | + * @return a non-null, non-empty Base64 string |
| 38 | + * @throws IOException if the item cannot be serialised |
| 39 | + */ |
| 40 | + static String serialize(ItemStack item) throws IOException { |
| 41 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 42 | + try (BukkitObjectOutputStream out = new BukkitObjectOutputStream(bytes)) { |
| 43 | + out.writeObject(item); |
| 44 | + } |
| 45 | + return Base64.getEncoder().encodeToString(bytes.toByteArray()); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Deserialises an {@link ItemStack} from a Base64 string produced by |
| 50 | + * {@link #serialize(ItemStack)}. |
| 51 | + * |
| 52 | + * @param base64 the Base64 string to deserialise |
| 53 | + * @return the restored {@link ItemStack} |
| 54 | + * @throws IOException if the byte data cannot be read |
| 55 | + * @throws ClassNotFoundException if the item class cannot be resolved |
| 56 | + */ |
| 57 | + static ItemStack deserialize(String base64) throws IOException, ClassNotFoundException { |
| 58 | + byte[] data = Base64.getDecoder().decode(base64); |
| 59 | + try (BukkitObjectInputStream in = new BukkitObjectInputStream(new ByteArrayInputStream(data))) { |
| 60 | + return (ItemStack) in.readObject(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Loads a YAML {@link File} safely by stripping |
| 66 | + * {@code ==: org.bukkit.inventory.ItemStack} type-tag lines from the raw text |
| 67 | + * before handing it to {@link YamlConfiguration#loadFromString}. |
| 68 | + * |
| 69 | + * <p>Bukkit's {@code YamlConstructor} auto-deserializes any map node tagged |
| 70 | + * {@code ==: org.bukkit.inventory.ItemStack} during file loading. On Spigot this |
| 71 | + * fails with {@code Material cannot be null} whenever the data was originally |
| 72 | + * written by a Paper 1.21+ server. Removing those lines turns the node into a |
| 73 | + * plain map; {@code getItemStack()} then returns {@code null} (handled gracefully |
| 74 | + * by the callers), and no ERROR is logged. |
| 75 | + * |
| 76 | + * @param file the YAML file to load; need not exist |
| 77 | + * @return a populated {@link YamlConfiguration}, or an empty one if the file does |
| 78 | + * not exist or cannot be parsed |
| 79 | + */ |
| 80 | + static YamlConfiguration loadSafe(File file) { |
| 81 | + YamlConfiguration config = new YamlConfiguration(); |
| 82 | + if (file == null || !file.exists()) { |
| 83 | + return config; |
| 84 | + } |
| 85 | + try { |
| 86 | + String raw = Files.readString(file.toPath()); |
| 87 | + // Replace Bukkit YAML type-tag lines with a YAML comment so the |
| 88 | + // YamlConstructor never attempts platform-specific deserialization. |
| 89 | + raw = raw.replace("==: org.bukkit.inventory.ItemStack", "# legacy-item-tag stripped"); |
| 90 | + config.loadFromString(raw); |
| 91 | + } catch (IOException | InvalidConfigurationException ignored) { |
| 92 | + // Return the empty config; callers treat missing sections as empty data. |
| 93 | + } |
| 94 | + return config; |
| 95 | + } |
| 96 | +} |
0 commit comments