Skip to content

Commit

Permalink
Potential fix for prices from ShopGUIPlus never load (#1594, #1599)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Feb 19, 2023
1 parent 529cdcd commit 0e0a7fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,33 @@ public BigDecimal getPrice(Key key) {
double price = cachedPrices.getOrDefault(key, 0D);

if (price == 0) {
for (Shop shop : shopPlugin.getShopManager().getShops()) {
for (ShopItem shopItem : shop.getShopItems()) {
if (Key.of(shopItem.getItem()).equals(key)) {
double shopPrice;
try {
for (Shop shop : shopPlugin.getShopManager().getShops()) {
for (ShopItem shopItem : shop.getShopItems()) {
if (Key.of(shopItem.getItem()).equals(key)) {
double shopPrice;

switch (plugin.getSettings().getSyncWorth()) {
case BUY:
shopPrice = shopItem.getBuyPriceForAmount(1);
break;
case SELL:
shopPrice = shopItem.getSellPriceForAmount(1);
break;
default:
shopPrice = 0;
break;
}
switch (plugin.getSettings().getSyncWorth()) {
case BUY:
shopPrice = shopItem.getBuyPriceForAmount(1);
break;
case SELL:
shopPrice = shopItem.getSellPriceForAmount(1);
break;
default:
shopPrice = 0;
break;
}

if (shopPrice > price) {
price = shopPrice;
cachedPrices.put(key, price);
if (shopPrice > price) {
price = shopPrice;
cachedPrices.put(key, price);
}
}
}
}
} catch (Throwable error) {
Log.warn(error, "Failed to load prices for item " + key);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,14 @@ public PricesProvider getPricesProvider() {
@Override
public void setPricesProvider(PricesProvider pricesProvider) {
this.pricesProvider = pricesProvider;
this.pricesProvider.getWhenPricesAreReady().whenComplete((result, error) -> {
this.pricesProvider.getWhenPricesAreReady().whenComplete((result, error) -> this.forcePricesLoad());
}

public void forcePricesLoad() {
if (this.pricesLoadCallbacks != null) {
this.pricesLoadCallbacks.forEach(Runnable::run);
this.pricesLoadCallbacks = null;
});
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class PlayersListener implements Listener {
private final SuperiorSkyblockPlugin plugin;
private final Singleton<IslandPreviewListener> islandPreviewListener;
private final Singleton<IslandOutsideListener> islandOutsideListener;
private boolean firstPlayerJoin = true;

public PlayersListener(SuperiorSkyblockPlugin plugin) {
this.plugin = plugin;
Expand All @@ -83,6 +84,12 @@ public PlayersListener(SuperiorSkyblockPlugin plugin) {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onPlayerLogin(PlayerLoginEvent e) {
if (firstPlayerJoin) {
// On first player join, we want to force all prices to be loaded if they haven't by now
plugin.getProviders().forcePricesLoad();
firstPlayerJoin = false;
}

List<SuperiorPlayer> duplicatedPlayers = plugin.getPlayers().matchAllPlayers(superiorPlayer ->
superiorPlayer.getName().equalsIgnoreCase(e.getPlayer().getName()) &&
!superiorPlayer.getUniqueId().equals(e.getPlayer().getUniqueId()));
Expand Down

0 comments on commit 0e0a7fd

Please sign in to comment.