From f52d9feef82988b295bd4d0200b38fc394ac1144 Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Tue, 1 Mar 2016 17:05:58 -0500 Subject: [PATCH] Update Alchemy for 1.9 Configuration style has changed, but theoretically old version should still work I think. --- .../config/skills/alchemy/PotionConfig.java | 37 +++++++++---- .../skills/alchemy/AlchemyPotion.java | 54 +++++++++++++++---- .../skills/alchemy/AlchemyPotionBrewer.java | 12 ++--- src/main/resources/potions.yml | 9 +++- 4 files changed, 85 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/config/skills/alchemy/PotionConfig.java b/src/main/java/com/gmail/nossr50/config/skills/alchemy/PotionConfig.java index a97c8e229..6c9b661dc 100644 --- a/src/main/java/com/gmail/nossr50/config/skills/alchemy/PotionConfig.java +++ b/src/main/java/com/gmail/nossr50/config/skills/alchemy/PotionConfig.java @@ -28,7 +28,7 @@ public class PotionConfig extends ConfigLoader { private List concoctionsIngredientsTierSeven = new ArrayList(); private List concoctionsIngredientsTierEight = new ArrayList(); - private Map potionMap = new HashMap(); + private Map potionMap = new HashMap(); private PotionConfig() { super("potions.yml"); @@ -90,11 +90,11 @@ public class PotionConfig extends ConfigLoader { int pass = 0; int fail = 0; - for (String dataValue : potionSection.getKeys(false)) { - AlchemyPotion potion = loadPotion(potionSection.getConfigurationSection(dataValue)); + for (String potionName : potionSection.getKeys(false)) { + AlchemyPotion potion = loadPotion(potionSection.getConfigurationSection(potionName)); if (potion != null) { - potionMap.put(potion.getDataValue(), potion); + potionMap.put(potionName, potion); pass++; } else { @@ -115,12 +115,20 @@ public class PotionConfig extends ConfigLoader { */ private AlchemyPotion loadPotion(ConfigurationSection potion_section) { try { - short dataValue = Short.parseShort(potion_section.getName()); + String name = potion_section.getString("Name"); if (name != null) { name = ChatColor.translateAlternateColorCodes('&', name); } + + short dataValue = Short.parseShort(potion_section.getString("Data")); + + Material material = Material.POTION; + String mat = potion_section.getString("Material", null); + if (mat != null) { + material = Material.valueOf(mat); + } List lore = new ArrayList(); if (potion_section.contains("Lore")) { @@ -147,12 +155,12 @@ public class PotionConfig extends ConfigLoader { } } - Map children = new HashMap(); + Map children = new HashMap(); if (potion_section.contains("Children")) { for (String child : potion_section.getConfigurationSection("Children").getKeys(false)) { ItemStack ingredient = loadIngredient(child); if (ingredient != null) { - children.put(ingredient, Short.parseShort(potion_section.getConfigurationSection("Children").getString(child))); + children.put(ingredient, potion_section.getConfigurationSection("Children").getString(child)); } else { mcMMO.p.getLogger().warning("Failed to parse child for potion " + name + ": " + child); @@ -160,7 +168,7 @@ public class PotionConfig extends ConfigLoader { } } - return new AlchemyPotion(dataValue, name, lore, effects, children); + return new AlchemyPotion(material, dataValue, name, lore, effects, children); } catch (Exception e) { mcMMO.p.getLogger().warning("Failed to load Alchemy potion: " + potion_section.getName()); @@ -220,7 +228,16 @@ public class PotionConfig extends ConfigLoader { return potionMap.containsKey(item.getDurability()); } - public AlchemyPotion getPotion(short durability) { - return potionMap.get(durability); + public AlchemyPotion getPotion(String name) { + return potionMap.get(name); + } + + public AlchemyPotion getPotion(ItemStack item) { + for (AlchemyPotion potion : potionMap.values()) { + if (potion.isSimilar(item)) { + return potion; + } + } + return null; } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/skills/alchemy/AlchemyPotion.java b/src/main/java/com/gmail/nossr50/datatypes/skills/alchemy/AlchemyPotion.java index 69d2d5baa..2ecb2c17b 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/skills/alchemy/AlchemyPotion.java +++ b/src/main/java/com/gmail/nossr50/datatypes/skills/alchemy/AlchemyPotion.java @@ -10,14 +10,18 @@ import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.Potion; import org.bukkit.potion.PotionEffect; +import com.gmail.nossr50.config.skills.alchemy.PotionConfig; + public class AlchemyPotion { + private Material material; private short dataValue; private String name; private List lore; private List effects; - private Map children; + private Map children; - public AlchemyPotion(short dataValue, String name, List lore, List effects, Map children) { + public AlchemyPotion(Material material, short dataValue, String name, List lore, List effects, Map children) { + this.material = material; this.dataValue = dataValue; this.lore = lore; this.name = name; @@ -30,7 +34,7 @@ public class AlchemyPotion { } public ItemStack toItemStack(int amount) { - ItemStack potion = new ItemStack(Material.POTION, amount, this.getDataValue()); + ItemStack potion = new ItemStack(material, amount, this.getDataValue()); PotionMeta meta = (PotionMeta) potion.getItemMeta(); if (this.getName() != null) { @@ -51,6 +55,10 @@ public class AlchemyPotion { return potion; } + public Material getMaterial() { + return material; + } + public Potion toPotion(int amount) { return Potion.fromItemStack(this.toItemStack(amount)); } @@ -87,22 +95,50 @@ public class AlchemyPotion { this.effects = effects; } - public Map getChildren() { + public Map getChildren() { return children; } - public void setChildren(Map children) { + public void setChildren(Map children) { this.children = children; } - public Short getChildDataValue(ItemStack ingredient) { + public AlchemyPotion getChild(ItemStack ingredient) { if (!children.isEmpty()) { - for (Entry child : children.entrySet()) { + for (Entry child : children.entrySet()) { if (ingredient.isSimilar(child.getKey())) { - return child.getValue(); + return PotionConfig.getInstance().getPotion(child.getValue()); } } } - return -1; + return null; + } + + public boolean isSimilar(ItemStack item) { + if (item.getType() != material) { + return false; + } + if (item.getDurability() != dataValue) { + return false; + } + if (!item.hasItemMeta()) { + return false; + } + PotionMeta meta = (PotionMeta) item.getItemMeta(); + for (PotionEffect effect : effects) { + if (!meta.hasCustomEffect(effect.getType())) { + return false; + } + } + if (!meta.hasLore()) { + return false; + } + if (!meta.getLore().equals(lore)) { + return false; + } + if (!meta.hasDisplayName()) { + return false; + } + return meta.getDisplayName().equals(name); } } diff --git a/src/main/java/com/gmail/nossr50/skills/alchemy/AlchemyPotionBrewer.java b/src/main/java/com/gmail/nossr50/skills/alchemy/AlchemyPotionBrewer.java index a149ded15..9cacc5c72 100644 --- a/src/main/java/com/gmail/nossr50/skills/alchemy/AlchemyPotionBrewer.java +++ b/src/main/java/com/gmail/nossr50/skills/alchemy/AlchemyPotionBrewer.java @@ -36,7 +36,7 @@ public final class AlchemyPotionBrewer { continue; } - if (getChildPotion(PotionConfig.getInstance().getPotion(contents[i].getDurability()), contents[Alchemy.INGREDIENT_SLOT]) != null) { + if (getChildPotion(PotionConfig.getInstance().getPotion(contents[i]), contents[Alchemy.INGREDIENT_SLOT]) != null) { return true; } } @@ -45,8 +45,8 @@ public final class AlchemyPotionBrewer { } private static AlchemyPotion getChildPotion(AlchemyPotion potion, ItemStack ingredient) { - if (potion != null && potion.getChildDataValue(ingredient) != -1) { - return PotionConfig.getInstance().getPotion(potion.getChildDataValue(ingredient)); + if (potion != null) { + return potion.getChild(ingredient); } return null; @@ -118,8 +118,8 @@ public final class AlchemyPotionBrewer { continue; } - AlchemyPotion input = PotionConfig.getInstance().getPotion(item.getDurability()); - AlchemyPotion output = PotionConfig.getInstance().getPotion(input.getChildDataValue(ingredient)); + AlchemyPotion input = PotionConfig.getInstance().getPotion(item); + AlchemyPotion output = input.getChild(ingredient); inputList.add(input); @@ -138,7 +138,7 @@ public final class AlchemyPotionBrewer { removeIngredient(inventory, player); for (AlchemyPotion input : inputList) { - AlchemyPotion output = PotionConfig.getInstance().getPotion(input.getChildDataValue(ingredient)); + AlchemyPotion output = input.getChild(ingredient); if (output != null && player != null) { PotionStage potionStage = PotionStage.getPotionStage(input, output); diff --git a/src/main/resources/potions.yml b/src/main/resources/potions.yml index d023ccac9..f55472232 100644 --- a/src/main/resources/potions.yml +++ b/src/main/resources/potions.yml @@ -63,9 +63,12 @@ Potions: ### NON-EFFECT POTIONS ##################################################### - 0: # Water Bottle + WATER: # Water Bottle + Name: Water Bottle + Material: POTION + Data: 0 Children: - BLAZE_POWDER: 8192 # Mundane Potion + BLAZE_POWDER: MUNDANE_POTION FERMENTED_SPIDER_EYE: 8200 # Potion of Weakness GHAST_TEAR: 8192 # Mundane Potion GLOWSTONE_DUST: 32 # Thick Potion @@ -77,6 +80,8 @@ Potions: SUGAR: 8192 # Mundane Potion 16: # Awkward Potion + Material: POTION + Data: 0 Children: APPLE: 5376 # Potion of Health Boost BLAZE_POWDER: 8201 # Potion of Strength