Allow for empty objective lores

This commit is contained in:
Indyuce 2021-08-25 21:13:17 +02:00
parent ebf0d40fbf
commit 63c837be2c
2 changed files with 184 additions and 182 deletions

View File

@ -3,9 +3,9 @@ package net.Indyuce.mmocore.api.quest;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import io.lumine.mythic.lib.MythicLib;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import io.lumine.mythic.lib.MythicLib;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.boss.BarColor; import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle; import org.bukkit.boss.BarStyle;
@ -20,171 +20,171 @@ import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
public class PlayerQuests { public class PlayerQuests {
private final PlayerData playerData; private final PlayerData playerData;
private final BossBar bossbar; private final BossBar bossbar;
private final Map<String, Long> finished = new HashMap<>(); private final Map<String, Long> finished = new HashMap<>();
private QuestProgress current; private QuestProgress current;
public PlayerQuests(PlayerData playerData) { public PlayerQuests(PlayerData playerData) {
this.playerData = playerData; this.playerData = playerData;
bossbar = MythicLib.plugin.getVersion().getWrapper().createBossBar( bossbar = MythicLib.plugin.getVersion().getWrapper().createBossBar(
new NamespacedKey(MMOCore.plugin, "quest_bar_" + playerData.getUniqueId().toString()), new NamespacedKey(MMOCore.plugin, "quest_bar_" + playerData.getUniqueId().toString()),
"", BarColor.PURPLE, BarStyle.SEGMENTED_20); "", BarColor.PURPLE, BarStyle.SEGMENTED_20);
if(playerData.isOnline()) if (playerData.isOnline())
bossbar.addPlayer(playerData.getPlayer()); bossbar.addPlayer(playerData.getPlayer());
} }
@Deprecated @Deprecated
public PlayerQuests(PlayerData playerData, BossBar bar) { public PlayerQuests(PlayerData playerData, BossBar bar) {
this.playerData = playerData; this.playerData = playerData;
this.bossbar = bar; this.bossbar = bar;
} }
public PlayerQuests load(ConfigurationSection config) { public PlayerQuests load(ConfigurationSection config) {
if (config.contains("current")) if (config.contains("current"))
try { try {
current = MMOCore.plugin.questManager.get(config.getString("current.id")).generateNewProgress(playerData, config.getInt("current.objective")); current = MMOCore.plugin.questManager.get(config.getString("current.id")).generateNewProgress(playerData, config.getInt("current.objective"));
} catch (Exception e) { } catch (Exception e) {
playerData.log(Level.WARNING, "Couldn't load current quest progress (ID '" + config.getString("current.id") + "')"); playerData.log(Level.WARNING, "Couldn't load current quest progress (ID '" + config.getString("current.id") + "')");
} }
if (config.contains("finished")) if (config.contains("finished"))
for (String key : config.getConfigurationSection("finished").getKeys(false)) for (String key : config.getConfigurationSection("finished").getKeys(false))
finished.put(key, config.getLong("finished." + key)); finished.put(key, config.getLong("finished." + key));
/* /*
* must update the boss bar once the instance is loaded, otherwise it * must update the boss bar once the instance is loaded, otherwise it
* won't detect the current quest. THE BOSS BAR UPDATE is in the player * won't detect the current quest. THE BOSS BAR UPDATE is in the player
* data class, this way it is still set invisible even if the player has * data class, this way it is still set invisible even if the player has
* no quest data * no quest data
*/ */
return this; return this;
} }
public void save(ConfigurationSection config) { public void save(ConfigurationSection config) {
if (current != null) { if (current != null) {
config.set("current.id", current.getQuest().getId()); config.set("current.id", current.getQuest().getId());
config.set("current.objective", current.getObjectiveNumber()); config.set("current.objective", current.getObjectiveNumber());
} else } else
config.set("current", null); config.set("current", null);
for (String key : finished.keySet()) for (String key : finished.keySet())
config.set("finished." + key, finished.get(key)); config.set("finished." + key, finished.get(key));
} }
public String toJsonString() { public String toJsonString() {
JsonObject json = new JsonObject(); JsonObject json = new JsonObject();
if(current != null) { if (current != null) {
JsonObject cur = new JsonObject(); JsonObject cur = new JsonObject();
cur.addProperty("id", current.getQuest().getId()); cur.addProperty("id", current.getQuest().getId());
cur.addProperty("objective", current.getObjectiveNumber()); cur.addProperty("objective", current.getObjectiveNumber());
json.add("current", cur); json.add("current", cur);
} }
JsonObject fin = new JsonObject(); JsonObject fin = new JsonObject();
for (String key : finished.keySet()) for (String key : finished.keySet())
fin.addProperty(key, finished.get(key)); fin.addProperty(key, finished.get(key));
if(finished.size() != 0) if (finished.size() != 0)
json.add("finished", fin); json.add("finished", fin);
return json.toString(); return json.toString();
} }
public void load(String json) { public void load(String json) {
Gson parser = new Gson(); Gson parser = new Gson();
JsonObject jo = parser.fromJson(json, JsonObject.class); JsonObject jo = parser.fromJson(json, JsonObject.class);
if(jo.has("current")) { if (jo.has("current")) {
JsonObject cur = jo.getAsJsonObject("current"); JsonObject cur = jo.getAsJsonObject("current");
try { try {
current = MMOCore.plugin.questManager.get(cur.get("id").getAsString()).generateNewProgress(playerData, cur.get("objective").getAsInt()); current = MMOCore.plugin.questManager.get(cur.get("id").getAsString()).generateNewProgress(playerData, cur.get("objective").getAsInt());
} catch (Exception e) { } catch (Exception e) {
playerData.log(Level.WARNING, "Couldn't load current quest progress (ID '" + cur.get("id").getAsString() + "')"); playerData.log(Level.WARNING, "Couldn't load current quest progress (ID '" + cur.get("id").getAsString() + "')");
} }
} }
if(jo.has("finished")) { if (jo.has("finished")) {
for (Entry<String, JsonElement> entry : jo.getAsJsonObject("finished").entrySet()) for (Entry<String, JsonElement> entry : jo.getAsJsonObject("finished").entrySet())
finished.put(entry.getKey(), entry.getValue().getAsLong()); finished.put(entry.getKey(), entry.getValue().getAsLong());
} }
for(Entry<String, Long> entry : finished.entrySet()) for (Entry<String, Long> entry : finished.entrySet())
MMOCore.log("Finished: (" + entry.getKey() + ") - at: " + entry.getValue()); MMOCore.log("Finished: (" + entry.getKey() + ") - at: " + entry.getValue());
} }
public QuestProgress getCurrent() { public QuestProgress getCurrent() {
return current; return current;
} }
public boolean hasCurrent() { public boolean hasCurrent() {
return current != null; return current != null;
} }
public Set<String> getFinishedQuests() { public Set<String> getFinishedQuests() {
return finished.keySet(); return finished.keySet();
} }
public boolean hasCurrent(Quest quest) { public boolean hasCurrent(Quest quest) {
return hasCurrent() && current.getQuest().equals(quest); return hasCurrent() && current.getQuest().equals(quest);
} }
public boolean hasFinished(Quest quest) { public boolean hasFinished(Quest quest) {
return finished.containsKey(quest.getId()); return finished.containsKey(quest.getId());
} }
public void finishCurrent() { public void finishCurrent() {
finished.put(current.getQuest().getId(), System.currentTimeMillis()); finished.put(current.getQuest().getId(), System.currentTimeMillis());
start(null); start(null);
} }
public void resetFinishedQuests() { public void resetFinishedQuests() {
finished.clear(); finished.clear();
} }
public Date getFinishDate(Quest quest) { public Date getFinishDate(Quest quest) {
return new Date(finished.get(quest.getId())); return new Date(finished.get(quest.getId()));
} }
public void start(Quest quest) { public void start(Quest quest) {
// close current objective progress if quest is active // close current objective progress if quest is active
if (hasCurrent()) if (hasCurrent())
current.getProgress().close(); current.getProgress().close();
// apply newer quest // apply newer quest
current = quest == null ? null : quest.generateNewProgress(playerData); current = quest == null ? null : quest.generateNewProgress(playerData);
updateBossBar(); updateBossBar();
} }
public boolean checkCooldownAvailability(Quest quest) { public boolean checkCooldownAvailability(Quest quest) {
return (finished.get(quest.getId()) + quest.getDelayMillis()) < System.currentTimeMillis(); return (finished.get(quest.getId()) + quest.getDelayMillis()) < System.currentTimeMillis();
} }
public long getDelayFeft(Quest quest) { public long getDelayFeft(Quest quest) {
return Math.max(finished.get(quest.getId()) + quest.getDelayMillis() - System.currentTimeMillis(), 0); return Math.max(finished.get(quest.getId()) + quest.getDelayMillis() - System.currentTimeMillis(), 0);
} }
public boolean checkParentAvailability(Quest quest) { public boolean checkParentAvailability(Quest quest) {
for (Quest parent : quest.getParents()) for (Quest parent : quest.getParents())
if (!hasFinished(parent)) if (!hasFinished(parent))
return false; return false;
return true; return true;
} }
public void updateBossBar() { public void updateBossBar() {
if (!hasCurrent()) { if (!hasCurrent() || !current.getProgress().getObjective().hasLore()) {
bossbar.setVisible(false); bossbar.setVisible(false);
return; return;
} }
bossbar.setVisible(true); bossbar.setVisible(true);
bossbar.setColor(current.getProgress().getObjective().getBarColor()); bossbar.setColor(current.getProgress().getObjective().getBarColor());
bossbar.setTitle(current.getFormattedLore()); bossbar.setTitle(current.getFormattedLore());
bossbar.setProgress((double) current.getObjectiveNumber() / current.getQuest().getObjectives().size()); bossbar.setProgress((double) current.getObjectiveNumber() / current.getQuest().getObjectives().size());
} }
public void resetBossBar() { public void resetBossBar() {
bossbar.removeAll(); bossbar.removeAll();
} }
} }

