Skip to content

Commit

Permalink
fix: prevent unnecessary quest saves during plugin load
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioGr committed May 7, 2023
1 parent bc797c8 commit 2bd9a94
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public AdminEditCommands(final NotQuests main, PaperCommandManager<CommandSender
final Duration durationCooldown = context.get("duration");
final long cooldownInMinutes = durationCooldown.toMinutes();

quest.setAcceptCooldownComplete(cooldownInMinutes);
quest.setAcceptCooldownComplete(cooldownInMinutes, true);
context.getSender().sendMessage(main.parse(
"<success>Complete acceptCooldown for Quest <highlight>" + quest.getIdentifier() + "</highlight> has been set to <highlight2>"
+ durationCooldown.toDaysPart() + " days, " + durationCooldown.toHoursPart() + " hours, " + durationCooldown.toMinutesPart() + " minutes" + "</highlight2>!"
Expand All @@ -98,7 +98,7 @@ public AdminEditCommands(final NotQuests main, PaperCommandManager<CommandSender
.meta(CommandMeta.DESCRIPTION, "Disables the wait time for players between accepting quests.")
.handler((context) -> {
final Quest quest = context.get("quest");
quest.setAcceptCooldownComplete(-1);
quest.setAcceptCooldownComplete(-1, true);
context.getSender().sendMessage(main.parse(
"<success>Complete acceptCooldown for Quest <highlight>" + quest.getIdentifier() + "</highlight> has been set to <highlight2>disabled</highlight2>!"
));
Expand Down Expand Up @@ -227,13 +227,13 @@ public AdminEditCommands(final NotQuests main, PaperCommandManager<CommandSender
final Quest quest = context.get("quest");
int maxCompletions = context.get("max. completions");
if (maxCompletions > 0) {
quest.setMaxCompletions(maxCompletions);
quest.setMaxCompletions(maxCompletions, true);
context.getSender().sendMessage(main.parse(
"<success>Maximum amount of completions for Quest <highlight>" + quest.getIdentifier() + "</highlight> has been set to <highlight2>"
+ maxCompletions + "</highlight2>!"
));
} else {
quest.setMaxCompletions(-1);
quest.setMaxCompletions(-1, true);
context.getSender().sendMessage(main.parse(
"<success>Maximum amount of completions for Quest <highlight>" + quest.getIdentifier() + "</highlight> has been set to <highlight2>"
+ "unlimited (default)</highlight2>!"
Expand All @@ -252,13 +252,13 @@ public AdminEditCommands(final NotQuests main, PaperCommandManager<CommandSender
final Quest quest = context.get("quest");
int maxAccepts = context.get("max. accepts");
if (maxAccepts > 0) {
quest.setMaxAccepts(maxAccepts);
quest.setMaxAccepts(maxAccepts, true);
context.getSender().sendMessage(main.parse(
"<success>Maximum amount of accepts for Quest <highlight>" + quest.getIdentifier() + "</highlight> has been set to <highlight2>"
+ maxAccepts + "</highlight2>!"
));
} else {
quest.setMaxAccepts(-1);
quest.setMaxAccepts(-1, true);
context.getSender().sendMessage(main.parse(
"<success>Maximum amount of accepts for Quest <highlight>" + quest.getIdentifier() + "</highlight> has been set to <highlight2>"
+ "unlimited (default)</highlight2>!"
Expand All @@ -277,13 +277,13 @@ public AdminEditCommands(final NotQuests main, PaperCommandManager<CommandSender
final Quest quest = context.get("quest");
int maxFails = context.get("max. fails");
if (maxFails > 0) {
quest.setMaxFails(maxFails);
quest.setMaxFails(maxFails, true);
context.getSender().sendMessage(main.parse(
"<success>Maximum amount of fails for Quest <highlight>" + quest.getIdentifier() + "</highlight> has been set to <highlight2>"
+ maxFails + "</highlight2>!"
));
} else {
quest.setMaxFails(-1);
quest.setMaxFails(-1, true);
context.getSender().sendMessage(main.parse(
"<success>Maximum amount of fails for Quest <highlight>" + quest.getIdentifier() + "</highlight> has been set to <highlight2>"
+ "unlimited (default)</highlight2>!"
Expand All @@ -299,7 +299,7 @@ public AdminEditCommands(final NotQuests main, PaperCommandManager<CommandSender
.handler((context) -> {
final Quest quest = context.get("quest");
boolean takeEnabled = context.get("Take Enabled");
quest.setTakeEnabled(takeEnabled);
quest.setTakeEnabled(takeEnabled, true);
if (takeEnabled) {
context.getSender().sendMessage(main.parse(
"<success>Quest taking (/notquests take) for the Quest <highlight>"
Expand All @@ -320,7 +320,7 @@ public AdminEditCommands(final NotQuests main, PaperCommandManager<CommandSender
.handler((context) -> {
final Quest quest = context.get("quest");
boolean abortEnabled = context.get("Abort Enabled");
quest.setAbortEnabled(abortEnabled);
quest.setAbortEnabled(abortEnabled, true);
if (abortEnabled) {
context.getSender().sendMessage(main.parse(
"<success>Quest aborting (/notquests abort) for the Quest <highlight>"
Expand Down Expand Up @@ -366,7 +366,7 @@ public AdminEditCommands(final NotQuests main, PaperCommandManager<CommandSender
}


quest.setTakeItem(guiItem);
quest.setTakeItem(guiItem, true);
context.getSender().sendMessage(main.parse(
"<success>Take Item Material for Quest <highlight>" + quest.getIdentifier()
+ "</highlight> has been set to <highlight2>" + guiItem.getType().name() + "</highlight2>!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,18 +441,18 @@ public void loadQuestsFromConfig(final Category category) {
category.getQuestsConfig().set("quests." + questName + ".limits.completions", oldMaxAccepts);
category.saveQuestsConfig();
}
quest.setMaxCompletions(category.getQuestsConfig().getInt("quests." + questName + ".limits.completions", -1));
quest.setMaxAccepts(category.getQuestsConfig().getInt("quests." + questName + ".limits.accepts", -1));
quest.setMaxFails(category.getQuestsConfig().getInt("quests." + questName + ".limits.fails", -1));
quest.setTakeEnabled(category.getQuestsConfig().getBoolean("quests." + questName + ".takeEnabled", true));
quest.setAbortEnabled(category.getQuestsConfig().getBoolean("quests." + questName + ".abortEnabled", true));
quest.setMaxCompletions(category.getQuestsConfig().getInt("quests." + questName + ".limits.completions", -1), false);
quest.setMaxAccepts(category.getQuestsConfig().getInt("quests." + questName + ".limits.accepts", -1), false);
quest.setMaxFails(category.getQuestsConfig().getInt("quests." + questName + ".limits.fails", -1), false);
quest.setTakeEnabled(category.getQuestsConfig().getBoolean("quests." + questName + ".takeEnabled", true), false);
quest.setAbortEnabled(category.getQuestsConfig().getBoolean("quests." + questName + ".abortEnabled", true), false);
if(category.getQuestsConfig().isInt("quests." + questName + ".acceptCooldown")){ // Convert
final int oldCooldown = category.getQuestsConfig().getInt("quests." + questName + ".acceptCooldown", -1);
category.getQuestsConfig().set("quests." + questName + ".acceptCooldown", null);
category.getQuestsConfig().set("quests." + questName + ".acceptCooldown.complete", oldCooldown);
category.saveQuestsConfig();
}
quest.setAcceptCooldownComplete(category.getQuestsConfig().getLong("quests." + questName + ".acceptCooldown.complete", -1));
quest.setAcceptCooldownComplete(category.getQuestsConfig().getLong("quests." + questName + ".acceptCooldown.complete", -1), false);

quest.setPredefinedProgressOrder(PredefinedProgressOrder.fromConfiguration(category.getQuestsConfig(), "quests." + questName + ".predefinedProgressOrder"), false);

Expand Down Expand Up @@ -632,7 +632,7 @@ public void loadQuestsFromConfig(final Category category) {


//TakeItem:
quest.setTakeItem(category.getQuestsConfig().getItemStack("quests." + questName + ".takeItem"));
quest.setTakeItem(category.getQuestsConfig().getItemStack("quests." + questName + ".takeItem"), false);

quests.add(quest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,59 +319,71 @@ public final int getMaxCompletions() {
return maxCompletions;
}

public void setMaxCompletions(int maxCompletions) {
public void setMaxCompletions(final int maxCompletions, final boolean save) {
this.maxCompletions = maxCompletions;
category.getQuestsConfig().set("quests." + questName + ".limits.completions", maxCompletions);
category.saveQuestsConfig();
if(save){
category.getQuestsConfig().set("quests." + questName + ".limits.completions", maxCompletions);
category.saveQuestsConfig();
}
}

public final int getMaxAccepts() {
return maxAccepts;
}

public void setMaxAccepts(int maxAccepts) {
public void setMaxAccepts(int maxAccepts, final boolean save) {
this.maxAccepts = maxAccepts;
category.getQuestsConfig().set("quests." + questName + ".limits.accepts", maxAccepts);
category.saveQuestsConfig();
if(save){
category.getQuestsConfig().set("quests." + questName + ".limits.accepts", maxAccepts);
category.saveQuestsConfig();
}
}

public final int getMaxFails() {
return maxFails;
}

public void setMaxFails(int maxFails) {
public void setMaxFails(int maxFails, final boolean save) {
this.maxFails = maxFails;
category.getQuestsConfig().set("quests." + questName + ".limits.fails", maxFails);
category.saveQuestsConfig();
if(save){
category.getQuestsConfig().set("quests." + questName + ".limits.fails", maxFails);
category.saveQuestsConfig();
}
}

public final boolean isAbortEnabled() {
return abortEnabled;
}

public void setAbortEnabled(final boolean abortEnabled) {
public void setAbortEnabled(final boolean abortEnabled, final boolean save) {
this.abortEnabled = abortEnabled;
category.getQuestsConfig().set("quests." + questName + ".abortEnabled", abortEnabled);
category.saveQuestsConfig();
if(save){
category.getQuestsConfig().set("quests." + questName + ".abortEnabled", abortEnabled);
category.saveQuestsConfig();
}
}
public final boolean isTakeEnabled() {
return takeEnabled;
}

public void setTakeEnabled(final boolean takeEnabled) {
public void setTakeEnabled(final boolean takeEnabled, final boolean save) {
this.takeEnabled = takeEnabled;
category.getQuestsConfig().set("quests." + questName + ".takeEnabled", takeEnabled);
category.saveQuestsConfig();
if(save){
category.getQuestsConfig().set("quests." + questName + ".takeEnabled", takeEnabled);
category.saveQuestsConfig();
}
}

public final long getAcceptCooldownComplete() {
return acceptCooldownComplete;
}

public void setAcceptCooldownComplete(long cooldownInMinutes) {
public void setAcceptCooldownComplete(long cooldownInMinutes, final boolean save) {
this.acceptCooldownComplete = cooldownInMinutes;
category.getQuestsConfig().set("quests." + questName + ".acceptCooldown.complete", cooldownInMinutes);
category.saveQuestsConfig();
if(save) {
category.getQuestsConfig().set("quests." + questName + ".acceptCooldown.complete", cooldownInMinutes);
category.saveQuestsConfig();
}
}


Expand Down Expand Up @@ -565,11 +577,13 @@ public final ItemStack getTakeItem() {
return takeItem;
}

public void setTakeItem(final ItemStack takeItem) {
public void setTakeItem(final ItemStack takeItem, final boolean save) {
if (takeItem != null) {
this.takeItem = takeItem;
category.getQuestsConfig().set("quests." + questName + ".takeItem", takeItem);
category.saveQuestsConfig();
if(save) {
category.getQuestsConfig().set("quests." + questName + ".takeItem", takeItem);
category.saveQuestsConfig();
}
}
}

Expand Down

0 comments on commit 2bd9a94

Please sign in to comment.