diff --git a/src/main/java/de/iani/treasurechest/database/TreasureChestDatabase.java b/src/main/java/de/iani/treasurechest/database/TreasureChestDatabase.java index 9e82f9e..b83e7c9 100644 --- a/src/main/java/de/iani/treasurechest/database/TreasureChestDatabase.java +++ b/src/main/java/de/iani/treasurechest/database/TreasureChestDatabase.java @@ -74,13 +74,13 @@ long time = System.currentTimeMillis(); smt.setLong(2, time); YamlConfiguration conf = new YamlConfiguration(); - conf.set("display", item.getDisplayItem()); + conf.set("display", item.getDisplayItem() == null ? null : item.getDisplayItem().serializeAsBytes()); ConfigurationSection itemsSection = conf.createSection("items"); ItemStack[] items = item.getPriceItems(); if (items != null) { for (int i = 0; i < items.length; i++) { ItemStack stack = items[i]; - itemsSection.set(Integer.toString(i), stack); + itemsSection.set(Integer.toString(i), stack == null ? null : stack.serializeAsBytes()); } } conf.set("money", item.getPriceMoney()); @@ -104,13 +104,13 @@ PreparedStatement smt = sqlConnection.getOrCreateStatement(updateContent); YamlConfiguration conf = new YamlConfiguration(); - conf.set("display", item.getDisplayItem()); + conf.set("display", item.getDisplayItem() == null ? null : item.getDisplayItem().serializeAsBytes()); ConfigurationSection itemsSection = conf.createSection("items"); ItemStack[] items = item.getPriceItems(); if (items != null) { for (int i = 0; i < items.length; i++) { ItemStack stack = items[i]; - itemsSection.set(Integer.toString(i), stack); + itemsSection.set(Integer.toString(i), stack == null ? null : stack.serializeAsBytes()); } } conf.set("money", item.getPriceMoney()); @@ -138,7 +138,7 @@ plugin.getLogger().log(Level.WARNING, "Could not load treasure chest item " + id + " for player " + owner, e); continue; } - ItemStack displayItem = conf.getItemStack("display"); + ItemStack displayItem = getStack(conf, "display"); if (displayItem == null || displayItem.getAmount() == 0 || displayItem.getType() == Material.AIR) { plugin.getLogger().log(Level.WARNING, "No display item for item " + id + " for player " + owner); continue; @@ -146,7 +146,7 @@ ArrayList stacks = new ArrayList<>(); ConfigurationSection itemsSection = conf.getConfigurationSection("items"); for (String key : itemsSection.getKeys(false)) { - ItemStack stack = itemsSection.getItemStack(key); + ItemStack stack = getStack(itemsSection, key); if (stack != null && stack.getAmount() > 0 && stack.getType() != Material.AIR) { stacks.add(stack); } @@ -160,6 +160,11 @@ }); } + private ItemStack getStack(ConfigurationSection conf, String key) { + Object object = conf.get(key); + return object instanceof ItemStack stack ? stack : object instanceof byte[] bytes ? ItemStack.deserializeBytes(bytes) : null; + } + public ArrayList getAllItems() throws SQLException { return this.connection.runCommands((connection, sqlConnection) -> { ArrayList result = new ArrayList<>(); @@ -176,7 +181,7 @@ plugin.getLogger().log(Level.WARNING, "Could not load treasure chest item " + id, e); continue; } - ItemStack displayItem = conf.getItemStack("display"); + ItemStack displayItem = getStack(conf, "display"); if (displayItem == null || displayItem.getAmount() == 0 || displayItem.getType() == Material.AIR) { plugin.getLogger().log(Level.WARNING, "No display item for item " + id); continue; @@ -184,7 +189,7 @@ ArrayList stacks = new ArrayList<>(); ConfigurationSection itemsSection = conf.getConfigurationSection("items"); for (String key : itemsSection.getKeys(false)) { - ItemStack stack = itemsSection.getItemStack(key); + ItemStack stack = getStack(itemsSection, key); if (stack != null && stack.getAmount() > 0 && stack.getType() != Material.AIR) { stacks.add(stack); }