View File

@ -1,59 +1,61 @@
package net.Indyuce.mmocore.api.quest.objective; package net.Indyuce.mmocore.api.quest.objective;
import io.lumine.mythic.lib.api.MMOLineConfig;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.quest.ObjectiveProgress;
import net.Indyuce.mmocore.api.quest.QuestProgress;
import net.Indyuce.mmocore.api.quest.trigger.Trigger;
import org.apache.commons.lang.Validate;
import org.bukkit.boss.BarColor;
import org.bukkit.configuration.ConfigurationSection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.boss.BarColor;
import org.bukkit.configuration.ConfigurationSection;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.quest.ObjectiveProgress;
import net.Indyuce.mmocore.api.quest.QuestProgress;
import net.Indyuce.mmocore.api.quest.trigger.Trigger;
import io.lumine.mythic.lib.api.MMOLineConfig;
public abstract class Objective { public abstract class Objective {
private final String id, lore; private final String id, lore;
private final BarColor barColor; private final BarColor barColor;
private final List<Trigger> triggers = new ArrayList<>(); private final List<Trigger> triggers = new ArrayList<>();
public Objective(ConfigurationSection config) { public Objective(ConfigurationSection config) {
this.id = config.getName(); this.id = config.getName();
this.lore = config.getString("lore"); this.lore = config.getString("lore");
Validate.notNull(lore, "Could not find objective lore"); Validate.notNull(config.getStringList("triggers"), "Could not load trigger list");
Validate.notNull(config.getStringList("triggers"), "Could not load trigger list");
String format = config.getString("bar-color", "PURPLE"); String format = config.getString("bar-color", "PURPLE");
barColor = BarColor.valueOf(format.toUpperCase().replace("-", "_").replace(" ", "_")); barColor = BarColor.valueOf(format.toUpperCase().replace("-", "_").replace(" ", "_"));
for (String key : config.getStringList("triggers")) for (String key : config.getStringList("triggers"))
try { try {
triggers.add(MMOCore.plugin.loadManager.loadTrigger(new MMOLineConfig(key))); triggers.add(MMOCore.plugin.loadManager.loadTrigger(new MMOLineConfig(key)));
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
MMOCore.plugin.getLogger().log(Level.WARNING, MMOCore.plugin.getLogger().log(Level.WARNING,
"Could not load trigger '" + key + "' from objective '" + id + "': " + exception.getMessage()); "Could not load trigger '" + key + "' from objective '" + id + "': " + exception.getMessage());
} }
} }
public String getId() { public String getId() {
return id; return id;
} }
public BarColor getBarColor() { public BarColor getBarColor() {
return barColor; return barColor;
} }
public String getDefaultLore() { public boolean hasLore() {
return lore; return lore != null && !lore.isEmpty();
} }
public List<Trigger> getTriggers() { public String getDefaultLore() {
return triggers; return lore;
} }
public abstract ObjectiveProgress newProgress(QuestProgress questProgress); public List<Trigger> getTriggers() {
return triggers;
}
public abstract ObjectiveProgress newProgress(QuestProgress questProgress);
} }