Skip to content

Commit

Permalink
Bring support for older ShopGUIPlus versions back
Browse files Browse the repository at this point in the history
Fixes #473
  • Loading branch information
Krakenied committed Dec 17, 2022
1 parent 3861112 commit 2152026
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 27 deletions.
Expand Up @@ -349,8 +349,9 @@ public void onEnable() {
}
if (Bukkit.getPluginManager().isPluginEnabled("ShopGUIPlus")) {
// not tested
taskTypeManager.registerTaskType(new ShopGUIPlusBuyTaskType(this));
taskTypeManager.registerTaskType(new ShopGUIPlusSellTaskType(this));
String shopGUIPlusVersion = Bukkit.getPluginManager().getPlugin("ShopGUIPlus").getDescription().getVersion();
taskTypeManager.registerTaskType(new ShopGUIPlusBuyTaskType(this, shopGUIPlusVersion));
taskTypeManager.registerTaskType(new ShopGUIPlusSellTaskType(this, shopGUIPlusVersion));
}
if (Bukkit.getPluginManager().isPluginEnabled("FabledSkyblock")) {
// not tested
Expand Down
Expand Up @@ -4,52 +4,79 @@
import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType;
import com.leonardobishop.quests.bukkit.util.TaskUtils;
import com.leonardobishop.quests.common.player.QPlayer;
import com.leonardobishop.quests.common.player.QPlayerManager;
import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress;
import com.leonardobishop.quests.common.quest.Quest;
import com.leonardobishop.quests.common.quest.Task;
import net.brcdev.shopgui.event.ShopPostTransactionEvent;
import net.brcdev.shopgui.shop.Shop;
import net.brcdev.shopgui.shop.ShopManager.ShopAction;
import net.brcdev.shopgui.shop.ShopTransactionResult;
import net.brcdev.shopgui.shop.item.ShopItem;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class ShopGUIPlusBuyTaskType extends BukkitTaskType {

private final BukkitQuestsPlugin plugin;
private Method getShopItemMethod;
private Method getShopMethod;
private Method getIdMethod;

public ShopGUIPlusBuyTaskType(BukkitQuestsPlugin plugin) {
super("shopguiplus_buy", TaskUtils.TASK_ATTRIBUTION_STRING, "Purchase a given item from a ShopGUI+ shop", "shopguiplus_buycertain");
public ShopGUIPlusBuyTaskType(BukkitQuestsPlugin plugin, String shopGUIPlusVersion) {
super("shopguiplus_buy", TaskUtils.TASK_ATTRIBUTION_STRING, "Purchase a given item from a ShopGUIPlus shop", "shopguiplus_buycertain");
this.plugin = plugin;

super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "amount"));
super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "amount"));
super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "shop-id"));
super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "item-id"));

try {
Class<?> clazz = Class.forName("net.brcdev.shopgui.shop.ShopTransactionResult");
this.getShopItemMethod = clazz.getDeclaredMethod("getShopItem");

Class<?> returnType = this.getShopItemMethod.getReturnType();
this.getShopMethod = returnType.getDeclaredMethod("getShop");
this.getIdMethod = returnType.getDeclaredMethod("getId");

return;
} catch (ClassNotFoundException | NoSuchMethodException ignored) { }

