Update Alchemy for 1.9 Configuration style has changed, but theoretically old version should still work I think.

This commit is contained in:
t00thpick1 2016-03-01 17:05:58 -05:00
parent f77ae6ee15
commit f52d9feef8
4 changed files with 85 additions and 27 deletions

View File

@ -28,7 +28,7 @@ public class PotionConfig extends ConfigLoader {
private List<ItemStack> concoctionsIngredientsTierSeven = new ArrayList<ItemStack>(); private List<ItemStack> concoctionsIngredientsTierSeven = new ArrayList<ItemStack>();
private List<ItemStack> concoctionsIngredientsTierEight = new ArrayList<ItemStack>(); private List<ItemStack> concoctionsIngredientsTierEight = new ArrayList<ItemStack>();
private Map<Short, AlchemyPotion> potionMap = new HashMap<Short, AlchemyPotion>(); private Map<String, AlchemyPotion> potionMap = new HashMap<String, AlchemyPotion>();
private PotionConfig() { private PotionConfig() {
super("potions.yml"); super("potions.yml");
@ -90,11 +90,11 @@ public class PotionConfig extends ConfigLoader {
int pass = 0; int pass = 0;
int fail = 0; int fail = 0;
for (String dataValue : potionSection.getKeys(false)) { for (String potionName : potionSection.getKeys(false)) {
AlchemyPotion potion = loadPotion(potionSection.getConfigurationSection(dataValue)); AlchemyPotion potion = loadPotion(potionSection.getConfigurationSection(potionName));
if (potion != null) { if (potion != null) {
potionMap.put(potion.getDataValue(), potion); potionMap.put(potionName, potion);
pass++; pass++;
} }
else { else {
@ -115,12 +115,20 @@ public class PotionConfig extends ConfigLoader {
*/ */
private AlchemyPotion loadPotion(ConfigurationSection potion_section) { private AlchemyPotion loadPotion(ConfigurationSection potion_section) {
try { try {
short dataValue = Short.parseShort(potion_section.getName());
String name = potion_section.getString("Name"); String name = potion_section.getString("Name");
if (name != null) { if (name != null) {
name = ChatColor.translateAlternateColorCodes('&', name); 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<String> lore = new ArrayList<String>(); List<String> lore = new ArrayList<String>();
if (potion_section.contains("Lore")) { if (potion_section.contains("Lore")) {
@ -147,12 +155,12 @@ public class PotionConfig extends ConfigLoader {
} }
} }
Map<ItemStack, Short> children = new HashMap<ItemStack, Short>(); Map<ItemStack, String> children = new HashMap<ItemStack, String>();
if (potion_section.contains("Children")) { if (potion_section.contains("Children")) {
for (String child : potion_section.getConfigurationSection("Children").getKeys(false)) { for (String child : potion_section.getConfigurationSection("Children").getKeys(false)) {
ItemStack ingredient = loadIngredient(child); ItemStack ingredient = loadIngredient(child);
if (ingredient != null) { if (ingredient != null) {
children.put(ingredient, Short.parseShort(potion_section.getConfigurationSection("Children").getString(child))); children.put(ingredient, potion_section.getConfigurationSection("Children").getString(child));
} }
else { else {
mcMMO.p.getLogger().warning("Failed to parse child for potion " + name + ": " + child); 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) { catch (Exception e) {
mcMMO.p.getLogger().warning("Failed to load Alchemy potion: " + potion_section.getName()); 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()); return potionMap.containsKey(item.getDurability());
} }
public AlchemyPotion getPotion(short durability) { public AlchemyPotion getPotion(String name) {
return potionMap.get(durability); return potionMap.get(name);
}
public AlchemyPotion getPotion(ItemStack item) {
for (AlchemyPotion potion : potionMap.values()) {
if (potion.isSimilar(item)) {
return potion;
}
}
return null;
} }
} }

View File

@ -10,14 +10,18 @@ import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.Potion; import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import com.gmail.nossr50.config.skills.alchemy.PotionConfig;
public class AlchemyPotion { public class AlchemyPotion {
private Material material;
private short dataValue; private short dataValue;
private String name; private String name;
private List<String> lore; private List<String> lore;
private List<PotionEffect> effects; private List<PotionEffect> effects;
private Map<ItemStack, Short> children; private Map<ItemStack, String> children;
public AlchemyPotion(short dataValue, String name, List<String> lore, List<PotionEffect> effects, Map<ItemStack, Short> children) { public AlchemyPotion(Material material, short dataValue, String name, List<String> lore, List<PotionEffect> effects, Map<ItemStack, String> children) {
this.material = material;
this.dataValue = dataValue; this.dataValue = dataValue;
this.lore = lore; this.lore = lore;
this.name = name; this.name = name;
@ -30,7 +34,7 @@ public class AlchemyPotion {
} }
public ItemStack toItemStack(int amount) { 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(); PotionMeta meta = (PotionMeta) potion.getItemMeta();
if (this.getName() != null) { if (this.getName() != null) {
@ -51,6 +55,10 @@ public class AlchemyPotion {
return potion; return potion;
} }
public Material getMaterial() {
return material;
}
public Potion toPotion(int amount) { public Potion toPotion(int amount) {
return Potion.fromItemStack(this.toItemStack(amount)); return Potion.fromItemStack(this.toItemStack(amount));
} }
@ -87,22 +95,50 @@ public class AlchemyPotion {
this.effects = effects; this.effects = effects;
} }
public Map<ItemStack, Short> getChildren() { public Map<ItemStack, String> getChildren() {
return children; return children;
} }
public void setChildren(Map<ItemStack, Short> children) { public void setChildren(Map<ItemStack, String> children) {
this.children = children; this.children = children;
} }
public Short getChildDataValue(ItemStack ingredient) { public AlchemyPotion getChild(ItemStack ingredient) {
if (!children.isEmpty()) { if (!children.isEmpty()) {
for (Entry<ItemStack, Short> child : children.entrySet()) { for (Entry<ItemStack, String> child : children.entrySet()) {
if (ingredient.isSimilar(child.getKey())) { 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);
} }
} }

View File

@ -36,7 +36,7 @@ public final class AlchemyPotionBrewer {
continue; 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; return true;
} }
} }
@ -45,8 +45,8 @@ public final class AlchemyPotionBrewer {
} }
private static AlchemyPotion getChildPotion(AlchemyPotion potion, ItemStack ingredient) { private static AlchemyPotion getChildPotion(AlchemyPotion potion, ItemStack ingredient) {
if (potion != null && potion.getChildDataValue(ingredient) != -1) { if (potion != null) {
return PotionConfig.getInstance().getPotion(potion.getChildDataValue(ingredient)); return potion.getChild(ingredient);
} }
return null; return null;
@ -118,8 +118,8 @@ public final class AlchemyPotionBrewer {
continue; continue;
} }
AlchemyPotion input = PotionConfig.getInstance().getPotion(item.getDurability()); AlchemyPotion input = PotionConfig.getInstance().getPotion(item);
AlchemyPotion output = PotionConfig.getInstance().getPotion(input.getChildDataValue(ingredient)); AlchemyPotion output = input.getChild(ingredient);
inputList.add(input); inputList.add(input);
@ -138,7 +138,7 @@ public final class AlchemyPotionBrewer {
removeIngredient(inventory, player); removeIngredient(inventory, player);
for (AlchemyPotion input : inputList) { for (AlchemyPotion input : inputList) {
AlchemyPotion output = PotionConfig.getInstance().getPotion(input.getChildDataValue(ingredient)); AlchemyPotion output = input.getChild(ingredient);
if (output != null && player != null) { if (output != null && player != null) {
PotionStage potionStage = PotionStage.getPotionStage(input, output); PotionStage potionStage = PotionStage.getPotionStage(input, output);

View File

@ -63,9 +63,12 @@ Potions:
### NON-EFFECT POTIONS ##################################################### ### NON-EFFECT POTIONS #####################################################
0: # Water Bottle WATER: # Water Bottle
Name: Water Bottle
Material: POTION
Data: 0
Children: Children:
BLAZE_POWDER: 8192 # Mundane Potion BLAZE_POWDER: MUNDANE_POTION
FERMENTED_SPIDER_EYE: 8200 # Potion of Weakness FERMENTED_SPIDER_EYE: 8200 # Potion of Weakness
GHAST_TEAR: 8192 # Mundane Potion GHAST_TEAR: 8192 # Mundane Potion
GLOWSTONE_DUST: 32 # Thick Potion GLOWSTONE_DUST: 32 # Thick Potion
@ -77,6 +80,8 @@ Potions:
SUGAR: 8192 # Mundane Potion SUGAR: 8192 # Mundane Potion
16: # Awkward Potion 16: # Awkward Potion
Material: POTION
Data: 0
Children: Children:
APPLE: 5376 # Potion of Health Boost APPLE: 5376 # Potion of Health Boost
BLAZE_POWDER: 8201 # Potion of Strength BLAZE_POWDER: 8201 # Potion of Strength