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

View File

@ -1,59 +1,61 @@
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.List;
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 {
private final String id, lore;
private final String id, lore;
private final BarColor barColor;
private final List<Trigger> triggers = new ArrayList<>();
private final BarColor barColor;
private final List<Trigger> triggers = new ArrayList<>();
public Objective(ConfigurationSection config) {
this.id = config.getName();
this.lore = config.getString("lore");
public Objective(ConfigurationSection config) {
this.id = config.getName();
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");
barColor = BarColor.valueOf(format.toUpperCase().replace("-", "_").replace(" ", "_"));
String format = config.getString("bar-color", "PURPLE");
barColor = BarColor.valueOf(format.toUpperCase().replace("-", "_").replace(" ", "_"));
for (String key : config.getStringList("triggers"))
try {
triggers.add(MMOCore.plugin.loadManager.loadTrigger(new MMOLineConfig(key)));
} catch (IllegalArgumentException exception) {
MMOCore.plugin.getLogger().log(Level.WARNING,
"Could not load trigger '" + key + "' from objective '" + id + "': " + exception.getMessage());
}
}
for (String key : config.getStringList("triggers"))
try {
triggers.add(MMOCore.plugin.loadManager.loadTrigger(new MMOLineConfig(key)));
} catch (IllegalArgumentException exception) {
MMOCore.plugin.getLogger().log(Level.WARNING,
"Could not load trigger '" + key + "' from objective '" + id + "': " + exception.getMessage());
}
}
public String getId() {
return id;
}
public String getId() {
return id;
}
public BarColor getBarColor() {
return barColor;
}
public BarColor getBarColor() {
return barColor;
}
public String getDefaultLore() {
return lore;
}
public boolean hasLore() {
return lore != null && !lore.isEmpty();
}
public List<Trigger> getTriggers() {
return triggers;
}
public String getDefaultLore() {
return lore;
}
public abstract ObjectiveProgress newProgress(QuestProgress questProgress);
public List<Trigger> getTriggers() {
return triggers;
}
public abstract ObjectiveProgress newProgress(QuestProgress questProgress);
}