Skip to content

Commit

Permalink
Fixed errors when using ShopGUIPlus v1.73 (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Apr 1, 2022
1 parent 2a5b27b commit 4b69e65
Showing 1 changed file with 12 additions and 2 deletions.
@@ -1,21 +1,27 @@
package com.bgsoftware.wildchests.hooks;

import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.wildchests.WildChestsPlugin;
import com.bgsoftware.wildchests.api.hooks.PricesProvider;
import com.bgsoftware.wildchests.utils.Pair;
import net.brcdev.shopgui.ShopGuiPlugin;
import net.brcdev.shopgui.shop.Shop;
import net.brcdev.shopgui.shop.ShopItem;
import net.brcdev.shopgui.shop.ShopManager;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("unused")
public final class PricesProvider_ShopGUIPlus14 implements PricesProvider {

private static final ReflectMethod<Set<Shop>> GET_SHOPS_METHOD = new ReflectMethod<>(ShopManager.class, Set.class, "getShops");

// Added cache for shop items for better performance
private final Map<WrappedItemStack, Pair<ShopItem, Shop>> cachedShopItems = new HashMap<>();
private final ShopGuiPlugin plugin;
Expand All @@ -33,8 +39,7 @@ public double getPrice(OfflinePlayer offlinePlayer, ItemStack itemStack) {

WrappedItemStack wrappedItemStack = new WrappedItemStack(itemStack);
Pair<ShopItem, Shop> shopPair = cachedShopItems.computeIfAbsent(wrappedItemStack, i -> {
Map<String, Shop> shops = plugin.getShopManager().shops;
for (Shop shop : shops.values()) {
for (Shop shop : getShops()) {
for (ShopItem _shopItem : shop.getShopItems())
if (areSimilar(_shopItem.getItem(), itemStack, _shopItem.isCompareMeta()))
return new Pair<>(_shopItem, shop);
Expand All @@ -54,6 +59,11 @@ public double getPrice(OfflinePlayer offlinePlayer, ItemStack itemStack) {
return price;
}

private Collection<Shop> getShops() {
return GET_SHOPS_METHOD.isValid() ? GET_SHOPS_METHOD.invoke(plugin.getShopManager()) :
plugin.getShopManager().shops.values();
}

private static boolean areSimilar(ItemStack is1, ItemStack is2, boolean compareMetadata) {
return compareMetadata ? is1.isSimilar(is2) : is2 != null && is1 != null && is1.getType() == is2.getType() &&
is1.getDurability() == is2.getDurability();
Expand Down

0 comments on commit 4b69e65

Please sign in to comment.