Skip to content

Commit

Permalink
Added the ability to change the way members are sorted in top islands (
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Apr 30, 2022
1 parent ce02e14 commit 0a424be
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 1 deletion.
@@ -1,5 +1,6 @@
package com.bgsoftware.superiorskyblock.api.config;

import com.bgsoftware.superiorskyblock.api.enums.TopIslandMembersSorting;
import com.bgsoftware.superiorskyblock.api.handlers.BlockValuesManager;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
Expand Down Expand Up @@ -562,6 +563,12 @@ public interface SettingsManager {
*/
boolean isAutoUncoopWhenAlone();

/**
* Get the way to sort members in the top islands menu.
* Config-path: island-top-members-sorting
*/
TopIslandMembersSorting getTopIslandMembersSorting();

interface Database {

/**
Expand Down
@@ -0,0 +1,38 @@
package com.bgsoftware.superiorskyblock.api.enums;

import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;

import java.util.Comparator;

public enum TopIslandMembersSorting {

/**
* Sort members in the top-islands menu by their names.
*/
NAMES,

/**
* Sort members in the top-islands menu by their roles.
*/
ROLES;

private Comparator<SuperiorPlayer> comparator = null;

TopIslandMembersSorting() {
}

public void setComparator(Comparator<SuperiorPlayer> comparator) {
if (this.comparator != null)
throw new IllegalArgumentException("You cannot set a comparator after it was already been set.");

this.comparator = comparator;
}

public Comparator<SuperiorPlayer> getComparator() {
if (this.comparator == null)
throw new RuntimeException(this + " was not initialized.");

return comparator;
}

}
Expand Up @@ -68,6 +68,7 @@
import com.bgsoftware.superiorskyblock.utils.FileUtils;
import com.bgsoftware.superiorskyblock.utils.debug.PluginDebugger;
import com.bgsoftware.superiorskyblock.utils.events.EventsBus;
import com.bgsoftware.superiorskyblock.utils.islands.SortingComparators;
import com.bgsoftware.superiorskyblock.utils.islands.SortingTypes;
import com.bgsoftware.superiorskyblock.utils.items.EnchantsUtils;
import com.bgsoftware.superiorskyblock.utils.items.HeadUtils;
Expand Down Expand Up @@ -194,6 +195,14 @@ public void onLoad() {
SortingTypes.registerSortingTypes();
IslandFlags.registerFlags();

try {
SortingComparators.initializeTopIslandMembersSorting();
} catch (IllegalArgumentException error) {
shouldEnable = false;
log("&cThe TopIslandMembersSorting was already initialized. " +
"This can be caused by a reload or another plugin initializing it.");
}

this.servicesHandler.registerPlaceholdersService(new PlaceholdersServiceImpl());
this.servicesHandler.registerHologramsService(new HologramsServiceImpl(this));
this.servicesHandler.registerEnderDragonService(new DragonBattleServiceImpl(this));
Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.bgsoftware.common.config.CommentedConfiguration;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.enums.TopIslandMembersSorting;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.key.KeyMap;
import com.bgsoftware.superiorskyblock.api.key.KeySet;
Expand Down Expand Up @@ -199,6 +200,7 @@ public final class SettingsContainer {
public final long recalcTaskTimeout;
public final boolean autoLanguageDetection;
public final boolean autoUncoopWhenAlone;
public final TopIslandMembersSorting islandTopMembersSorting;

public SettingsContainer(SuperiorSkyblockPlugin plugin, YamlConfiguration config) throws HandlerLoadException {
databaseType = config.getString("database.type").toUpperCase(Locale.ENGLISH);
Expand Down Expand Up @@ -503,6 +505,13 @@ else if (sections.length == 3)
recalcTaskTimeout = config.getLong("recalc-task-timeout");
autoLanguageDetection = config.getBoolean("auto-language-detection", true);
autoUncoopWhenAlone = config.getBoolean("auto-uncoop-when-alone", false);
TopIslandMembersSorting islandTopMembersSorting;
try {
islandTopMembersSorting = TopIslandMembersSorting.valueOf(config.getString("island-top-members-sorting").toUpperCase());
} catch (IllegalArgumentException error) {
islandTopMembersSorting = TopIslandMembersSorting.NAMES;
}
this.islandTopMembersSorting = islandTopMembersSorting;
}

private List<String> loadInteractables(SuperiorSkyblockPlugin plugin) {
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.bgsoftware.common.config.CommentedConfiguration;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.config.SettingsManager;
import com.bgsoftware.superiorskyblock.api.enums.TopIslandMembersSorting;
import com.bgsoftware.superiorskyblock.api.handlers.BlockValuesManager;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
Expand Down Expand Up @@ -529,6 +530,11 @@ public boolean isAutoUncoopWhenAlone() {
return this.container.autoUncoopWhenAlone;
}

@Override
public TopIslandMembersSorting getTopIslandMembersSorting() {
return this.container.islandTopMembersSorting;
}

public void updateValue(String path, Object value) throws IOException {
SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();
File file = new File(plugin.getDataFolder(), "config.yml");
Expand Down
@@ -1,6 +1,7 @@
package com.bgsoftware.superiorskyblock.menu.button.impl.menu;

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.enums.TopIslandMembersSorting;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.service.placeholders.PlaceholdersService;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
Expand Down Expand Up @@ -100,6 +101,9 @@ public ItemStack modifyButtonItem(ItemStack buttonItem, MenuTopIslands superiorM
} else {
PlaceholdersService placeholdersService = plugin.getServices().getPlaceholdersService();

if (plugin.getSettings().getTopIslandMembersSorting() != TopIslandMembersSorting.NAMES)
members.sort(plugin.getSettings().getTopIslandMembersSorting().getComparator());

members.forEach(member -> {
String onlineMessage = member.isOnline() ?
Message.ISLAND_TOP_STATUS_ONLINE.getMessage(inventoryViewer.getUserLocale()) :
Expand Down
@@ -1,5 +1,6 @@
package com.bgsoftware.superiorskyblock.utils.islands;

import com.bgsoftware.superiorskyblock.api.enums.TopIslandMembersSorting;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.island.bank.BankTransaction;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
Expand Down Expand Up @@ -41,10 +42,20 @@ public final class SortingComparators {
int compare = Integer.compare(o2.getAllPlayersInside().size(), o1.getAllPlayersInside().size());
return compare == 0 ? ISLAND_NAMES_COMPARATOR.compare(o1, o2) : compare;
};
public final static Comparator<SuperiorPlayer> ISLAND_ROLES_COMPARATOR = (o1, o2) -> {
// Comparison is between o2 and o1 as the lower the weight is, the higher the player is.
int compare = Integer.compare(o2.getPlayerRole().getWeight(), o1.getPlayerRole().getWeight());
return compare == 0 ? PLAYER_NAMES_COMPARATOR.compare(o1, o2) : compare;
};

private SortingComparators() {

}

public static void initializeTopIslandMembersSorting() throws IllegalArgumentException {
TopIslandMembersSorting.NAMES.setComparator(PLAYER_NAMES_COMPARATOR);
TopIslandMembersSorting.ROLES.setComparator(ISLAND_ROLES_COMPARATOR);
}


}
8 changes: 7 additions & 1 deletion src/main/resources/config.yml
Expand Up @@ -751,4 +751,10 @@ recalc-task-timeout: 10
auto-language-detection: true

# Automatically uncoop players when there are no island members left online that can remove uncoop players.
auto-uncoop-when-alone: false
auto-uncoop-when-alone: false

# The way members of islands will be sorted in top islands when using the {4} placeholder.
# The followings are available:
# NAMES - Sort members by their names.
# ROLES - Sort members by their island roles.
island-top-members-sorting: NAMES

0 comments on commit 0a424be

Please sign in to comment.