Skip to content

Commit

Permalink
Added support for ShopGUIPlus v1.78
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Aug 6, 2022
1 parent c83410f commit d5d2083
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Hook_ShopGUIPlus78/build.gradle
@@ -0,0 +1,13 @@
group 'Hook_ShopGUIPlus78'

dependencies {
compileOnly 'net.brcdev:ShopGUIPlus-1.78:latest'
compileOnly "org.spigotmc:v1_8_R3-Taco:latest"
compileOnly project(":API")
compileOnly parent
}

if (project.hasProperty('hook.compile_shopguiplus') &&
!Boolean.valueOf(project.findProperty("hook.compile_shopguiplus").toString())) {
project.tasks.all { task -> task.enabled = false }
}
@@ -0,0 +1,83 @@
package com.bgsoftware.wildchests.hooks;

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.item.ShopItem;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Map;

public class PricesProvider_ShopGUIPlus78 implements PricesProvider {

// Added cache for shop items for better performance
private final Map<WrappedItemStack, Pair<ShopItem, Shop>> cachedShopItems = new HashMap<>();
private final ShopGuiPlugin plugin;

public PricesProvider_ShopGUIPlus78() {
WildChestsPlugin.log("- Using ShopGUIPlus as PricesProvider");
plugin = ShopGuiPlugin.getInstance();
}

@Override
public double getPrice(OfflinePlayer offlinePlayer, ItemStack itemStack) {
Player onlinePlayer = offlinePlayer.getPlayer();

double price = 0;

WrappedItemStack wrappedItemStack = new WrappedItemStack(itemStack);
Pair<ShopItem, Shop> shopPair = cachedShopItems.computeIfAbsent(wrappedItemStack, i -> {
for (Shop shop : plugin.getShopManager().getShops()) {
for (ShopItem _shopItem : shop.getShopItems())
if (areSimilar(_shopItem.getItem(), itemStack, _shopItem.isCompareMeta()))
return new Pair<>(_shopItem, shop);
}

return null;
});

if (shopPair != null) {
if (onlinePlayer == null) {
price = Math.max(price, shopPair.key.getSellPriceForAmount(itemStack.getAmount()));
} else {
price = Math.max(price, shopPair.key.getSellPriceForAmount(onlinePlayer, itemStack.getAmount()));
}
}

return price;
}

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();
}

private static final class WrappedItemStack {

private final ItemStack value;

WrappedItemStack(ItemStack value) {
this.value = value.clone();
this.value.setAmount(1);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WrappedItemStack that = (WrappedItemStack) o;
return value.equals(that.value);
}

@Override
public int hashCode() {
return value.hashCode();
}
}

}
1 change: 1 addition & 0 deletions settings.gradle
Expand Up @@ -8,6 +8,7 @@ include 'Hook_Essentials'
include 'Hook_QuantumShop'
include 'Hook_ShopGUIPlus12'
include 'Hook_ShopGUIPlus14'
include 'Hook_ShopGUIPlus78'
include 'Hook_SuperiorSkyblock'
include 'Hook_TransportPipes'
include 'Hook_Vault'
Expand Down
Expand Up @@ -187,7 +187,10 @@ private void registerPricesProvider(WildChestsPlugin plugin) {
Plugin shopGUIPlus = Bukkit.getPluginManager().getPlugin("ShopGUIPlus");
if (shopGUIPlus.getDescription().getVersion().startsWith("1.2")) {
pricesProvider = createInstance("PricesProvider_ShopGUIPlus12");
} else {
} else try {
Class.forName("net.brcdev.shopgui.shop.item.ShopItem");
pricesProvider = createInstance("PricesProvider_ShopGUIPlus78");
} catch (ClassNotFoundException error) {
pricesProvider = createInstance("PricesProvider_ShopGUIPlus14");
}
}
Expand Down

0 comments on commit d5d2083

Please sign in to comment.