Fixed unidentified items not display item tier

This commit is contained in:
Indyuce 2022-10-02 13:04:53 +02:00
parent 4cba417cce
commit dfa13fe137
3 changed files with 219 additions and 186 deletions

View File

@ -2,10 +2,11 @@ package net.Indyuce.mmoitems.api;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.api.item.NBTItem;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.droptable.DropTable;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
@ -17,133 +18,166 @@ import java.util.List;
import java.util.Random;
public class ItemTier {
@NotNull private final String name, id;
private final String name, id;
private final UnidentificationInfo unidentificationInfo;
// Unidentification
@NotNull private final UnidentificationInfo unidentificationInfo;
// Deconstruction
@Nullable
private final DropTable deconstructTable;
// deconstruction
@Nullable private final DropTable deconstruct;
// Item glow options
@Nullable
private final ChatColor glowColor;
private final boolean itemHint;
// item glow options
@Nullable private ChatColor color = null;
private boolean hint = false;
// Item generation
@Nullable
private final NumericStatFormula capacity;
private final double chance;
// item generation
private final double chance;
@Nullable private final NumericStatFormula capacity;
@NotNull
private static final Random RANDOM = new Random();
@NotNull private static final Random RANDOM = new Random();
private static final boolean GLOW = Bukkit.getPluginManager().getPlugin("GlowAPI") != null;
/**
* Load an ItemTier from the YML Configuration Itself
*
* @param config Configuration section to get all values from
*/
public ItemTier(@NotNull ConfigurationSection config) {
/**
* Load an ItemTier from the YML Configuration Itself
*
* @param config Configuration section to get all values from
*/
public ItemTier(@NotNull ConfigurationSection config) {
// The name and ID, crucial parts.
id = config.getName().toUpperCase().replace("-", "_");
name = MythicLib.plugin.parseColors(config.getString("name"));
// The name and ID, crucial parts.
id = config.getName().toUpperCase().replace("-", "_");
name = MythicLib.plugin.parseColors(config.getString("name"));
// Deconstruct and Unidentification
deconstructTable = config.contains("deconstruct-item") ? new DropTable(config.getConfigurationSection("deconstruct-item")) : null;
// Deconstruct and Unidentification
deconstruct = config.contains("deconstruct-item") ? new DropTable(config.getConfigurationSection("deconstruct-item")) : null;
final ConfigurationSection unidentificationSection = config.getConfigurationSection("unidentification");
unidentificationInfo = unidentificationSection == null ? UnidentificationInfo.DEFAULT : new UnidentificationInfo(unidentificationSection);
ConfigurationSection unidentificationSection = config.getConfigurationSection("unidentification");
if (unidentificationSection == null) { unidentificationInfo = getDefaultUnident(); }
else { unidentificationInfo = new UnidentificationInfo(unidentificationSection); }
if (config.contains("item-glow")) {
itemHint = config.getBoolean("item-glow.hint");
glowColor = ChatColor.valueOf(UtilityMethods.enumName(config.getString("item-glow.color", "WHITE")));
} else {
itemHint = false;
glowColor = null;
}
if (config.contains("item-glow")) {
hint = config.getBoolean("item-glow.hint");
color = ChatColor.valueOf(UtilityMethods.enumName(config.getString("item-glow.color", "WHITE")));
} else {
hint = false;
color = null;
}
// What are the chances?
chance = config.getDouble("generation.chance");
capacity = config.contains("generation.capacity") ? new NumericStatFormula(config.get("generation.capacity")) : null;
}
// What are the chances?
chance = config.getDouble("generation.chance");
capacity = config.contains("generation.capacity") ? new NumericStatFormula(config.get("generation.capacity")) : null;
}
@NotNull
public String getId() {
return id;
}
@NotNull public String getId() { return id; }
@NotNull
public String getName() {
return name;
}
@NotNull public String getName() { return name; }
public boolean hasDropTable() {
return deconstructTable != null;
}
public boolean hasDropTable() { return deconstruct != null; }
@Nullable
public DropTable getDropTable() {
return deconstructTable;
}
@Nullable public DropTable getDropTable() { return deconstruct; }
/**
* @return Reads the deconstruction drop table. This may return a list
* containing multiple items and they should all be added to the
* player's inventory
*/
public List<ItemStack> getDeconstructedLoot(@NotNull PlayerData player) {
//noinspection ConstantConditions
return hasDropTable() ? deconstructTable.read(player, false) : new ArrayList<>();
}
public boolean hasColor() { return color != null; }
public boolean hasColor() {
return glowColor != null;
}
@Nullable public ChatColor getColor() { return color; }
@Nullable
public ChatColor getColor() {
return glowColor;
}
public boolean isHintEnabled() { return hint; }
public boolean isHintEnabled() {
return itemHint;
}
/**
* @return The chance of the tier being chosen when generating a random item
*/
public double getGenerationChance() { return chance; }
/**
* @return The chance of the tier being chosen when generating a random item
*/
public double getGenerationChance() {
return chance;
}
/**
* @return If the item tier has a modifier capacity ie if this tier let
* generated items have modifiers
*/
public boolean hasCapacity() { return capacity != null; }
/**
* @return If the item tier has a modifier capacity ie if this tier let
* generated items have modifiers
*/
public boolean hasCapacity() {
return capacity != null;
}
/**
* @return The formula for modifier capacity which can be then rolled to
* generate a random amount of modifier capacity when generating a
* random item
*/
@Nullable public NumericStatFormula getModifierCapacity() { return capacity; }
/**
* @return The formula for modifier capacity which can be then rolled to
* generate a random amount of modifier capacity when generating a
* random item
*/
@Nullable
public NumericStatFormula getModifierCapacity() {
return capacity;
}
@NotNull public UnidentificationInfo getUnidentificationInfo() { return unidentificationInfo; }
@NotNull
public UnidentificationInfo getUnidentificationInfo() {
return unidentificationInfo;
}
/**
* @return Reads the deconstruction drop table. This may return a list
* containing multiple items and they should all be added to the
* player's inventory
*/
public List<ItemStack> getDeconstructedLoot(@NotNull PlayerData player) {
//noinspection ConstantConditions
return hasDropTable() ? deconstruct.read(player, false) : new ArrayList<>();
}
public static class UnidentificationInfo {
@NotNull
private final String name, prefix;
private final int range;
/**
* @return Default unidentification info, if it is missing in the config.
*/
@NotNull private UnidentificationInfo getDefaultUnident() { return new UnidentificationInfo(UnidentificationInfo.UNIDENT_NAME, UnidentificationInfo.UNIDENT_PREFIX, 0); }
public static final String DEFAULT_NAME = "Unidentified Item";
public static final String DEFAULT_PREFIX = "Unknown";
public static final UnidentificationInfo DEFAULT = new UnidentificationInfo(UnidentificationInfo.DEFAULT_NAME, UnidentificationInfo.DEFAULT_PREFIX, 0);
public class UnidentificationInfo {
@NotNull
private final String name, prefix;
private final int range;
public UnidentificationInfo(@NotNull ConfigurationSection config) {
this(config.getString("name", DEFAULT_NAME), config.getString("prefix", DEFAULT_PREFIX), config.getInt("range"));
}
public static final String UNIDENT_NAME = "Unidentified Item";
public static final String UNIDENT_PREFIX = "Unknown";
public UnidentificationInfo(@NotNull String name, @NotNull String prefix, int range) {
this.name = name;
this.prefix = prefix;
this.range = range;
}
public UnidentificationInfo(@NotNull ConfigurationSection config) {
this(config.getString("name", UNIDENT_NAME), config.getString("prefix", UNIDENT_PREFIX), config.getInt("range"));
}
@NotNull
public String getPrefix() {
return prefix;
}
public UnidentificationInfo(@NotNull String name, @NotNull String prefix, int range) {
this.name = name;
this.prefix = prefix;
this.range = range;
}
@NotNull
public String getDisplayName() {
return name;
}
@NotNull public String getPrefix() { return prefix; }
public int[] calculateRange(int level) {
int min = (int) Math.max(1, (level - (double) range * RANDOM.nextDouble()));
return new int[]{min, min + range};
}
}
@NotNull public String getDisplayName() {
return name;
}
public int[] calculateRange(int level) {
int min = (int) Math.max(1, (level - (double) range * RANDOM.nextDouble()));
return new int[] { min, min + range };
}
}
@Nullable
public static ItemTier ofItem(NBTItem item) {
final @Nullable String format = item.getString("MMOITEMS_TIER");
return format == null ? null : MMOItems.plugin.getTiers().get(format);
}
}

