Adds support for mythic enchants to be applied using the enchants stat.

This commit is contained in:
Ethan 2021-04-04 23:19:56 -04:00
parent e2ab12adc3
commit c82962cd61
3 changed files with 71 additions and 35 deletions

View File

@ -5,21 +5,23 @@ import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.api.util.LegacyComponent;
import io.lumine.mythic.lib.api.util.ui.FriendlyFeedbackProvider;
import io.lumine.mythicenchants.enchants.MythicEnchant;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.util.DynamicLore;
import net.Indyuce.mmoitems.api.util.message.FFPMMOItems;
import net.Indyuce.mmoitems.stat.data.EnchantListData;
import net.Indyuce.mmoitems.stat.data.MaterialData;
import net.Indyuce.mmoitems.stat.data.StringListData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.Indyuce.mmoitems.stat.type.StatHistory;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.attribute.AttributeModifier.Operation;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@ -163,7 +165,22 @@ public class ItemStackBuilder {
lore.insert("lore", parsed);
}
final List<String> list = lore.build();
List<String> list = lore.build();
if (MMOItems.plugin.getMythicEnchantsSupport() != null && mmoitem.hasData(ItemStats.ENCHANTS)) {
ItemStack metaItem = item.clone();
ItemMeta meta = metaItem.getItemMeta();
meta.setLore(list);
metaItem.setItemMeta(meta);
EnchantListData data = (EnchantListData) mmoitem.getData(ItemStats.ENCHANTS);
for (Enchantment enchant : data.getEnchants()) {
int lvl = data.getLevel(enchant);
if (lvl != 0 && enchant instanceof MythicEnchant) {
MMOItems.plugin.getMythicEnchantsSupport().handleEnchant(metaItem, enchant, lvl);
}
}
list = metaItem.getItemMeta().getLore();
}
JsonArray array = new JsonArray();
for (String s : list)
array.add(s);

View File

@ -1,16 +1,29 @@
package net.Indyuce.mmoitems.stat;
import java.util.*;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.SupportedNBTTagValues;
import io.lumine.mythic.lib.api.util.AltChar;
import io.lumine.mythic.lib.api.util.ui.FriendlyFeedbackCategory;
import io.lumine.mythic.lib.api.util.ui.FriendlyFeedbackProvider;
import io.lumine.mythic.lib.api.util.ui.PlusMinusPercent;
import io.lumine.mythic.lib.api.util.ui.SilentNumbers;
import io.lumine.mythicenchants.MythicEnchants;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.edition.StatEdition;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.api.util.message.FFPMMOItems;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.stat.data.EnchantListData;
import net.Indyuce.mmoitems.stat.data.random.RandomEnchantListData;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.data.type.UpgradeInfo;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.Indyuce.mmoitems.stat.type.StatHistory;
import net.Indyuce.mmoitems.stat.type.Upgradable;
import org.apache.commons.lang.Validate;
@ -21,24 +34,16 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.edition.StatEdition;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.stat.data.EnchantListData;
import net.Indyuce.mmoitems.stat.data.random.RandomEnchantListData;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import io.lumine.mythic.lib.api.util.AltChar;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public class Enchants extends ItemStat implements Upgradable {
public Enchants() {
super("ENCHANTS", Material.ENCHANTED_BOOK, "Enchantments", new String[] { "The item enchants." }, new String[] { "all" });
@ -74,15 +79,13 @@ public class Enchants extends ItemStat implements Upgradable {
* getByName is deprecated, but it's safe to
* use and will make the user experience better
*/
@SuppressWarnings("deprecation")
@Override
public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) {
String[] split = message.split(" ");
Validate.isTrue(split.length >= 2, "Use this format: {Enchant Name} {Enchant Level Numeric Formula}. Example: 'sharpness 5 0.3' "
+ "stands for Sharpness 5, plus 0.3 level per item level (rounded up to lower integer)");
Enchantment enchant = Enchantment.getByKey(NamespacedKey.minecraft(split[0].toLowerCase().replace("-", "_")));
if(enchant == null) enchant = Enchantment.getByName(split[0].toUpperCase());
Enchantment enchant = getEnchant(split[0]);
Validate.notNull(enchant, split[0]
+ " is not a valid enchantment! All enchants can be found here: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/enchantments/Enchantment.html");
@ -189,7 +192,7 @@ public class Enchants extends ItemStat implements Upgradable {
// Get Namespaced
Enchantment ench = null;
try { ench = Enchantment.getByKey(NamespacedKey.minecraft(enchantment)); } catch (Exception ignored) {}
try { ench = getEnchant(enchantment); } catch (Exception ignored) {}
// Parse Integer
Integer lvl = SilentNumbers.IntegerParse(level);
@ -414,6 +417,23 @@ public class Enchants extends ItemStat implements Upgradable {
}
}
@SuppressWarnings("deprecation")
public static Enchantment getEnchant(String key) {
key = key.toLowerCase().replace("-", "_");
Enchantment enchant = Enchantment.getByKey(NamespacedKey.minecraft(key));
if (enchant == null) {
if (MMOItems.plugin.getMythicEnchantsSupport() != null) {
enchant = Enchantment.getByKey(new NamespacedKey(MythicEnchants.inst(), key));
if (enchant != null)
return enchant;
}
enchant = Enchantment.getByName(key);
}
return enchant;
}
public static class EnchantUpgradeInfo implements UpgradeInfo {
@NotNull HashMap<Enchantment, PlusMinusPercent> perEnchantmentOperations = new HashMap<>();
@ -494,7 +514,7 @@ public class Enchants extends ItemStat implements Upgradable {
PlusMinusPercent pmpRead = PlusMinusPercent.getFromString(pmpStr, ffp);
Enchantment ench = null;
try { ench = Enchantment.getByKey(NamespacedKey.minecraft(enchStr)); } catch (Exception ignored) {}
try { ench = getEnchant(enchStr); } catch (Exception ignored) {}
// L
if (pmpRead == null) { unreadableStatements.append(' ').append(ffp.getFeedbackOf(FriendlyFeedbackCategory.ERROR).get(0).forConsole(ffp.getPalette())); failure = true; }

View File

@ -1,19 +1,18 @@
package net.Indyuce.mmoitems.stat.data.random;
import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.stat.Enchants;
import net.Indyuce.mmoitems.stat.data.EnchantListData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.Validate;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.stat.data.EnchantListData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
public class RandomEnchantListData implements RandomStatData {
private final Map<Enchantment, NumericStatFormula> enchants = new HashMap<>();
@ -21,7 +20,7 @@ public class RandomEnchantListData implements RandomStatData {
Validate.notNull(config, "Config cannot be null");
for (String key : config.getKeys(false)) {
Enchantment enchant = Enchantment.getByKey(NamespacedKey.minecraft(key.toLowerCase().replace("-", "_")));
Enchantment enchant = Enchants.getEnchant(key);
Validate.notNull(enchant, "Could not find enchant with key '" + key + "'");
addEnchant(enchant, new NumericStatFormula(config.get(key)));
}