From 16a77f281e33a344a942af5b3b50bc2c5818e0bd Mon Sep 17 00:00:00 2001 From: EasyMFnE Date: Sat, 23 Sep 2017 15:35:38 -0400 Subject: [PATCH 1/2] Add automatic Alchemy potion colorization and custom color configuration. --- .../config/skills/alchemy/PotionConfig.java | 45 ++++++++++++++++++- .../skills/alchemy/AlchemyPotion.java | 17 ++++++- src/main/resources/potions.yml | 1 + 3 files changed, 61 insertions(+), 2 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 2523ebaf3..8fef2b278 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 @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; import org.bukkit.ChatColor; +import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; @@ -165,6 +166,14 @@ public class PotionConfig extends ConfigLoader { } } } + + Color color = null; + if (potion_section.contains("Color")) { + color = Color.fromRGB(potion_section.getInt("Color")); + } + else { + color = this.generateColor(effects); + } Map children = new HashMap(); if (potion_section.contains("Children")) { @@ -179,7 +188,7 @@ public class PotionConfig extends ConfigLoader { } } - return new AlchemyPotion(material, data, name, lore, effects, children); + return new AlchemyPotion(material, data, name, lore, effects, color, children); } catch (Exception e) { mcMMO.p.getLogger().warning("Failed to load Alchemy potion: " + potion_section.getName()); @@ -251,4 +260,38 @@ public class PotionConfig extends ConfigLoader { } return null; } + + public Color generateColor(List effects) { + if (effects != null && !effects.isEmpty()) { + List colors = new ArrayList(); + for (PotionEffect effect : effects) { + if (effect.getType().getColor() != null) { + colors.add(effect.getType().getColor()); + } + } + if (!colors.isEmpty()) { + if (colors.size() > 1) { + return calculateAverageColor(colors); + } + return colors.get(0); + } + } + return null; + } + + public Color calculateAverageColor(List colors) { + int red = 0; + int green = 0; + int blue = 0; + for (Color color : colors) { + System.out.println("Input color: " + color); + red += color.getRed(); + green += color.getGreen(); + blue += color.getBlue(); + } + Color color = Color.fromRGB(red/colors.size(), green/colors.size(), blue/colors.size()); + System.out.println("Output color: " + color); + return color; + } + } 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 a7f95f2ce..d94a7fd2d 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 @@ -4,6 +4,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; @@ -19,15 +20,17 @@ public class AlchemyPotion { private String name; private List lore; private List effects; + private Color color; private Map children; - public AlchemyPotion(Material material, PotionData data, String name, List lore, List effects, Map children) { + public AlchemyPotion(Material material, PotionData data, String name, List lore, List effects, Color color, Map children) { this.material = material; this.data = data; this.lore = lore; this.name = name; this.effects = effects; this.children = children; + this.color = color; } public String toString() { @@ -52,6 +55,10 @@ public class AlchemyPotion { meta.addCustomEffect(effect, true); } } + + if (this.getColor() != null) { + meta.setColor(this.getColor()); + } potion.setItemMeta(meta); return potion; @@ -97,6 +104,14 @@ public class AlchemyPotion { this.effects = effects; } + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + public Map getChildren() { return children; } diff --git a/src/main/resources/potions.yml b/src/main/resources/potions.yml index 212b41cee..a49f78118 100644 --- a/src/main/resources/potions.yml +++ b/src/main/resources/potions.yml @@ -45,6 +45,7 @@ Concoctions: # Name: Custom display name for this potion (optional) # Material: Material for this potion, defaults to POTION # Data: Data value for this potion (if this is not present it will read from identifier) +# Color: Custom color for the potion in RGB integer (e.g. 0xFF000, optional) # Lore: ["lore1","lore2"...] Custom lore for this potion (section is optional) # Children: The potential children of this potion (section is optional) # : The ingredient used, and resultant potion's identifier From fcb6391ba3bf70225118a465464b44b6e0bbe1b1 Mon Sep 17 00:00:00 2001 From: EasyMFnE Date: Thu, 28 Sep 2017 11:44:15 -0400 Subject: [PATCH 2/2] Remove leftover debug code. --- .../com/gmail/nossr50/config/skills/alchemy/PotionConfig.java | 2 -- 1 file changed, 2 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 8fef2b278..768e89f54 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 @@ -284,13 +284,11 @@ public class PotionConfig extends ConfigLoader { int green = 0; int blue = 0; for (Color color : colors) { - System.out.println("Input color: " + color); red += color.getRed(); green += color.getGreen(); blue += color.getBlue(); } Color color = Color.fromRGB(red/colors.size(), green/colors.size(), blue/colors.size()); - System.out.println("Output color: " + color); return color; }