Fixed lore duplication from creative mode

Reworked /ecoskip
This commit is contained in:
Auxilor 2020-08-28 19:35:53 +01:00
parent d46f715bab
commit bf92af0a93
3 changed files with 53 additions and 29 deletions

View File

@ -44,7 +44,7 @@ public class DisplayPacketAdapter extends PacketAdapter {
if(!event.getPacketType().equals(PacketType.Play.Client.SET_CREATIVE_SLOT)) return;
event.getPacket().getItemModifier().modify(0, (item) -> {
item = EnchantDisplay.displayEnchantments(item);
item = EnchantDisplay.revertDisplay(item);
return item;
});
}

View File

@ -34,12 +34,7 @@ public class EnchantDisplay {
private static final NamespacedKey key = new NamespacedKey(Main.getInstance(), "ecoenchantlore-len");
/**
* The meta key of the length of advancedenchantments enchantments in lore
*/
private static final NamespacedKey keyAE = new NamespacedKey(Main.getInstance(), "ecoenchantlore-aelen");
/**
* The meta key to function like {@link ItemFlag#HIDE_ENCHANTS}
* The meta key to hide enchantments in lore
*/
public static final NamespacedKey keySkip = new NamespacedKey(Main.getInstance(), "ecoenchantlore-skip");
@ -81,6 +76,51 @@ public class EnchantDisplay {
shrinkPerLine = ConfigManager.getConfig().getInt("lore.shrink.maximum-per-line");
}
/**
* Revert display
* @param item The item to revert
* @return The item, updated
*/
public static ItemStack revertDisplay(ItemStack item) {
if(item == null) return null;
if(!Target.Applicable.ALL.getMaterials().contains(item.getType()))
return item;
ItemMeta meta = item.getItemMeta();
List<String> itemLore = new ArrayList<>();
if(meta == null) return item;
if(meta.hasLore())
itemLore = meta.getLore();
try {
if (meta.getPersistentDataContainer().has(key, PersistentDataType.INTEGER)) {
int enchantLoreLength = meta.getPersistentDataContainer().get(key, PersistentDataType.INTEGER);
if(itemLore.size() >= enchantLoreLength) {
itemLore.subList(0, enchantLoreLength).clear();
}
}
} catch (NullPointerException ignored) {}
if (meta instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta metaBook = (EnchantmentStorageMeta) meta;
metaBook.removeItemFlags(ItemFlag.HIDE_POTION_EFFECTS); // Thanks ShaneBee!
metaBook.removeItemFlags(ItemFlag.HIDE_ENCHANTS); // Here just in case
metaBook.setLore(itemLore);
item.setItemMeta(metaBook);
} else {
meta.removeItemFlags(ItemFlag.HIDE_ENCHANTS);
meta.setLore(itemLore);
item.setItemMeta(meta);
}
return item;
}
/**
* Show all enchantments in item lore
* @param item The item to update
@ -94,6 +134,8 @@ public class EnchantDisplay {
if(!Target.Applicable.ALL.getMaterials().contains(item.getType()))
return oldItem;
item = revertDisplay(item);
ItemMeta meta = item.getItemMeta();
List<String> itemLore = new ArrayList<>();
@ -105,7 +147,7 @@ public class EnchantDisplay {
if(meta == null) return oldItem;
if(meta.getPersistentDataContainer().has(keySkip, PersistentDataType.INTEGER))
return item;
return oldItem;
if(meta.hasLore())
itemLore = meta.getLore();
@ -115,25 +157,6 @@ public class EnchantDisplay {
List<String> enchantLore = new ArrayList<>();
try {
if (meta.getPersistentDataContainer().has(key, PersistentDataType.INTEGER)) {
int enchantLoreLength = meta.getPersistentDataContainer().get(key, PersistentDataType.INTEGER);
int loreEnd = loreStart;
if (meta.getPersistentDataContainer().has(keyAE, PersistentDataType.INTEGER)) {
int oldAEenchantLoreLength = meta.getPersistentDataContainer().get(keyAE, PersistentDataType.INTEGER);
loreEnd += oldAEenchantLoreLength;
}
if(itemLore.size() >= loreStart + enchantLoreLength + loreEnd) {
itemLore.subList(loreStart, enchantLoreLength + loreEnd).clear();
}
}
} catch (NullPointerException ignored) {}
if(Main.hasAE) {
int totalAE = AEAPI.getEnchantmentsOnItem(item).size();
meta.getPersistentDataContainer().set(key, PersistentDataType.INTEGER, totalAE);
}
Map<Enchantment, Integer> enchantments;
List<Enchantment> forRemoval = new ArrayList<>();
@ -143,6 +166,7 @@ public class EnchantDisplay {
enchantments = meta.getEnchants();
}
final ItemStack finalItem = item;
enchantments.forEach(((enchantment, integer) -> {
boolean isEcoEnchant = EcoEnchants.getFromEnchantment(enchantment) != null;
@ -189,7 +213,7 @@ public class EnchantDisplay {
}
if(!(isMaxLevelOne || type == EcoEnchant.EnchantmentType.CURSE)) {
if (useNumerals && item.getEnchantmentLevel(enchantment) < numbersThreshold) {
if (useNumerals && finalItem.getEnchantmentLevel(enchantment) < numbersThreshold) {
name += " " + Numeral.getNumeral(integer);
} else {
name += " " + integer;

View File

@ -374,8 +374,8 @@ public class Loader {
Bukkit.getLogger().info("Loading Commands...");
Bukkit.getPluginCommand("ecoreload").setExecutor(new CommandEcoreload());
Bukkit.getPluginCommand("ecodebug").setExecutor(new CommandEcodebug());
Bukkit.getPluginCommand("ecoskip").setExecutor(new CommandEcoskip());
Bukkit.getPluginCommand("enchantinfo").setExecutor(new CommandEnchantinfo());
Bukkit.getPluginCommand("ecoskip").setExecutor(new CommandEcoskip());
Bukkit.getLogger().info("");
/*