Fixed issue with quest list custom model data

This commit is contained in:
Jules 2023-10-01 16:17:51 +02:00
parent 5c185476dd
commit d6ec76573c

View File

@ -1,7 +1,9 @@
package net.Indyuce.mmocore.gui; package net.Indyuce.mmocore.gui;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.SoundEvent;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.quest.Quest;
import net.Indyuce.mmocore.api.util.math.format.DelayFormat; import net.Indyuce.mmocore.api.util.math.format.DelayFormat;
import net.Indyuce.mmocore.experience.Profession; import net.Indyuce.mmocore.experience.Profession;
import net.Indyuce.mmocore.gui.api.EditableInventory; import net.Indyuce.mmocore.gui.api.EditableInventory;
@ -10,14 +12,11 @@ import net.Indyuce.mmocore.gui.api.InventoryClickContext;
import net.Indyuce.mmocore.gui.api.item.InventoryItem; import net.Indyuce.mmocore.gui.api.item.InventoryItem;
import net.Indyuce.mmocore.gui.api.item.Placeholders; import net.Indyuce.mmocore.gui.api.item.Placeholders;
import net.Indyuce.mmocore.gui.api.item.SimplePlaceholderItem; import net.Indyuce.mmocore.gui.api.item.SimplePlaceholderItem;
import net.Indyuce.mmocore.api.quest.Quest;
import net.Indyuce.mmocore.api.SoundEvent;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
@ -108,9 +107,7 @@ public class QuestViewer extends EditableInventory {
List<String> lore = new ArrayList<>(getLore()); List<String> lore = new ArrayList<>(getLore());
/* // Replace quest lore
* replace quest lore
*/
index = lore.indexOf("{lore}"); index = lore.indexOf("{lore}");
if (index >= 0) { if (index >= 0) {
lore.remove(index); lore.remove(index);
@ -118,22 +115,20 @@ public class QuestViewer extends EditableInventory {
lore.add(index + j, quest.getLore().get(j)); lore.add(index + j, quest.getLore().get(j));
} }
/* // Calculate lore for later
* calculate quest info for later.
*/
int reqCount = quest.countLevelRestrictions(); int reqCount = quest.countLevelRestrictions();
boolean started = inv.getPlayerData().getQuestData().hasCurrent(quest), completed = inv.getPlayerData().getQuestData().hasFinished(quest), boolean started = inv.getPlayerData().getQuestData().hasCurrent(quest), completed = inv.getPlayerData().getQuestData().hasFinished(quest),
cooldown = completed && inv.getPlayerData().getQuestData().checkCooldownAvailability(quest); cooldown = completed && inv.getPlayerData().getQuestData().checkCooldownAvailability(quest);
lore.removeIf(next -> (next.startsWith("{level_req}") && reqCount < 1) || lore.removeIf(next -> (next.startsWith("{level_req}") && reqCount < 1)
(next.startsWith("{started}") && !started) || (next.startsWith("{!started}") && started) || (next.startsWith("{started}") && !started)
|| (next.startsWith("{completed}") && !completed) || (next.startsWith("{completed_cannot_redo}") && || (next.startsWith("{!started}") && started)
!(completed && !quest.isRedoable())) || (next.startsWith("{completed_can_redo}") && !(cooldown && quest.isRedoable())) || (next.startsWith("{completed}") && !completed)
|| (next.startsWith("{completed_cannot_redo}") && !(completed && !quest.isRedoable()))
|| (next.startsWith("{completed_can_redo}") && !(cooldown && quest.isRedoable()))
|| (next.startsWith("{completed_delay}") && !(completed && !cooldown))); || (next.startsWith("{completed_delay}") && !(completed && !cooldown)));
/* // Replace level requirements
* replace level requirements
*/
index = lore.indexOf("{level_req}{level_requirements}"); index = lore.indexOf("{level_req}{level_requirements}");
if (index >= 0) { if (index >= 0) {
lore.remove(index); lore.remove(index);
@ -149,28 +144,25 @@ public class QuestViewer extends EditableInventory {
} }
} }
Placeholders holders = getPlaceholders(inv.getPlayerData(), quest); Placeholders holders = getPlaceholders(inv, n);
for (int j = 0; j < lore.size(); j++) for (int j = 0; j < lore.size(); j++)
lore.set(j, ChatColor.GRAY + holders.apply(inv.getPlayer(), lore.get(j))); lore.set(j, ChatColor.GRAY + holders.apply(inv.getPlayer(), lore.get(j)));
/* // Generate item
* generate item ItemStack item = super.display(inv, n);
*/
ItemStack item = new ItemStack(getMaterial());
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
meta.setDisplayName(holders.apply(inv.getPlayer(), getName()));
meta.addItemFlags(ItemFlag.values());
meta.setLore(lore); meta.setLore(lore);
meta.getPersistentDataContainer().set(new NamespacedKey(MMOCore.plugin, "quest_id"), PersistentDataType.STRING,quest.getId()); meta.getPersistentDataContainer().set(new NamespacedKey(MMOCore.plugin, "quest_id"), PersistentDataType.STRING,quest.getId());
item.setItemMeta(meta); item.setItemMeta(meta);
return item; return item;
} }
private Placeholders getPlaceholders(PlayerData data, Quest quest) { @Override
public Placeholders getPlaceholders(GeneratedInventory inv, int n) {
Quest quest = ((QuestInventory) inv).quests.get(n);
PlayerData data = inv.getPlayerData();
Placeholders holders = new Placeholders(); Placeholders holders = new Placeholders();
holders.register("name", quest.getName()); holders.register("name", quest.getName());
holders.register("total_level_req", quest.getLevelRestrictions().size() + (quest.getLevelRestriction(null) > 0 ? 1 : 0)); holders.register("total_level_req", quest.getLevelRestrictions().size() + (quest.getLevelRestriction(null) > 0 ? 1 : 0));