From 977a3863668d112abea6de3c0dd9e840fbc5afbb Mon Sep 17 00:00:00 2001 From: PikaMug <2267126+PikaMug@users.noreply.github.com> Date: Wed, 1 Nov 2023 18:31:37 -0400 Subject: [PATCH] Weak loading of legacy items, see #2151 --- .../file/BukkitActionYamlStorage.java | 22 ++++++++-- .../file/BukkitQuestYamlStorage.java | 44 ++++++++++++++++--- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/me/pikamug/quests/storage/implementation/file/BukkitActionYamlStorage.java b/core/src/main/java/me/pikamug/quests/storage/implementation/file/BukkitActionYamlStorage.java index d9baf8be8..fc4a98dc0 100644 --- a/core/src/main/java/me/pikamug/quests/storage/implementation/file/BukkitActionYamlStorage.java +++ b/core/src/main/java/me/pikamug/quests/storage/implementation/file/BukkitActionYamlStorage.java @@ -12,6 +12,7 @@ import me.pikamug.quests.util.BukkitItemUtil; import me.pikamug.quests.util.BukkitMiscUtil; import org.bukkit.Effect; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; @@ -211,13 +212,28 @@ public class BukkitActionYamlStorage implements ActionStorageImpl { } if (data.contains(actionKey + "items")) { final LinkedList temp = new LinkedList<>(); - final List stackList = (List) data.get(actionKey + "items"); - if (BukkitConfigUtil.checkList(stackList, ItemStack.class)) { - for (final ItemStack stack : stackList) { + final List itemList = (List) data.get(actionKey + "items"); + if (BukkitConfigUtil.checkList(itemList, ItemStack.class)) { + for (final Object item : itemList) { + final ItemStack stack = (ItemStack) item; if (stack != null) { temp.add(stack); } } + } else if (BukkitConfigUtil.checkList(itemList, String.class)) { + // Legacy + for (final Object item : itemList) { + final String stack = (String) item; + if (stack != null) { + final String itemName = stack.substring(5, stack.indexOf(':')); + final Material itemMat = Material.matchMaterial(itemName); + if (itemMat != null) { + temp.add(new ItemStack(itemMat, 1)); + } else { + throw new ActionFormatException("'items' has invalid name" + itemName, actionKey); + } + } + } } else { throw new ActionFormatException("'items' is not a list of items", actionKey); } diff --git a/core/src/main/java/me/pikamug/quests/storage/implementation/file/BukkitQuestYamlStorage.java b/core/src/main/java/me/pikamug/quests/storage/implementation/file/BukkitQuestYamlStorage.java index 812b397a3..fdb7401b4 100644 --- a/core/src/main/java/me/pikamug/quests/storage/implementation/file/BukkitQuestYamlStorage.java +++ b/core/src/main/java/me/pikamug/quests/storage/implementation/file/BukkitQuestYamlStorage.java @@ -222,7 +222,7 @@ public class BukkitQuestYamlStorage implements QuestStorageImpl { } } if (config.contains("quests." + questId + ".gui-display")) { - ItemStack stack = config.getItemStack("quests." + questId + ".gui-display"); + final ItemStack stack = config.getItemStack("quests." + questId + ".gui-display"); if (stack != null) { quest.setGUIDisplay(stack); } else { @@ -335,13 +335,28 @@ public class BukkitQuestYamlStorage implements QuestStorageImpl { final BukkitDependencies depends = plugin.getDependencies(); if (config.contains("quests." + questKey + ".rewards.items")) { final LinkedList temp = new LinkedList<>(); - final List stackList = (List) config.get("quests." + questKey + ".rewards.items"); - if (BukkitConfigUtil.checkList(stackList, ItemStack.class)) { - for (final ItemStack stack : stackList) { + final List itemList = (List) config.get("quests." + questKey + ".rewards.items"); + if (BukkitConfigUtil.checkList(itemList, ItemStack.class)) { + for (final Object item : itemList) { + final ItemStack stack = (ItemStack) item; if (stack != null) { temp.add(stack); } } + } else if (BukkitConfigUtil.checkList(itemList, String.class)) { + // Legacy + for (final Object item : itemList) { + final String stack = (String) item; + if (stack != null) { + final String itemName = stack.substring(5, stack.indexOf(':')); + final Material itemMat = Material.matchMaterial(itemName); + if (itemMat != null) { + temp.add(new ItemStack(itemMat, 1)); + } else { + throw new QuestFormatException("Reward 'items' has invalid name" + itemName, questKey); + } + } + } } else { throw new QuestFormatException("Reward 'items' has invalid formatting", questKey); } @@ -505,14 +520,29 @@ public class BukkitQuestYamlStorage implements QuestStorageImpl { } if (config.contains("quests." + questKey + ".requirements.items")) { final List temp = new LinkedList<>(); - final List stackList = (List) config.get("quests." + questKey + final List itemList = (List) config.get("quests." + questKey + ".requirements.items"); - if (BukkitConfigUtil.checkList(stackList, ItemStack.class)) { - for (final ItemStack stack : stackList) { + if (BukkitConfigUtil.checkList(itemList, ItemStack.class)) { + for (final Object item : itemList) { + final ItemStack stack = (ItemStack) item; if (stack != null) { temp.add(stack); } } + } else if (BukkitConfigUtil.checkList(itemList, String.class)) { + // Legacy + for (final Object item : itemList) { + final String stack = (String) item; + if (stack != null) { + final String itemName = stack.substring(5, stack.indexOf(':')); + final Material itemMat = Material.matchMaterial(itemName); + if (itemMat != null) { + temp.add(new ItemStack(itemMat, 1)); + } else { + throw new QuestFormatException("Requirement 'items' has invalid name" + itemName, questKey); + } + } + } } else { throw new QuestFormatException("Requirement 'items' has invalid formatting", questKey); }