mirror of
https://gitlab.com/phoenix-dvpmt/mmoitems.git
synced 2025-02-01 11:11:21 +01:00
Fixed AE enchants not transfering with gem stones/upgrading
This commit is contained in:
parent
354a5f9336
commit
265d889195
@ -197,11 +197,11 @@
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- AE Api -->
|
||||
<!-- AdvancedEnchants API -->
|
||||
<dependency>
|
||||
<groupId>n3kas</groupId>
|
||||
<groupId>net.advancedplugins</groupId>
|
||||
<artifactId>ae.api</artifactId>
|
||||
<version>5.7.6</version>
|
||||
<version>8.7.4</version>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
@ -56,6 +56,17 @@ public class LoreBuilder extends Buildable<List<String>> {
|
||||
lore.add(index, element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts specific lines at a specific index in the item lore.
|
||||
* Used by custom enchantment plugins to add enchant display to item lore.
|
||||
*
|
||||
* @param index Index of insertion
|
||||
* @param elements Strings to insert
|
||||
*/
|
||||
public void insert(int index, @NotNull Collection<String> elements) {
|
||||
lore.addAll(index, elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a list of strings in the item lore. The lines are added only if a
|
||||
* line #item-stat-id# can be found in the lore format.
|
||||
|
@ -3,74 +3,69 @@ package net.Indyuce.mmoitems.comp.enchants.advanced_enchants;
|
||||
import io.lumine.mythic.lib.api.item.ItemTag;
|
||||
import io.lumine.mythic.lib.api.item.SupportedNBTTagValues;
|
||||
import io.lumine.mythic.lib.api.util.ui.SilentNumbers;
|
||||
import io.lumine.mythic.lib.util.annotation.BackwardsCompatibility;
|
||||
import io.lumine.mythic.lib.version.VersionMaterial;
|
||||
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
|
||||
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
|
||||
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
|
||||
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
|
||||
import net.Indyuce.mmoitems.stat.data.type.StatData;
|
||||
import net.Indyuce.mmoitems.stat.type.InternalStat;
|
||||
import net.Indyuce.mmoitems.stat.type.ItemStat;
|
||||
import net.advancedplugins.ae.enchanthandler.enchantments.AEnchants;
|
||||
import net.advancedplugins.ae.enchanthandler.enchantments.AdvancedEnchantment;
|
||||
import net.advancedplugins.ae.api.AEAPI;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* List of enchantments in an item yes
|
||||
*/
|
||||
public class AdvancedEnchantsStat extends ItemStat<RandomStatData<AdvancedEnchantMap>, AdvancedEnchantMap> implements InternalStat {
|
||||
private final Plugin ae;
|
||||
private final String aeNamespace;
|
||||
|
||||
public AdvancedEnchantsStat() {
|
||||
super("ADVANCED_ENCHANTS", VersionMaterial.EXPERIENCE_BOTTLE.toMaterial(), "Advanced Enchants", new String[]{"The AEnchants of this item. Format:", "\u00a7e[internal_name] [level]"}, new String[]{"!miscellaneous", "!block", "all"});
|
||||
|
||||
ae = Bukkit.getPluginManager().getPlugin("AdvancedEnchantments");
|
||||
Validate.notNull(ae, "Could not find plugin AdvancedEnchants");
|
||||
aeNamespace = new NamespacedKey(ae, "any").getNamespace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomStatData whenInitialized(Object object) {
|
||||
public RandomStatData<AdvancedEnchantMap> whenInitialized(Object object) {
|
||||
throw new RuntimeException("Not supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull AdvancedEnchantMap data) {
|
||||
|
||||
// Do that
|
||||
Map<String, Integer> aes = data.enchants;
|
||||
|
||||
// Enchant the item
|
||||
for (String ench : aes.keySet()) {
|
||||
|
||||
Integer lvl = aes.get(ench);
|
||||
data.enchants.forEach((ench, lvl) -> {
|
||||
|
||||
// Skip trash
|
||||
if (ench == null || lvl == null || ench.isEmpty())
|
||||
continue;
|
||||
|
||||
// Find the actual enchantment and include lore
|
||||
AdvancedEnchantment instance = AEnchants.matchEnchant(ench);
|
||||
if (instance == null)
|
||||
return;
|
||||
if (ench == null || lvl == null || ench.isEmpty()) return;
|
||||
|
||||
// Add lore and tag
|
||||
item.getLore().insert(0, instance.getDisplay(lvl));
|
||||
item.addItemTag(getEnchantTag(ench, lvl));
|
||||
}
|
||||
item.getLore().insert(0, AEAPI.getEnchantLore(ench, lvl));
|
||||
item.getMeta().getPersistentDataContainer().set(new NamespacedKey(ae, AE_KEY_PREFIX + ench), PersistentDataType.INTEGER, lvl);
|
||||
});
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ArrayList<ItemTag> getAppliedNBT(@NotNull AdvancedEnchantMap data) {
|
||||
ArrayList<ItemTag> array = new ArrayList<>();
|
||||
Map<String, Integer> aes = ((AdvancedEnchantMap) data).enchants;
|
||||
|
||||
for (String ench : aes.keySet())
|
||||
array.add(getEnchantTag(ench, aes.get(ench)));
|
||||
|
||||
return array;
|
||||
// Stat history is not supported
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -94,74 +89,50 @@ public class AdvancedEnchantsStat extends ItemStat<RandomStatData<AdvancedEnchan
|
||||
return new AdvancedEnchantMap();
|
||||
}
|
||||
|
||||
private static final String AE_TAG = "ae_enchantment";
|
||||
private static final String AE_KEY_PREFIX = "ae_enchantment-";
|
||||
|
||||
/**
|
||||
* @param name Name of the AEnch ~ arrow_deflect
|
||||
* @param level Level of the AEnch ~ 4
|
||||
* @return The tag ~ "ae_enchantment;arrow_deflect": 4
|
||||
*/
|
||||
private ItemTag getEnchantTag(@NotNull String name, int level) {
|
||||
return new ItemTag(AE_TAG + ";" + name, level);
|
||||
}
|
||||
|
||||
@BackwardsCompatibility(version = "i_love_this_plugin_sooooooooo_much_so_so_much")
|
||||
private static final String LEGACY_AE_KEY_PREFIX = "ae_enchantment;";
|
||||
|
||||
@Override
|
||||
public void whenLoaded(@NotNull ReadMMOItem mmoitem) {
|
||||
final ItemMeta meta = mmoitem.getNBT().getItem().getItemMeta();
|
||||
if (meta == null) return;
|
||||
|
||||
// Look at all tags that start with ae_enchantment I guess
|
||||
ArrayList<ItemTag> relevantTags = new ArrayList<>();
|
||||
final AdvancedEnchantMap enchants = new AdvancedEnchantMap();
|
||||
|
||||
// Backwards compatibility
|
||||
for (String tag : mmoitem.getNBT().getTags()) {
|
||||
if (tag == null) continue;
|
||||
if (!tag.startsWith(LEGACY_AE_KEY_PREFIX)) continue;
|
||||
|
||||
// Valid tag?
|
||||
if (tag == null) {
|
||||
continue;
|
||||
}
|
||||
if (tag.startsWith(AE_TAG)) {
|
||||
|
||||
ItemTag thatTag = ItemTag.getTagAtPath(tag, mmoitem.getNBT(), SupportedNBTTagValues.DOUBLE);
|
||||
|
||||
// Not null?
|
||||
if (thatTag != null) {
|
||||
|
||||
relevantTags.add(new ItemTag(tag, SilentNumbers.round((Double) thatTag.getValue())));
|
||||
}
|
||||
}
|
||||
final String enchantTag = tag.substring(LEGACY_AE_KEY_PREFIX.length());
|
||||
final int lvl = mmoitem.getNBT().getInteger(tag);
|
||||
enchants.enchants.put(enchantTag, lvl);
|
||||
}
|
||||
|
||||
// Generate data
|
||||
StatData data = getLoadedNBT(relevantTags);
|
||||
for (NamespacedKey nsk : meta.getPersistentDataContainer().getKeys()) {
|
||||
if (!nsk.getNamespace().equals(aeNamespace)) continue;
|
||||
|
||||
// Valid?
|
||||
if (data != null)
|
||||
final String tag = nsk.getKey();
|
||||
if (!tag.startsWith(AE_KEY_PREFIX)) continue;
|
||||
|
||||
// This data has the enchantments that used to be stored in the item.
|
||||
mmoitem.setData(this, data);
|
||||
final Integer lvlInteger = meta.getPersistentDataContainer().get(nsk, PersistentDataType.INTEGER);
|
||||
if (lvlInteger == null || lvlInteger == 0) continue;
|
||||
|
||||
final String enchantTag = tag.substring(AE_KEY_PREFIX.length());
|
||||
enchants.enchants.put(enchantTag, lvlInteger);
|
||||
}
|
||||
|
||||
// Not empty?
|
||||
if (!enchants.enchants.isEmpty()) mmoitem.setData(this, enchants);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public AdvancedEnchantMap getLoadedNBT(@NotNull ArrayList<ItemTag> storedTags) {
|
||||
AdvancedEnchantMap enchants = new AdvancedEnchantMap();
|
||||
|
||||
// Yes
|
||||
for (ItemTag tag : storedTags) {
|
||||
if (tag == null || !(tag.getValue() instanceof Integer))
|
||||
continue;
|
||||
|
||||
// Path must be valid
|
||||
int spc = tag.getPath().indexOf(AE_TAG + ";");
|
||||
if (spc < 0)
|
||||
continue;
|
||||
|
||||
// Crop
|
||||
String enchantment = tag.getPath().substring(AE_TAG.length() + 1 + spc);
|
||||
int value = (int) tag.getValue();
|
||||
|
||||
// Save enchant in map
|
||||
enchants.enchants.put(enchantment, value);
|
||||
}
|
||||
|
||||
// Thats it
|
||||
return enchants.enchants.size() == 0 ? null : enchants;
|
||||
// Stat history is not supported
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user