From 2a4ce63e337c59a03ba8d719bb9482a216f53cad Mon Sep 17 00:00:00 2001 From: Auxilor Date: Mon, 14 Sep 2020 20:33:28 +0100 Subject: [PATCH] Added support for custom lore colors --- .../ecoenchants/config/configs/Rarity.java | 37 +++++++++++++++++++ .../ecoenchants/display/EnchantDisplay.java | 5 +++ .../enchantments/EnchantmentRarity.java | 36 +++++++++++++++--- .../listeners/EnchantingListeners.java | 2 + Plugin/src/main/resources/config.yml | 2 +- Plugin/src/main/resources/rarity.yml | 27 +++++++++++++- 6 files changed, 101 insertions(+), 8 deletions(-) diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/config/configs/Rarity.java b/Plugin/src/main/java/com/willfp/ecoenchants/config/configs/Rarity.java index 2a2ed18d..a845eb55 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/config/configs/Rarity.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/config/configs/Rarity.java @@ -4,6 +4,7 @@ import com.willfp.ecoenchants.config.YamlConfig; import org.bukkit.Material; import java.util.HashSet; +import java.util.List; import java.util.Set; /** @@ -17,4 +18,40 @@ public class Rarity extends YamlConfig { public Set getRarities() { return config.getConfigurationSection("rarities").getKeys(false); } + + public int getInt(String path) { + return config.getInt(path); + } + + public int getInt(String path, int def) { + return config.getInt(path, def); + } + + public List getInts(String path) { + return config.getIntegerList(path); + } + + public boolean getBool(String path) { + return config.getBoolean(path); + } + + public List getBools(String path) { + return config.getBooleanList(path); + } + + public String getString(String path) { + return config.getString(path); + } + + public List getStrings(String path) { + return config.getStringList(path); + } + + public double getDouble(String path) { + return config.getDouble(path); + } + + public List getDoubles(String path) { + return config.getDoubleList(path); + } } diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java index 899ab4ac..7558141b 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java @@ -5,6 +5,7 @@ import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.config.ConfigManager; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; +import com.willfp.ecoenchants.enchantments.EnchantmentRarity; import com.willfp.ecoenchants.enchantments.EnchantmentTarget; import com.willfp.ecoenchants.util.NumberUtils; import org.apache.commons.lang.WordUtils; @@ -202,6 +203,10 @@ public class EnchantDisplay { if(isEcoEnchant) { name = enchantment.getName(); description = EcoEnchants.getFromEnchantment(enchantment).getDescription(); + EnchantmentRarity rarity = EcoEnchants.getFromEnchantment(enchantment).getRarity(); + if(rarity.hasCustomColor()) { + color = rarity.getCustomColor(); + } description.replaceAll(line -> prefix + descriptionColor + line); if(!EcoEnchants.getFromEnchantment(enchantment).isEnabled()) forRemoval.add(enchantment); } else { diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EnchantmentRarity.java b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EnchantmentRarity.java index 6103e47c..00cb7b1f 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EnchantmentRarity.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EnchantmentRarity.java @@ -1,6 +1,7 @@ package com.willfp.ecoenchants.enchantments; import com.willfp.ecoenchants.config.ConfigManager; +import org.bukkit.ChatColor; import java.util.HashSet; import java.util.Optional; @@ -17,6 +18,7 @@ public class EnchantmentRarity { private final int minimumLevel; private final double villagerProbability; private final double lootProbability; + private final String customColor; /** * Create new EnchantmentRarity @@ -25,8 +27,9 @@ public class EnchantmentRarity { * @param minimumLevel The minimum xp level * @param villagerProbability The probability of a villager obtaining an enchantment with this rarity * @param lootProbability The probability of an item in a loot chest having an enchantment with this rarity + * @param customColor The custom display color, or null if not enabled */ - public EnchantmentRarity(String name, double probability, int minimumLevel, double villagerProbability, double lootProbability) { + public EnchantmentRarity(String name, double probability, int minimumLevel, double villagerProbability, double lootProbability, String customColor) { Optional matching = rarities.stream().filter(rarity -> rarity.getName().equalsIgnoreCase(name)).findFirst(); matching.ifPresent(rarities::remove); @@ -35,6 +38,7 @@ public class EnchantmentRarity { this.minimumLevel = minimumLevel; this.villagerProbability = villagerProbability; this.lootProbability = lootProbability; + this.customColor = customColor; rarities.add(this); } @@ -47,6 +51,22 @@ public class EnchantmentRarity { return this.name; } + /** + * Is custom color enabled + * @return If has enabled custom color + */ + public boolean hasCustomColor() { + return this.customColor != null; + } + + /** + * Get custom color + * @return The custom color + */ + public String getCustomColor() { + return this.customColor; + } + /** * Get the probability of obtaining enchantment with this rarity from an enchanting table * @return The probability as a percentage @@ -97,12 +117,16 @@ public class EnchantmentRarity { Set raritiesNames = ConfigManager.getRarity().getRarities(); raritiesNames.forEach((rarity) -> { String name = rarity; - double probability = ConfigManager.getConfig().getDouble("rarities." + rarity + ".table-probability"); - int minimumLevel = ConfigManager.getConfig().getInt("rarities." + rarity + ".minimum-level"); - double villagerProbability = ConfigManager.getConfig().getDouble("rarities." + rarity + ".villager-probability"); - double lootProbability = ConfigManager.getConfig().getDouble("rarities." + rarity + ".loot-probability"); + double probability = ConfigManager.getRarity().getDouble("rarities." + rarity + ".table-probability"); + int minimumLevel = ConfigManager.getRarity().getInt("rarities." + rarity + ".minimum-level"); + double villagerProbability = ConfigManager.getRarity().getDouble("rarities." + rarity + ".villager-probability"); + double lootProbability = ConfigManager.getRarity().getDouble("rarities." + rarity + ".loot-probability"); + String customColor = null; + if(ConfigManager.getRarity().getBool("rarities." + rarity + ".custom-color.enabled")) { + customColor = ChatColor.translateAlternateColorCodes('&', ConfigManager.getRarity().getString("rarities." + rarity + ".custom-color.color")); + } - new EnchantmentRarity(name, probability, minimumLevel, villagerProbability, lootProbability); + new EnchantmentRarity(name, probability, minimumLevel, villagerProbability, lootProbability, customColor); }); } diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/listeners/EnchantingListeners.java b/Plugin/src/main/java/com/willfp/ecoenchants/listeners/EnchantingListeners.java index 3e5f7719..248bc8de 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/listeners/EnchantingListeners.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/listeners/EnchantingListeners.java @@ -5,6 +5,7 @@ import com.willfp.ecoenchants.config.ConfigManager; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.util.NumberUtils; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.EnchantmentOffer; @@ -38,6 +39,7 @@ public class EnchantingListeners implements Listener { Player player = event.getEnchanter(); ItemStack item = event.getItem(); int cost = event.getExpLevelCost(); + Map toAdd = event.getEnchantsToAdd(); if (!ConfigManager.getConfig().getBool("enchanting-table.enabled")) { new BukkitRunnable() { diff --git a/Plugin/src/main/resources/config.yml b/Plugin/src/main/resources/config.yml index cd5aba8e..0709f013 100644 --- a/Plugin/src/main/resources/config.yml +++ b/Plugin/src/main/resources/config.yml @@ -19,7 +19,7 @@ lore: use-numbers-above-threshold: 10 #After level 10, enchantments will display as Name Number, eg: Sharpness 25 instead of Sharpness XXV describe: # Describe enchantments in lore - enabled: true + enabled: false before-lines: 5 # Describe before or equal to number of enchantments wrap: 30 # Word wrap after number of characters diff --git a/Plugin/src/main/resources/rarity.yml b/Plugin/src/main/resources/rarity.yml index 6c13ab23..a3924912 100644 --- a/Plugin/src/main/resources/rarity.yml +++ b/Plugin/src/main/resources/rarity.yml @@ -8,38 +8,63 @@ rarities: # Villager probability is the chance of a villager having this trade as a percentage. Vanilla default for all enchantments is 2.7%, however you can choose this per-rarity. # Loot probability is the chance of an item in a loot chest having this enchantment as a percentage + # Custom Color is a custom name color for all enchantments of rarity to have + # This is disabled by default. + # Curses override this, and always display in their specified color. + common: table-probability: 30 minimum-level: 1 villager-probability: 10.5 loot-probability: 12 + custom-color: + enabled: false + color: "&7" uncommon: table-probability: 20 minimum-level: 5 villager-probability: 9 loot-probability: 16 + custom-color: + enabled: false + color: "&e" rare: table-probability: 20 minimum-level: 15 villager-probability: 7.5 loot-probability: 18 + custom-color: + enabled: false + color: "&a" epic: table-probability: 10 minimum-level: 16 villager-probability: 6 loot-probability: 20 + custom-color: + enabled: false + color: "&9" legendary: table-probability: 8 minimum-level: 20 villager-probability: 4.5 loot-probability: 15 + custom-color: + enabled: false + color: "&6" special: table-probability: 2 minimum-level: 30 villager-probability: 3 loot-probability: 5 + custom-color: + enabled: false + color: "&d" veryspecial: table-probability: 1 minimum-level: 30 villager-probability: 1.5 - loot-probability: 2 \ No newline at end of file + loot-probability: 2 + custom-color: + enabled: false + color: "&5" \ No newline at end of file