Skip to content

Commit

Permalink
Fixed ignoring and un-ignoring islands not updating top islands menu
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Mar 18, 2022
1 parent 265661e commit 9b10574
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
Expand Up @@ -83,12 +83,23 @@ public interface IslandsContainer {

/**
* Sort islands for the top-islands.
* The islands will not get sorted if only one island exists.
*
* @param sortingType The type of sorting to use.
* @param onFinish Callback method
* @param onFinish Callback method
*/
void sortIslands(SortingType sortingType, @Nullable Runnable onFinish);

/**
* Sort islands for the top-islands.
*
* @param sortingType The type of sorting to use.
* @param forceSort Whether to force-sort the islands.
* When true, islands will get sorted even if only one island exists.
* @param onFinish Callback method
*/
void sortIslands(SortingType sortingType, boolean forceSort, @Nullable Runnable onFinish);

/**
* Get all islands sorted by a specific sorting-type.
*
Expand All @@ -105,7 +116,7 @@ public interface IslandsContainer {
* Add a new sorting-type.
*
* @param sortingType The sorting-type to add.
* @param sort Whether to sort the islands or not when the sorting-type is added.
* @param sort Whether to sort the islands or not when the sorting-type is added.
*/
void addSortingType(SortingType sortingType, boolean sort);

Expand Down
Expand Up @@ -1570,6 +1570,7 @@ public boolean isIgnored() {
public void setIgnored(boolean ignored) {
PluginDebugger.debug("Action: Set Ignored, Island: " + owner.getName() + ", Ignored: " + ignored);
this.isTopIslandsIgnored = ignored;
plugin.getGrid().setForceSort(true); // We want top islands to get sorted again even if only 1 island exists
IslandsDatabaseBridge.saveIgnoredStatus(this);
}

Expand Down Expand Up @@ -1661,7 +1662,7 @@ public void setBankLimit(BigDecimal bankLimit) {
this.bankLimit = new UpgradeValue<>(bankLimit, i -> i.compareTo(new BigDecimal(-1)) < 0);

// Trying to give interest again if the last one failed.
if(hasGiveInterestFailed())
if (hasGiveInterestFailed())
giveInterest(false);

IslandsDatabaseBridge.saveBankLimit(this);
Expand All @@ -1688,7 +1689,7 @@ public boolean giveInterest(boolean checkOnlineOwner) {
BigDecimal balanceToGive = balance.multiply(new BigDecimal(BuiltinModules.BANK.bankInterestPercentage / 100D));

// If the money that will be given exceeds limit, we want to give money later.
if(balanceToGive.add(balance).compareTo(getBankLimit()) > 0) {
if (balanceToGive.add(balance).compareTo(getBankLimit()) > 0) {
giveInterestFailed = true;
return false;
}
Expand Down
Expand Up @@ -23,7 +23,7 @@ public final class DefaultIslandsContainer implements IslandsContainer {

private static final Predicate<Island> ISLANDS_PREDICATE = island -> !island.isIgnored();

private final SortedRegistry<UUID, Island, SortingType> sortedIslands = new SortedRegistry<>();
private final SortedRegistry<UUID, Island, SortingType> sortedIslands = new SortedRegistry<>(ISLANDS_PREDICATE);
private final Map<IslandPosition, Island> islandsByPositions = new ConcurrentHashMap<>();
private final Map<UUID, Island> islandsByUUID = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -114,7 +114,12 @@ public void transferIsland(UUID oldOwner, UUID newOwner) {

@Override
public void sortIslands(SortingType sortingType, Runnable onFinish) {
this.sortedIslands.sort(sortingType, ISLANDS_PREDICATE, onFinish);
this.sortIslands(sortingType, false, onFinish);
}

@Override
public void sortIslands(SortingType sortingType, boolean forceSort, Runnable onFinish) {
this.sortedIslands.sort(sortingType, forceSort, onFinish);
}

@Override
Expand All @@ -129,7 +134,7 @@ public List<Island> getIslandsUnsorted() {

@Override
public void addSortingType(SortingType sortingType, boolean sort) {
this.sortedIslands.registerSortingType(sortingType, sort, ISLANDS_PREDICATE);
this.sortedIslands.registerSortingType(sortingType, sort);
}

private void runWithCustomWorld(Location islandLocation, Island island, World.Environment environment, Consumer<Location> onSuccess) {
Expand Down
Expand Up @@ -5,6 +5,7 @@
import com.google.common.collect.Iterables;
import org.bukkit.Bukkit;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -14,11 +15,18 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public final class SortedRegistry<K, V, Z extends Comparator<V>> {

private final Map<Z, Set<V>> sortedValues = new ConcurrentHashMap<>();
private final Map<K, V> innerMap = new ConcurrentHashMap<>();
@Nullable
private final Predicate<V> valuesPredicate;

public SortedRegistry(@Nullable Predicate<V> valuesPredicate) {
this.valuesPredicate = valuesPredicate;
}

public V get(K key) {
return innerMap.get(key);
Expand All @@ -35,8 +43,10 @@ public int indexOf(V value, Z sortingType) {
}

public V put(K key, V value) {
for (Set<V> sortedTree : sortedValues.values())
sortedTree.add(value);
if (canAddValue(value)) {
for (Set<V> sortedTree : sortedValues.values())
sortedTree.add(value);
}
return innerMap.put(key, value);
}

Expand All @@ -53,15 +63,15 @@ public List<V> getIslands(Z sortingType) {
return Collections.unmodifiableList(new ArrayList<>(this.sortedValues.get(sortingType)));
}

public void sort(Z sortingType, Predicate<V> predicate, Runnable onFinish) {
if (innerMap.size() <= 1) {
public void sort(Z sortingType, boolean forceSort, Runnable onFinish) {
if (!forceSort && innerMap.size() <= 1) {
if (onFinish != null)
onFinish.run();
return;
}

if (Bukkit.isPrimaryThread()) {
Executor.async(() -> sort(sortingType, predicate, onFinish));
Executor.async(() -> sort(sortingType, forceSort, onFinish));
return;
}

Expand All @@ -70,7 +80,7 @@ public void sort(Z sortingType, Predicate<V> predicate, Runnable onFinish) {
Set<V> newSortedTree = new ConcurrentSkipListSet<>(sortingType);

for (V element : innerMap.values()) {
if (predicate == null || predicate.test(element))
if (canAddValue(element))
newSortedTree.add(element);
}

Expand All @@ -80,19 +90,23 @@ public void sort(Z sortingType, Predicate<V> predicate, Runnable onFinish) {
onFinish.run();
}

public void registerSortingType(Z sortingType, boolean sort, Predicate<V> predicate) {
public void registerSortingType(Z sortingType, boolean sort) {
Preconditions.checkArgument(!sortedValues.containsKey(sortingType), "You cannot register an existing sorting type to the database.");

Set<V> sortedIslands = new ConcurrentSkipListSet<>(sortingType);
sortedIslands.addAll(innerMap.values());
sortedIslands.addAll(innerMap.values().stream().filter(this::canAddValue).collect(Collectors.toList()));
sortedValues.put(sortingType, sortedIslands);

if (sort)
sort(sortingType, predicate, null);
sort(sortingType, false, null);
}

private void ensureType(Z sortingType) {
Preconditions.checkState(sortedValues.containsKey(sortingType), "The sorting-type " + sortingType + " doesn't exist in the database. Please contact author!");
}

private boolean canAddValue(V value) {
return valuesPredicate == null || valuesPredicate.test(value);
}

}
Expand Up @@ -78,6 +78,8 @@ public final class GridHandler extends AbstractHandler implements GridManager {

private boolean pluginDisable = false;

private boolean forceSort = false;

public GridHandler(SuperiorSkyblockPlugin plugin, IslandsPurger islandsPurger, IslandPreviews islandPreviews,
IslandsContainer islandsContainer) {
super(plugin);
Expand Down Expand Up @@ -412,11 +414,17 @@ public void sortIslands(SortingType sortingType, Runnable onFinish) {

PluginDebugger.debug("Action: Sort Islands, Sorting Type: " + sortingType.getName());

this.islandsContainer.sortIslands(sortingType, () -> {
this.islandsContainer.sortIslands(sortingType, forceSort, () -> {
plugin.getMenus().refreshTopIslands(sortingType);
if (onFinish != null)
onFinish.run();
});

forceSort = false;
}

public void setForceSort(boolean forceSort) {
this.forceSort = forceSort;
}

@Override
Expand Down

0 comments on commit 9b10574

Please sign in to comment.