plugin.getLogger().severe("Failed to register event handler for ShopGUIPlus task type!");
plugin.getLogger().severe("ShopGUIPlus version detected: " + shopGUIPlusVersion);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void afterTransaction(ShopPostTransactionEvent event) {
public void onShopPostTransaction(ShopPostTransactionEvent event) {
ShopTransactionResult result = event.getResult();
ShopAction shopAction = result.getShopAction();
if (shopAction != ShopAction.BUY) {
return;
}

Player player = result.getPlayer();
QPlayerManager playerManager = this.plugin.getPlayerManager();
QPlayer qPlayer = playerManager.getPlayer(player.getUniqueId());
QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId());
if (qPlayer == null) {
return;
}

ShopItem shopItem = result.getShopItem();
Shop shop = shopItem.getShop();
String shopId = shop.getId();
String itemId = shopItem.getId();

Shop shop;
String itemId;
String shopId;

try {
Object shopItem = getShopItemMethod.invoke(result);
shop = (Shop) getShopMethod.invoke(shopItem);
itemId = (String) getIdMethod.invoke(shopItem);
shopId = shop.getId();
} catch (InvocationTargetException | IllegalAccessException e) {
// It should never happen
return;
}

int amountBought = result.getAmount();

for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) {
Expand Down
Expand Up @@ -4,60 +4,87 @@
import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType;
import com.leonardobishop.quests.bukkit.util.TaskUtils;
import com.leonardobishop.quests.common.player.QPlayer;
import com.leonardobishop.quests.common.player.QPlayerManager;
import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress;
import com.leonardobishop.quests.common.quest.Quest;
import com.leonardobishop.quests.common.quest.Task;
import net.brcdev.shopgui.event.ShopPostTransactionEvent;
import net.brcdev.shopgui.shop.Shop;
import net.brcdev.shopgui.shop.ShopManager.ShopAction;
import net.brcdev.shopgui.shop.ShopTransactionResult;
import net.brcdev.shopgui.shop.item.ShopItem;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class ShopGUIPlusSellTaskType extends BukkitTaskType {

private final BukkitQuestsPlugin plugin;
private Method getShopItemMethod;
private Method getShopMethod;
private Method getIdMethod;

public ShopGUIPlusSellTaskType(BukkitQuestsPlugin plugin) {
super("shopguiplus_sell", TaskUtils.TASK_ATTRIBUTION_STRING, "Sell a given item to a ShopGUI+ shop", "shopguiplus_sellcertain");
public ShopGUIPlusSellTaskType(BukkitQuestsPlugin plugin, String shopGUIPlusVersion) {
super("shopguiplus_sell", TaskUtils.TASK_ATTRIBUTION_STRING, "Sell a given item to a ShopGUIPlus shop", "shopguiplus_sellcertain");
this.plugin = plugin;

super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "amount"));
super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "amount"));
super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "shop-id"));
super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "item-id"));

try {
Class<?> clazz = Class.forName("net.brcdev.shopgui.shop.ShopTransactionResult");
this.getShopItemMethod = clazz.getDeclaredMethod("getShopItem");

Class<?> returnType = this.getShopItemMethod.getReturnType();
this.getShopMethod = returnType.getDeclaredMethod("getShop");
this.getIdMethod = returnType.getDeclaredMethod("getId");

return;
} catch (ClassNotFoundException | NoSuchMethodException ignored) { }

plugin.getLogger().severe("Failed to register event handler for ShopGUIPlus task type!");
plugin.getLogger().severe("ShopGUIPlus version detected: " + shopGUIPlusVersion);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void afterTransaction(ShopPostTransactionEvent event) {
public void onShopPostTransaction(ShopPostTransactionEvent event) {
ShopTransactionResult result = event.getResult();
ShopAction shopAction = result.getShopAction();
if (shopAction != ShopAction.SELL && shopAction != ShopAction.SELL_ALL) {
return;
}

Player player = result.getPlayer();
QPlayerManager playerManager = this.plugin.getPlayerManager();
QPlayer qPlayer = playerManager.getPlayer(player.getUniqueId());
QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId());
if (qPlayer == null) {
return;
}

ShopItem shopItem = result.getShopItem();
Shop shop = shopItem.getShop();
String shopId = shop.getId();
String itemId = shopItem.getId();
Shop shop;
String itemId;
String shopId;

try {
Object shopItem = getShopItemMethod.invoke(result);
shop = (Shop) getShopMethod.invoke(shopItem);
itemId = (String) getIdMethod.invoke(shopItem);
shopId = shop.getId();
} catch (InvocationTargetException | IllegalAccessException e) {
// It should never happen
return;
}

int amountBought = result.getAmount();

for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) {
Quest quest = pendingTask.quest();
Task task = pendingTask.task();
TaskProgress taskProgress = pendingTask.taskProgress();

super.debug("Player bought item (shop = " + shopId + ", item id = " + itemId + ")", quest.getId(), task.getId(), player.getUniqueId());
super.debug("Player sold item (shop = " + shopId + ", item id = " + itemId + ")", quest.getId(), task.getId(), player.getUniqueId());

String taskShopId = (String) task.getConfigValue("shop-id");
if (taskShopId == null || !taskShopId.equals(shopId)) {
Expand Down

0 comments on commit 2152026

Please sign in to comment.