1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-30 21:07:48 +01:00

Reusing quest objects on plugin reload to avoid issues

This commit is contained in:
Zrips 2023-09-20 15:40:49 +03:00
parent 2bfd5bef5e
commit 80b8ac5684
6 changed files with 76 additions and 5 deletions

View File

@ -167,6 +167,8 @@ public final class Jobs extends JavaPlugin {
private static boolean hasLimitedItems = false;
public static boolean fullyLoaded = false;
private static final int MAX_ENTRIES = 20;
public static final LinkedHashMap<UUID, FastPayment> FASTPAYMENT = new LinkedHashMap<UUID, FastPayment>(MAX_ENTRIES + 1, .75F, false) {
protected boolean removeEldestEntry(Map.Entry<UUID, FastPayment> eldest) {
@ -779,7 +781,7 @@ public final class Jobs extends JavaPlugin {
System.out.println("There was some issues when starting plugin. Please contact dev about this. Plugin will be disabled.");
setEnabled(false);
}
fullyLoaded = true;
CMIMessages.consoleMessage(suffix);
}

View File

@ -84,6 +84,7 @@ public class quests implements Cmd {
List<QuestProgression> list = jPlayer.getQuestProgressions(jobProg.getJob());
for (QuestProgression q : list) {
int totalAmountNeeded = q.getTotalAmountNeeded();
int totalAmountDone = q.getTotalAmountDone();

View File

@ -1453,17 +1453,22 @@ public class ConfigManager {
Quest quest = new Quest(sqsection.getString("Name", one), job);
ActionType actionType = ActionType.getByName(sqsection.getString("Action"));
quest.setConfigName(one);
if (actionType != null) {
KeyValues kv = getKeyValue(sqsection.getString("Target").toUpperCase(), actionType, jobFullName);
if (kv != null) {
int amount = sqsection.getInt("Amount", 1);
quest.addObjective(new QuestObjective(actionType, kv.getId(), kv.getMeta(), (kv.getType() + kv.getSubType()).toUpperCase(), amount));
QuestObjective newObjective = new QuestObjective(actionType, kv.getId(), kv.getMeta(), (kv.getType() + kv.getSubType()).toUpperCase(), amount);
quest.addObjective(newObjective);
}
}
for (String oneObjective : sqsection.getStringList("Objectives")) {
List<QuestObjective> objectives = QuestObjective.get(oneObjective, jobFullName);
quest.addObjectives(objectives);
}
@ -1472,7 +1477,6 @@ public class ConfigManager {
if (sqsection.isInt("toLevel"))
quest.setMaxLvl(sqsection.getInt("toLevel"));
quest.setConfigName(one);
quest.setChance(sqsection.getInt("Chance", 100));
quest.setRewardCmds(sqsection.getStringList("RewardCommands"));
quest.setDescription(sqsection.getStringList("RewardDesc"));

View File

@ -10,12 +10,15 @@ import java.util.Set;
import com.gamingmesh.jobs.Jobs;
import net.Zrips.CMILib.Logs.CMIDebug;
public class Quest {
private String configName = "";
private String questName = "";
private Job job;
private long validUntil = 0L;
private int objectiveCount = 0;
private int chance = 100, minLvl = 0;
private Integer maxLvl;
@ -191,6 +194,7 @@ public class Quest {
return false;
}
@Deprecated
public void setObjectives(Map<ActionType, Map<String, QuestObjective>> objectives) {
if (objectives == null) {
return;
@ -206,7 +210,42 @@ public class Quest {
}
}
// Grabs existing job-quest-objective in case this is reload and we can reuse same objective object to avoid issue
private QuestObjective reuseOldObjectiveObject(QuestObjective objective) {
if (!Jobs.fullyLoaded)
return objective;
Job oldJob = Jobs.getJob(job.getName());
if (oldJob == null)
return objective;
Quest oldQuest = oldJob.getQuest(this.getConfigName());
if (oldQuest == null)
return objective;
Map<String, QuestObjective> oldObjectives = oldQuest.getObjectives().get(objective.getAction());
if (oldObjectives == null)
return objective;
QuestObjective oldObjective = oldObjectives.get(objective.getTargetName());
if (oldObjective == null)
return oldObjective;
if (oldObjective.getIdentifier().equals(objective.getIdentifier())) {
return oldObjective;
}
return objective;
}
public void addObjective(QuestObjective objective) {
objective = reuseOldObjectiveObject(objective);
Map<String, QuestObjective> old = objectives.get(objective.getAction());
if (old == null) {
old = new HashMap<>();
@ -227,4 +266,18 @@ public class Quest {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
private void count() {
for (ActionType one : actions) {
Map<String, QuestObjective> old = objectives.get(one);
if (old != null)
objectiveCount += old.size();
}
}
public int getObjectiveCount() {
if (objectiveCount < 1)
count();
return objectiveCount;
}
}

View File

@ -16,6 +16,8 @@ public class QuestObjective {
private int amount = Integer.MAX_VALUE;
private ActionType action = null;
private String serializedLine = "";
public static List<QuestObjective> get(String objective, String jobName) {
String[] split = objective.split(";", 3);
@ -115,4 +117,12 @@ public class QuestObjective {
public boolean same(QuestObjective obj) {
return obj.id == this.id && obj.meta.equals(this.meta) && obj.name.equals(this.name) && obj.amount == this.amount && obj.action == this.action;
}
public String getIdentifier() {
if (serializedLine.isEmpty()) {
serializedLine = getAction().toString() + ";" + getTargetName() + ";" + getAmount();
}
return serializedLine;
}
}

View File

@ -56,6 +56,7 @@ public class QuestProgression {
public int getTotalAmountDone() {
int amountDone = 0;
for (Integer one : done.values()) {
amountDone += one;
}