From 45ce71a73aac20fdf4badfe15605c9855d8692f2 Mon Sep 17 00:00:00 2001 From: dev-hydrogen <96733109+dev-hydrogen@users.noreply.github.com> Date: Thu, 21 Apr 2022 19:31:59 +0300 Subject: [PATCH] implement customizable rarities, no longer hardcoded --- .../java/com/ilm9001/cosmetics/Cosmetics.java | 37 ++++++++++-- .../ilm9001/cosmetics/rarity/Rarities.java | 56 +++++++++++-------- .../cosmetics/summon/CosmeticFactory.java | 12 ++-- .../com/ilm9001/cosmetics/util/Cosmetic.java | 11 ++-- src/main/resources/cosmetics.yml | 2 +- src/main/resources/rarities.yml | 16 ++++++ 6 files changed, 94 insertions(+), 40 deletions(-) create mode 100644 src/main/resources/rarities.yml diff --git a/src/main/java/com/ilm9001/cosmetics/Cosmetics.java b/src/main/java/com/ilm9001/cosmetics/Cosmetics.java index 9292134..9e6e69e 100644 --- a/src/main/java/com/ilm9001/cosmetics/Cosmetics.java +++ b/src/main/java/com/ilm9001/cosmetics/Cosmetics.java @@ -5,8 +5,10 @@ import com.ilm9001.cosmetics.commands.RefreshCosmeticsList; import com.ilm9001.cosmetics.listeners.ArmorSlotClickListener; import com.ilm9001.cosmetics.listeners.RightClickEventListener; +import com.ilm9001.cosmetics.rarity.Rarities; import com.ilm9001.cosmetics.summon.CosmeticFactory; import com.ilm9001.cosmetics.util.Cosmetic; +import com.ilm9001.cosmetics.util.Util; import org.bstats.bukkit.Metrics; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; @@ -23,8 +25,10 @@ public class Cosmetics extends JavaPlugin { private static List cosmeticList; private static FileConfiguration cosmetics; + private static FileConfiguration rarities; private static File cosmeticsFile; - + private static File raritiesFile; + // Static abuse? No, Static abusage. @Override @@ -32,14 +36,19 @@ public void onEnable() { instance = this; this.saveDefaultConfig(); - this.createCosmetics(); - + this.createFiles(); + Rarities.setRaritiesFromFile(); + if(cosmeticFactory == null) { cosmeticFactory = new CosmeticFactory(); cosmeticList = cosmeticFactory.getCosmeticsFromConfig(); } int pluginId = 12993; Metrics metrics = new Metrics(this,pluginId); + + if(Util.isSpigot()) { + getLogger().info("Spigot detected, disabling paper-only features"); + } // probably a good idea to eventually move these elsewhere to work as a method this.getCommand("givecosmetic").setExecutor(new GiveCosmetic()); @@ -77,29 +86,45 @@ public void onDisable() { public static FileConfiguration getCosmetics() { return cosmetics; } + + public static FileConfiguration getRarities() { + return rarities; + } + + public static File getRaritiesFile() { + return raritiesFile; + } /** - * Reloads cosmetics file for use in CosmeticFactory. + * Reloads files. */ - public static void refreshCosmetics() { + public static void refreshFiles() { try { cosmetics.load(cosmeticsFile); + rarities.load(raritiesFile); } catch (IOException | InvalidConfigurationException e) { e.printStackTrace(); } } // https://www.spigotmc.org/wiki/config-files/ - private void createCosmetics() { + private void createFiles() { cosmeticsFile = new File(getDataFolder(), "cosmetics.yml"); + raritiesFile = new File(getDataFolder(), "rarities.yml"); if (!cosmeticsFile.exists()) { cosmeticsFile.getParentFile().mkdirs(); saveResource("cosmetics.yml", false); } + if(!raritiesFile.exists()) { + raritiesFile.getParentFile().mkdirs(); + saveResource("rarities.yml", false); + } cosmetics = new YamlConfiguration(); + rarities = new YamlConfiguration(); try { cosmetics.load(cosmeticsFile); + rarities.load(raritiesFile); } catch (IOException | InvalidConfigurationException e) { e.printStackTrace(); } diff --git a/src/main/java/com/ilm9001/cosmetics/rarity/Rarities.java b/src/main/java/com/ilm9001/cosmetics/rarity/Rarities.java index 4b41279..9d726b4 100644 --- a/src/main/java/com/ilm9001/cosmetics/rarity/Rarities.java +++ b/src/main/java/com/ilm9001/cosmetics/rarity/Rarities.java @@ -1,33 +1,41 @@ package com.ilm9001.cosmetics.rarity; +import com.ilm9001.cosmetics.Cosmetics; +import com.ilm9001.cosmetics.util.Cosmetic; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextColor; -import net.kyori.adventure.text.format.TextDecoration; +import org.bukkit.configuration.file.FileConfiguration; -public enum Rarities { - COMMON (new Rarity("COMMON", Component.text("\uF80A") - .decoration(TextDecoration.ITALIC,false) - .color(TextColor.color(255,255,255)), TextColor.color(0x9c9c9f))), - UNCOMMON (new Rarity("UNCOMMON", Component.text("\uF80B") - .decoration(TextDecoration.ITALIC,false) - .color(TextColor.color(255,255,255)),TextColor.color(0x71cc1b))), - RARE (new Rarity("RARE", Component.text("\uF80C") - .decoration(TextDecoration.ITALIC,false) - .color(TextColor.color(255,255,255)),TextColor.color(0x2bd0f2))), - EPIC (new Rarity("EPIC", Component.text("\uF80D") - .decoration(TextDecoration.ITALIC,false) - .color(TextColor.color(255,255,255)),TextColor.color(0x6060FF))), - LEGENDARY (new Rarity("LEGENDARY", Component.text("\uF80E") - .decoration(TextDecoration.ITALIC,false) - .color(TextColor.color(255,255,255)),TextColor.color(0xffb920))); - - private final Rarity rarity; - - Rarities(Rarity rarity) { - this.rarity = rarity; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class Rarities { + private static final List rarities = new ArrayList<>(); + + public static void setRaritiesFromFile() { + List cosmeticsList = new ArrayList<>(); + Cosmetics.refreshFiles(); + FileConfiguration config = Cosmetics.getRarities(); + + for (String internalname : config.getConfigurationSection("Rarities").getKeys(false)) { + Component displayName; + TextColor color; + Map valuesmap = config.getConfigurationSection("Cosmetics."+internalname).getValues(false);; + List colorList; + if(valuesmap.get("color") instanceof List && valuesmap.get("color") != null) { + colorList = (List) valuesmap.get("color");; + } else throw new IllegalArgumentException("Color list is not a list, or is null"); + + displayName = Component.text((String) valuesmap.get("display")); + color = TextColor.color(colorList.get(0).intValue(), colorList.get(1).intValue(), colorList.get(2).intValue()); + + Rarity rarity = new Rarity(internalname, displayName, color); + rarities.add(rarity); + } } - public Rarity getRarity() { - return rarity; + public static List getRarities() { + return rarities; } } diff --git a/src/main/java/com/ilm9001/cosmetics/summon/CosmeticFactory.java b/src/main/java/com/ilm9001/cosmetics/summon/CosmeticFactory.java index 069b06c..515601e 100644 --- a/src/main/java/com/ilm9001/cosmetics/summon/CosmeticFactory.java +++ b/src/main/java/com/ilm9001/cosmetics/summon/CosmeticFactory.java @@ -10,12 +10,13 @@ import net.kyori.adventure.text.format.TextDecoration; import org.bukkit.Material; import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; public class CosmeticFactory { private ArrayList cosmeticNames; @@ -34,7 +35,7 @@ public CosmeticFactory() { */ public @NotNull List getCosmeticsFromConfig() { List cosmeticsList = new ArrayList<>(); - Cosmetics.refreshCosmetics(); + Cosmetics.refreshFiles(); FileConfiguration config = Cosmetics.getCosmetics(); for (String internalname : config.getConfigurationSection("Cosmetics").getKeys(false)) { @@ -45,7 +46,7 @@ public CosmeticFactory() { List formattedLore; CosmeticType type; Map valuesmap; - Rarity rarity; + AtomicReference rarity = new AtomicReference<>(); cosmeticCount++; cosmeticNames.add(internalname); @@ -56,7 +57,8 @@ public CosmeticFactory() { name = Component.text((String) valuesmap.get("name")); modelID = (Integer) valuesmap.get("modelID"); type = CosmeticType.valueOf((String)valuesmap.get("type")); - rarity = Rarities.valueOf(((String)valuesmap.get("rarity")).toUpperCase()).getRarity(); + Rarities.getRarities().stream().filter((r) -> !Objects.equals(r.getInternalRarityName(), valuesmap.get("rarity"))) + .findFirst().ifPresent(rarity::set); if(valuesmap.get("material") != null) material = Material.matchMaterial(valuesmap.get("material").toString()); else material = Material.PAPER; @@ -69,7 +71,7 @@ public CosmeticFactory() { .decoration(TextDecoration.ITALIC,false) )); - Cosmetic cosmetic = new Cosmetic(internalname,name,modelID,material,formattedLore,type,rarity); + Cosmetic cosmetic = new Cosmetic(internalname,name,modelID,material,formattedLore,type,rarity.get()); cosmeticsList.add(cosmetic); } Cosmetics.setCachedCosmeticList(cosmeticsList); diff --git a/src/main/java/com/ilm9001/cosmetics/util/Cosmetic.java b/src/main/java/com/ilm9001/cosmetics/util/Cosmetic.java index f508ca6..26f106f 100644 --- a/src/main/java/com/ilm9001/cosmetics/util/Cosmetic.java +++ b/src/main/java/com/ilm9001/cosmetics/util/Cosmetic.java @@ -17,7 +17,9 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; /** * Main Cosmetic class. @@ -120,12 +122,13 @@ public Cosmetic(String internalCosmeticName, Component cosmeticName, Integer mod PersistentDataContainer metaContainer = meta.getPersistentDataContainer(); Optional type; String internalname; - Rarity rarity; + AtomicReference rarity = new AtomicReference<>(); if(Util.isCosmetic(itemStack)) { type = CosmeticType.getFromID(metaContainer.get(typekey, PersistentDataType.BYTE)); internalname = metaContainer.get(namekey,PersistentDataType.STRING); - rarity = Rarities.valueOf(metaContainer.get(raritykey,PersistentDataType.STRING).toUpperCase()).getRarity(); + Rarities.getRarities().stream().filter((r) -> !Objects.equals(r.getInternalRarityName(), metaContainer.get(raritykey, PersistentDataType.STRING))) + .findFirst().ifPresent(rarity::set); } else { return null; } @@ -134,9 +137,9 @@ public Cosmetic(String internalCosmeticName, Component cosmeticName, Integer mod List lore = new ArrayList<>(); LegacyComponentSerializer serializer = LegacyComponentSerializer.builder().build(); meta.getLore().forEach((l) -> lore.add(serializer.deserialize(l))); - return new Cosmetic(internalname,Component.text(meta.getDisplayName()),meta.getCustomModelData(),itemStack.getType(),lore,type.get(),rarity); + return new Cosmetic(internalname,Component.text(meta.getDisplayName()),meta.getCustomModelData(),itemStack.getType(),lore,type.get(), rarity.get()); } - return new Cosmetic(internalname,meta.displayName(),meta.getCustomModelData(),itemStack.getType(),meta.lore(),type.get(),rarity); + return new Cosmetic(internalname,meta.displayName(),meta.getCustomModelData(),itemStack.getType(),meta.lore(),type.get(), rarity.get()); } else { return null; } diff --git a/src/main/resources/cosmetics.yml b/src/main/resources/cosmetics.yml index 867722e..e60ae6f 100644 --- a/src/main/resources/cosmetics.yml +++ b/src/main/resources/cosmetics.yml @@ -5,4 +5,4 @@ Cosmetics: lore: ["Test item"] # Lore to display material: PAPER # Material, recommended: PAPER, must be all uppercase type: HAT # Types: INVENTORY_ITEM, HAT, CHESTPLATE, LEGGINGS, BOOTS - rarity: LEGENDARY # Rarity, must be all Upper-case, Types: COMMON, UNCOMMON, RARE, EPIC, LEGENDARY \ No newline at end of file + rarity: LEGENDARY # Rarity, must be a rarity defined in rarities.yml \ No newline at end of file diff --git a/src/main/resources/rarities.yml b/src/main/resources/rarities.yml new file mode 100644 index 0000000..ee8511d --- /dev/null +++ b/src/main/resources/rarities.yml @@ -0,0 +1,16 @@ +Rarities: + COMMON: # Internally used name, something you can recognize across plugins, something like "COMMON, UNCOMMON, RARE, EPIC, LEGENDARY" + display: "\uF80A" # String to display as lore. You may use custom fonts as long as you format the unicode character correctly (such as "\uF80A") May be empty to not display rarity. + color: [156,156,156] # Color of the rarity. RGB 0-255. + UNCOMMON: + display: "\uF80B" + color: [ 113,204,27 ] + RARE: + display: "\uF80C" + color: [ 43,208,242 ] + EPIC: + display: "\uF80D" + color: [ 96,96,255 ] + LEGENDARY: + display: "\uF80E" + color: [ 255,185,32 ] \ No newline at end of file