From 65f04ba70e5e7851023bc98e67bc69b26a12f347 Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sun, 26 Jul 2020 13:39:33 -0400 Subject: [PATCH] updated cloud expansion list to be sorted via a configuration option of values [name, author, latest]. closes #363 --- .../cloud/CommandECloudExpansionList.java | 4 +-- .../configuration/ExpansionSort.java | 31 +++++++++++++++++++ .../configuration/PlaceholderAPIConfig.java | 18 +++++++++++ src/main/resources/config.yml | 1 + 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/main/java/me/clip/placeholderapi/configuration/ExpansionSort.java diff --git a/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloudExpansionList.java b/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloudExpansionList.java index 480102f..d4e4bec 100644 --- a/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloudExpansionList.java +++ b/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloudExpansionList.java @@ -7,6 +7,7 @@ import com.google.common.collect.Sets; import com.google.common.primitives.Ints; import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.commands.PlaceholderCommand; +import me.clip.placeholderapi.configuration.ExpansionSort; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.cloud.CloudExpansion; import me.clip.placeholderapi.util.Format; @@ -22,7 +23,6 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -85,7 +85,7 @@ public final class CommandECloudExpansionList extends PlaceholderCommand return; } - expansions.sort(Comparator.comparing(CloudExpansion::getLastUpdate).reversed()); + expansions.sort(plugin.getPlaceholderAPIConfig().getExpansionSort().orElse(ExpansionSort.LATEST)); if (!(sender instanceof Player) && params.size() < 2) { diff --git a/src/main/java/me/clip/placeholderapi/configuration/ExpansionSort.java b/src/main/java/me/clip/placeholderapi/configuration/ExpansionSort.java new file mode 100644 index 0000000..b8306d7 --- /dev/null +++ b/src/main/java/me/clip/placeholderapi/configuration/ExpansionSort.java @@ -0,0 +1,31 @@ +package me.clip.placeholderapi.configuration; + +import me.clip.placeholderapi.expansion.cloud.CloudExpansion; +import org.jetbrains.annotations.NotNull; + +import java.util.Comparator; + +public enum ExpansionSort implements Comparator +{ + + NAME(Comparator.comparing(CloudExpansion::getName)), + AUTHOR(Comparator.comparing(CloudExpansion::getAuthor)), + LATEST(Comparator.comparing(CloudExpansion::getLastUpdate).reversed()); + + + @NotNull + private final Comparator comparator; + + ExpansionSort(@NotNull final Comparator comparator) + { + this.comparator = comparator; + } + + + @Override + public final int compare(final CloudExpansion expansion1, final CloudExpansion expansion2) + { + return comparator.compare(expansion1, expansion2); + } + +} diff --git a/src/main/java/me/clip/placeholderapi/configuration/PlaceholderAPIConfig.java b/src/main/java/me/clip/placeholderapi/configuration/PlaceholderAPIConfig.java index 30635e2..5fd4062 100644 --- a/src/main/java/me/clip/placeholderapi/configuration/PlaceholderAPIConfig.java +++ b/src/main/java/me/clip/placeholderapi/configuration/PlaceholderAPIConfig.java @@ -23,6 +23,8 @@ package me.clip.placeholderapi.configuration; import me.clip.placeholderapi.PlaceholderAPIPlugin; import org.jetbrains.annotations.NotNull; +import java.util.Optional; + public final class PlaceholderAPIConfig { @@ -64,6 +66,22 @@ public final class PlaceholderAPIConfig } + public Optional getExpansionSort() + { + final String option = plugin.getConfig().getString("cloud_sorting", ExpansionSort.LATEST.name()); + + try + { + //noinspection ConstantConditions (bad spigot annotation) + return Optional.of(ExpansionSort.valueOf(option.toUpperCase())); + } + catch (final IllegalArgumentException ignored) + { + return Optional.empty(); + } + } + + @NotNull public String dateFormat() { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1202dba..6fa0861 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -10,6 +10,7 @@ # Download placeholders: /papi ecloud check_updates: true cloud_enabled: true +cloud_sorting: "name" cloud_allow_unverified_expansions: false boolean: 'true': 'yes'