Skip to content

Commit

Permalink
implement customizable rarities, no longer hardcoded
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-hydrogen committed Apr 21, 2022
1 parent bf9e575 commit 45ce71a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 40 deletions.
37 changes: 31 additions & 6 deletions src/main/java/com/ilm9001/cosmetics/Cosmetics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,23 +25,30 @@ public class Cosmetics extends JavaPlugin {
private static List<Cosmetic> cosmeticList;

private static FileConfiguration cosmetics;
private static FileConfiguration rarities;
private static File cosmeticsFile;

private static File raritiesFile;

// Static abuse? No, Static abusage.

@Override
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());
Expand Down Expand Up @@ -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();
}
Expand Down
56 changes: 32 additions & 24 deletions src/main/java/com/ilm9001/cosmetics/rarity/Rarities.java
Original file line number Diff line number Diff line change
@@ -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<Rarity> rarities = new ArrayList<>();

public static void setRaritiesFromFile() {
List<Cosmetic> cosmeticsList = new ArrayList<>();
Cosmetics.refreshFiles();
FileConfiguration config = Cosmetics.getRarities();

for (String internalname : config.getConfigurationSection("Rarities").getKeys(false)) {
Component displayName;
TextColor color;
Map<String,Object> valuesmap = config.getConfigurationSection("Cosmetics."+internalname).getValues(false);;
List<Number> colorList;
if(valuesmap.get("color") instanceof List && valuesmap.get("color") != null) {
colorList = (List<Number>) 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<Rarity> getRarities() {
return rarities;
}
}
12 changes: 7 additions & 5 deletions src/main/java/com/ilm9001/cosmetics/summon/CosmeticFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> cosmeticNames;
Expand All @@ -34,7 +35,7 @@ public CosmeticFactory() {
*/
public @NotNull List<Cosmetic> getCosmeticsFromConfig() {
List<Cosmetic> cosmeticsList = new ArrayList<>();
Cosmetics.refreshCosmetics();
Cosmetics.refreshFiles();
FileConfiguration config = Cosmetics.getCosmetics();

for (String internalname : config.getConfigurationSection("Cosmetics").getKeys(false)) {
Expand All @@ -45,7 +46,7 @@ public CosmeticFactory() {
List<Component> formattedLore;
CosmeticType type;
Map<String,Object> valuesmap;
Rarity rarity;
AtomicReference<Rarity> rarity = new AtomicReference<>();

cosmeticCount++;
cosmeticNames.add(internalname);
Expand All @@ -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;
Expand All @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/ilm9001/cosmetics/util/Cosmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -120,12 +122,13 @@ public Cosmetic(String internalCosmeticName, Component cosmeticName, Integer mod
PersistentDataContainer metaContainer = meta.getPersistentDataContainer();
Optional<CosmeticType> type;
String internalname;
Rarity rarity;
AtomicReference<Rarity> 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;
}
Expand All @@ -134,9 +137,9 @@ public Cosmetic(String internalCosmeticName, Component cosmeticName, Integer mod
List<Component> 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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/cosmetics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
rarity: LEGENDARY # Rarity, must be a rarity defined in rarities.yml
16 changes: 16 additions & 0 deletions src/main/resources/rarities.yml
Original file line number Diff line number Diff line change
@@ -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 ]

0 comments on commit 45ce71a

Please sign in to comment.