Skip to content

Commit

Permalink
⚡ Cached formatted quest description
Browse files Browse the repository at this point in the history
  • Loading branch information
SkytAsul committed Apr 4, 2023
1 parent 27e865f commit 667109b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import fr.skytasul.quests.gui.quests.PlayerListGUI;
import fr.skytasul.quests.gui.quests.PlayerListGUI.Category;
import fr.skytasul.quests.players.PlayerAccount;
Expand Down Expand Up @@ -70,4 +71,20 @@ public List<String> formatDescription() {
return list;
}

@Override
public int hashCode() {
return Objects.hash(descriptionOptions, quest, acc, category, source);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof QuestDescriptionContext))
return false;

QuestDescriptionContext context = (QuestDescriptionContext) obj;

return descriptionOptions.equals(context.descriptionOptions) && quest.equals(context.quest)
&& acc.equals(context.acc) && category == context.category && source == context.source;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.bukkit.entity.Player;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import fr.skytasul.quests.api.options.QuestOptionString;
import fr.skytasul.quests.api.options.description.QuestDescriptionContext;
import fr.skytasul.quests.api.options.description.QuestDescriptionProvider;
import fr.skytasul.quests.utils.Lang;
import fr.skytasul.quests.utils.Utils;
import fr.skytasul.quests.utils.XMaterial;

public class OptionDescription extends QuestOptionString implements QuestDescriptionProvider {

private List<String> cachedDescription;
private Cache<QuestDescriptionContext, List<String>> cachedDescription =
CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES).build();

@Override
public void setValue(String value) {
Expand Down Expand Up @@ -46,8 +51,12 @@ public boolean isMultiline() {

@Override
public List<String> provideDescription(QuestDescriptionContext context) {
if (cachedDescription == null) cachedDescription = Arrays.asList("§7" + getValue());
return cachedDescription;
List<String> description = cachedDescription.getIfPresent(context);
if (description == null) {
description = Arrays.asList("§7" + Utils.finalFormat(context.getPlayerAccount().getPlayer(), getValue(), true));
cachedDescription.put(context, description);
}
return description;
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/fr/skytasul/quests/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ public static void sendMessageWP(CommandSender sender, String msg, Object... rep
}

public static String finalFormat(CommandSender sender, String text, boolean playerName, Object... replace) {
if (DependenciesManager.papi.isEnabled() && sender instanceof Player) text = QuestsPlaceholders.setPlaceholders((Player) sender, text);
if (playerName) text = text.replace("{PLAYER}", sender.getName()).replace("{PREFIX}", QuestsConfiguration.getPrefix());
if (DependenciesManager.papi.isEnabled() && sender instanceof Player)
text = QuestsPlaceholders.setPlaceholders((Player) sender, text);
if (playerName && sender != null)
text = text.replace("{PLAYER}", sender.getName()).replace("{PREFIX}", QuestsConfiguration.getPrefix());
text = ChatColor.translateAlternateColorCodes('&', text);
return format(text, replace);
}
Expand Down

0 comments on commit 667109b

Please sign in to comment.