View File

@ -223,8 +223,7 @@ public class MMOItem implements ItemReference {
*/
@Nullable
public ItemTier getTier() {
final @Nullable StatData found = stats.get(ItemStats.TIER);
return found == null ? null : MMOItems.plugin.getTiers().get(found.toString());
return hasData(ItemStats.TIER) ? MMOItems.plugin.getTiers().get(stats.get(ItemStats.TIER).toString()) : null;
}
/**

View File

@ -4,7 +4,6 @@ import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.NBTItem;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ItemTier;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
@ -15,108 +14,109 @@ import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.io.BukkitObjectOutputStream;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import java.io.ByteArrayOutputStream;
import java.util.*;
public class UnidentifiedItem extends ConfigItem {
public UnidentifiedItem(Type type) {
super("unidentified", type.getItem().getType());
public UnidentifiedItem(Type type) {
super("unidentified", type.getItem().getType());
setName("#prefix#Unidentified " + type.getName());
setLore(Arrays.asList("&7This item is unidentified. I must", "&7find a way to identify it!", "{tier}", "{tier}&8Item Info:",
"{range}&8- &7Lvl Range: &e#range#", "{tier}&8- &7Item Tier: #prefix##tier#"));
}
setName("#prefix#Unidentified " + type.getName());
setLore(Arrays.asList("&7This item is unidentified. I must", "&7find a way to identify it!", "{tier}", "{tier}&8Item Info:",
"{range}&8- &7Lvl Range: &e#range#", "{tier}&8- &7Item Tier: #prefix##tier#"));
}
public ItemBuilder newBuilder(NBTItem item) {
return new ItemBuilder(item);
}
public ItemBuilder newBuilder(NBTItem item) {
return new ItemBuilder(item);
}
/*
* allows to build an unidentified item based on the given NBTItem.
*/
public class ItemBuilder {
private final int amount;
private final NBTItem item;
/**
* Allows to build an unidentified item based on the given NBTItem.
*/
public class ItemBuilder {
private final int amount;
private final NBTItem item;
private String name = getName();
private final List<String> lore = new ArrayList<>(getLore());
private String name = getName();
private final List<String> lore = new ArrayList<>(getLore());
public ItemBuilder(NBTItem item) {
this.amount = item.getItem().getAmount();
this.item = item;
}
public ItemBuilder(NBTItem item) {
this.amount = item.getItem().getAmount();
this.item = item;
}
// {tier} only displays when tier
// {level} only displays when level + tier
public ItemStack build() {
// {tier} only displays when tier
// {level} only displays when level + tier
public ItemStack build() {
/*
* load item data
*/
MMOItem mmoitem = new VolatileMMOItem(item);
ItemTier tier = mmoitem.getTier();
int level = mmoitem.hasData(ItemStats.REQUIRED_LEVEL) ? (int) ((DoubleData) mmoitem.getData(ItemStats.REQUIRED_LEVEL)).getValue() : -1;
// Load item data
final MMOItem mmoitem = new VolatileMMOItem(item);
final @Nullable ItemTier tier = ItemTier.ofItem(item);
final int level = mmoitem.hasData(ItemStats.REQUIRED_LEVEL) ? (int) ((DoubleData) mmoitem.getData(ItemStats.REQUIRED_LEVEL)).getValue() : -1;
/*
* load placeholders
*/
Map<String, String> placeholders = new HashMap<>();
if (tier != null) {
placeholders.put("prefix", tier.getUnidentificationInfo().getPrefix());
placeholders.put("tier", tier.getUnidentificationInfo().getDisplayName());
// Load placeholders
Map<String, String> placeholders = new HashMap<>();
if (tier != null) {
placeholders.put("prefix", tier.getUnidentificationInfo().getPrefix());
placeholders.put("tier", tier.getUnidentificationInfo().getDisplayName());
if (level > -1) {
int[] range = tier.getUnidentificationInfo().calculateRange(level);
placeholders.put("range", range[0] + "-" + range[1]);
}
} else
name = name.replace("#prefix#", "");
if (level > -1) {
int[] range = tier.getUnidentificationInfo().calculateRange(level);
placeholders.put("range", range[0] + "-" + range[1]);
}
} else
name = name.replace("#prefix#", "");
// Remove useless lore lines
lore.removeIf(s -> (s.startsWith("{tier}") && tier == null) || (s.startsWith("{range}") && (tier == null || level < 0)));
// Remove useless lore lines
lore.removeIf(s -> (s.startsWith("{tier}") && tier == null) || (s.startsWith("{range}") && (tier == null || level < 0)));
// Apply placeholders
for (String placeholder : placeholders.keySet())
name = name.replace("#" + placeholder + "#", placeholders.get(placeholder));
for (int n = 0; n < lore.size(); n++) {
String str = lore.get(n);
for (String placeholder : placeholders.keySet())
str = str.replace("#" + placeholder + "#", placeholders.get(placeholder));
lore.set(n, MythicLib.plugin.parseColors(str.replace("{range}", "").replace("{tier}", "")));
}
// Apply placeholders
for (String placeholder : placeholders.keySet())
name = name.replace("#" + placeholder + "#", placeholders.get(placeholder));
for (int n = 0; n < lore.size(); n++) {
String str = lore.get(n);
for (String placeholder : placeholders.keySet())
str = str.replace("#" + placeholder + "#", placeholders.get(placeholder));
lore.set(n, MythicLib.plugin.parseColors(str.replace("{range}", "").replace("{tier}", "")));
}
// Apply changes to item
item.getItem().setAmount(1);
ItemStack unidentified = MythicLib.plugin.getVersion().getWrapper().copyTexture(item)
.addTag(new ItemTag("MMOITEMS_UNIDENTIFIED_ITEM", serialize(item.toItem()))).toItem();
unidentified.setAmount(amount);
ItemMeta meta = unidentified.getItemMeta();
meta.addItemFlags(ItemFlag.values());
meta.setUnbreakable(true);
meta.setDisplayName(MythicLib.plugin.parseColors(name));
meta.setLore(lore);
if (customModelData != null) { meta.setCustomModelData(customModelData);}
unidentified.setItemMeta(meta);
// Apply changes to item
item.getItem().setAmount(1);
ItemStack unidentified = MythicLib.plugin.getVersion().getWrapper().copyTexture(item)
.addTag(new ItemTag("MMOITEMS_UNIDENTIFIED_ITEM", serialize(item.toItem()))).toItem();
unidentified.setAmount(amount);
ItemMeta meta = unidentified.getItemMeta();
meta.addItemFlags(ItemFlag.values());
meta.setUnbreakable(true);
meta.setDisplayName(MythicLib.plugin.parseColors(name));
meta.setLore(lore);
if (customModelData != null) {
meta.setCustomModelData(customModelData);
}
unidentified.setItemMeta(meta);
// Has model?
if (material != null && material.isItem()) { unidentified.setType(material); }
// Has model?
if (material != null && material.isItem()) {
unidentified.setType(material);
}
return unidentified;
}
return unidentified;
}
private String serialize(ItemStack item) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
dataOutput.writeObject(item);
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
private String serialize(ItemStack item) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
dataOutput.writeObject(item);
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
}