Added a GenerateLore event, which fires when lore is being generated for an item

This commit is contained in:
ASangarin 2022-06-13 22:48:21 +02:00
parent b077bdcd58
commit 2b0388adcf
3 changed files with 267 additions and 191 deletions

View File

@ -0,0 +1,72 @@
package net.Indyuce.mmoitems.api.event;
import net.Indyuce.mmoitems.api.item.build.LoreBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.util.List;
public class GenerateLoreEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final MMOItem item;
private final List<String> lore, parsed;
private final LoreBuilder builder;
public GenerateLoreEvent(MMOItem item, LoreBuilder builder, List<String> lore, List<String> parsed) {
this.item = item;
this.lore = lore;
this.parsed = parsed;
this.builder = builder;
}
/**
* @return The MMOItem the lore is being generated for
*/
public MMOItem getItem() {
return item;
}
/**
* @return The LoreBuilder used to build this lore.
*/
public LoreBuilder getBuilder() {
return builder;
}
/**
* @return The list of pre-placeholder lore (before any placeholders have been parsed)
*/
public List<String> getLore() {
return lore;
}
/**
* @return The list of parsed lore (after all placeholders have been parsed)
*/
public List<String> getParsedLore() {
return parsed;
}
/**
* Sets the final lore of the item.
* <p>
* This sets the parsed lore, so any future calls of
* getParsedLore() will return the new list.
*
* @param lore The new lore of the item
*/
public void setFinalLore(List<String> lore) {
this.parsed.clear();
this.parsed.addAll(lore);
}
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -4,15 +4,14 @@ import com.google.gson.JsonArray;
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.mythic.utils.adventure.text.Component;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.event.GenerateLoreEvent;
import net.Indyuce.mmoitems.api.event.ItemBuildEvent;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.api.util.message.FFPMMOItems;
import net.Indyuce.mmoitems.stat.DisplayName;
import net.Indyuce.mmoitems.stat.Enchants;
import net.Indyuce.mmoitems.stat.data.MaterialData;
@ -47,8 +46,8 @@ public class ItemStackBuilder {
private final LoreBuilder lore;
private final List<ItemTag> tags = new ArrayList<>();
private static final AttributeModifier fakeModifier = new AttributeModifier(
UUID.fromString("87851e28-af12-43f6-898e-c62bde6bd0ec"), "mmoitemsDecoy", 0, Operation.ADD_NUMBER);
private static final AttributeModifier fakeModifier = new AttributeModifier(UUID.fromString("87851e28-af12-43f6-898e-c62bde6bd0ec"),
"mmoitemsDecoy", 0, Operation.ADD_NUMBER);
/**
* Used to build an MMOItem into an ItemStack.
@ -61,17 +60,17 @@ public class ItemStackBuilder {
this.mmoitem = mmoitem;
// Generates a new ItemStack of the specified material (Specified in the Material stat, or a DIAMOND_SWORD if missing).
item = new ItemStack(mmoitem.hasData(ItemStats.MATERIAL) ?
((MaterialData) mmoitem.getData(ItemStats.MATERIAL)).getMaterial()
: Material.DIAMOND_SWORD);
item = new ItemStack(
mmoitem.hasData(ItemStats.MATERIAL) ? ((MaterialData) mmoitem.getData(ItemStats.MATERIAL)).getMaterial() : Material.DIAMOND_SWORD);
// Gets a lore builder, which will be used to apply the chosen lore format (Choose with the lore format stat, or the default one if unspecified)
lore = new LoreBuilder(mmoitem.hasData(ItemStats.LORE_FORMAT)
? MMOItems.plugin.getFormats().getFormat(mmoitem.getData(ItemStats.LORE_FORMAT).toString(), mmoitem.getType().getLoreFormat())
: MMOItems.plugin.getFormats().getFormat(mmoitem.getType().getLoreFormat()));
lore = new LoreBuilder(mmoitem.hasData(ItemStats.LORE_FORMAT) ? MMOItems.plugin.getFormats()
.getFormat(mmoitem.getData(ItemStats.LORE_FORMAT).toString(), mmoitem.getType().getLoreFormat()) : MMOItems.plugin.getFormats()
.getFormat(mmoitem.getType().getLoreFormat()));
// Gets the meta, and hides attributes
meta = item.getItemMeta();
//noinspection ConstantConditions
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
// Store the internal TYPE-ID Information (not stats, so it must be done manually here)
@ -79,8 +78,7 @@ public class ItemStackBuilder {
tags.add(new ItemTag("MMOITEMS_ITEM_ID", mmoitem.getId()));
// And a last technical tag for updating items
if (MMOItems.INTERNAL_REVISION_ID > 1)
tags.add(new ItemTag("MMOITEMS_INTERNAL_REVISION_ID", MMOItems.INTERNAL_REVISION_ID));
if (MMOItems.INTERNAL_REVISION_ID > 1) tags.add(new ItemTag("MMOITEMS_INTERNAL_REVISION_ID", MMOItems.INTERNAL_REVISION_ID));
}
public LoreBuilder getLore() {
@ -178,7 +176,10 @@ public class ItemStackBuilder {
// Get Template
MMOItemTemplate template = MMOItems.plugin.getTemplates().getTemplate(builtMMOItem.getType(), builtMMOItem.getId());
if (template == null) { throw new IllegalArgumentException("MMOItem $r" + builtMMOItem.getType().getId() + " " + builtMMOItem.getId() + "$b doesn't exist."); }
if (template == null) {
throw new IllegalArgumentException(
"MMOItem $r" + builtMMOItem.getType().getId() + " " + builtMMOItem.getId() + "$b doesn't exist.");
}
// Make necessary lore changes
((Previewable) stat).whenPreviewed(this, builtMMOItem.getData(stat), template.getBaseItemData().get(stat));
@ -198,36 +199,35 @@ public class ItemStackBuilder {
}
// Display gem stone lore hint thing
if (builtMMOItem.getType() == Type.GEM_STONE)
lore.insert("gem-stone-lore", ItemStat.translate("gem-stone-lore"));
if (builtMMOItem.getType() == Type.GEM_STONE) lore.insert("gem-stone-lore", ItemStat.translate("gem-stone-lore"));
// Display item type
lore.insert("item-type",
ItemStat.translate("item-type").replace("#",
builtMMOItem.getStats().contains(ItemStats.DISPLAYED_TYPE)
? builtMMOItem.getData(ItemStats.DISPLAYED_TYPE).toString()
: builtMMOItem.getType().getName()));
lore.insert("item-type", ItemStat.translate("item-type").replace("#",
builtMMOItem.getStats().contains(ItemStats.DISPLAYED_TYPE) ? builtMMOItem.getData(ItemStats.DISPLAYED_TYPE)
.toString() : builtMMOItem.getType().getName()));
// Calculate extra item lore with placeholders
if (builtMMOItem.hasData(ItemStats.LORE)) {
List<String> parsed = new ArrayList<>();
((StringListData) builtMMOItem.getData(ItemStats.LORE)).getList()
.forEach(str -> parsed.add(lore.applySpecialPlaceholders(str)));
((StringListData) builtMMOItem.getData(ItemStats.LORE)).getList().forEach(str -> parsed.add(lore.applySpecialPlaceholders(str)));
lore.insert("lore", parsed);
}
// Calculate and apply item lore
List<String> builtLore = lore.build();
meta.setLore(builtLore);
List<String> unparsedLore = lore.getLore();
List<String> parsedLore = lore.build();
GenerateLoreEvent event = new GenerateLoreEvent(builtMMOItem, lore, parsedLore, unparsedLore);
Bukkit.getPluginManager().callEvent(event);
meta.setLore(event.getParsedLore());
/*
* Save dynamic lore for later calculations. Not used anymore, but
* kept in case we need to roll back the lore update change.
*/
JsonArray array = new JsonArray();
builtLore.forEach(str -> array.add(str));
if (array.size() != 0)
tags.add(new ItemTag("MMOITEMS_DYNAMIC_LORE", array.toString()));
event.getParsedLore().forEach(array::add);
if (array.size() != 0) tags.add(new ItemTag("MMOITEMS_DYNAMIC_LORE", array.toString()));
/*
* This tag is added to entirely override default vanilla item attribute
@ -240,11 +240,11 @@ public class ItemStackBuilder {
NBTItem nbtItem = NBTItem.get(item);
// Apply item display name using Components for colors
if (mmoitem.hasData(ItemStats.NAME) && meta.hasDisplayName())
nbtItem.setDisplayNameComponent(LegacyComponent.parse(meta.getDisplayName()));
if (mmoitem.hasData(ItemStats.NAME) && meta.hasDisplayName()) nbtItem.setDisplayNameComponent(LegacyComponent.parse(meta.getDisplayName()));
if (meta.hasLore()) {
List<Component> componentLore = new LinkedList<>();
//noinspection ConstantConditions
meta.getLore().forEach(line -> componentLore.add(LegacyComponent.simpleParse(line)));
nbtItem.setLoreComponents(componentLore);
}

View File

@ -202,4 +202,8 @@ public class LoreBuilder {
private boolean isBar(String str) {
return str.startsWith("{bar}") || str.startsWith("{sbar}");
}
public List<String> getLore() {
return lore;
}
}