Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c83410f
commit d5d2083
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } | ||
} |
83 changes: 83 additions & 0 deletions
83
...GUIPlus78/src/main/java/com/bgsoftware/wildchests/hooks/PricesProvider_ShopGUIPlus78.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters