Skip to content

Commit

Permalink
Fixed fetching prices before they are loaded by ShopGUIPlus (#1560)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Jan 20, 2023
1 parent ef2b147 commit 479cc27
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

public interface PricesProvider {

Expand All @@ -25,4 +26,11 @@ public interface PricesProvider {
@Nullable
Key getBlockKey(Key blockKey);

/**
* Get a CompletableFuture that is completed when all prices and data of this provider are ready.
*/
default CompletableFuture<Void> getWhenPricesAreReady() {
return CompletableFuture.completedFuture(null);
}

}
Expand Up @@ -6,21 +6,33 @@
import com.bgsoftware.superiorskyblock.api.key.KeyMap;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import net.brcdev.shopgui.ShopGuiPlugin;
import net.brcdev.shopgui.event.ShopGUIPlusPostEnableEvent;
import net.brcdev.shopgui.shop.Shop;
import net.brcdev.shopgui.shop.item.ShopItem;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

public class PricesProvider_ShopGUIPlus78 implements PricesProvider {

private final ShopGuiPlugin shopPlugin = ShopGuiPlugin.getInstance();
private final KeyMap<Double> cachedPrices = KeyMap.createConcurrentKeyMap();
private final CompletableFuture<Void> readyFuture = new CompletableFuture<>();

private final SuperiorSkyblockPlugin plugin;

public PricesProvider_ShopGUIPlus78(SuperiorSkyblockPlugin plugin) {
this.plugin = plugin;
Log.info("Using ShopGUIPlus as a prices provider.");
Bukkit.getPluginManager().registerEvents(new Listener() {
@EventHandler
public void onShopsLoaded(ShopGUIPlusPostEnableEvent event) {
readyFuture.complete(null);
}
}, plugin);
}

@Override
Expand Down Expand Up @@ -62,4 +74,9 @@ public Key getBlockKey(Key blockKey) {
return cachedPrices.getKey(blockKey, null);
}

@Override
public CompletableFuture<Void> getWhenPricesAreReady() {
return this.readyFuture;
}

}
Expand Up @@ -63,6 +63,7 @@ public class ProvidersManagerImpl extends Manager implements ProvidersManager {
private static final BigDecimal MAX_DOUBLE = BigDecimal.valueOf(Double.MAX_VALUE);

private final List<AFKProvider> AFKProvidersList = new LinkedList<>();
private List<Runnable> pricesLoadCallbacks = new LinkedList<>();
private SpawnersProvider spawnersProvider = new SpawnersProvider_Default();
private StackedBlocksProvider stackedBlocksProvider = new StackedBlocksProvider_Default();
private EconomyProvider economyProvider = new EconomyProvider_Default();
Expand Down Expand Up @@ -209,6 +210,10 @@ public PricesProvider getPricesProvider() {
@Override
public void setPricesProvider(PricesProvider pricesProvider) {
this.pricesProvider = pricesProvider;
this.pricesProvider.getWhenPricesAreReady().whenComplete((result, error) -> {
this.pricesLoadCallbacks.forEach(Runnable::run);
this.pricesLoadCallbacks = null;
});
}

@Override
Expand Down Expand Up @@ -246,6 +251,14 @@ public void unregisterStackedBlocksListener(IStackedBlocksListener stackedBlocks
this.stackedBlocksListeners.remove(stackedBlocksListener);
}

public void addPricesLoadCallback(Runnable callback) {
if (this.pricesLoadCallbacks == null) {
callback.run();
} else {
this.pricesLoadCallbacks.add(callback);
}
}

public void notifyStackedBlocksListeners(OfflinePlayer offlinePlayer, Block block,
IStackedBlocksListener.Action action) {
this.stackedBlocksListeners.forEach(stackedBlocksListener ->
Expand Down
Expand Up @@ -296,9 +296,8 @@ public SIsland(IslandBuilderImpl builder) {
}
});
if (!builder.blockCounts.isEmpty()) {
BukkitExecutor.sync(() -> builder.blockCounts.forEach((block, count) ->
handleBlockPlace(block, count, false, false)
), 20L);
plugin.getProviders().addPricesLoadCallback(() -> builder.blockCounts.forEach((block, count) ->
handleBlockPlace(block, count, false, false)));
}

builder.warpCategories.forEach(warpCategoryRecord -> {
Expand Down

0 comments on commit 479cc27

Please sign in to comment.