summaryrefslogtreecommitdiffstats
path: root/bukkit/src/main/java
diff options
context:
space:
mode:
authorLMBishop <13875753+LMBishop@users.noreply.github.com>2023-07-08 16:41:55 +0100
committerLMBishop <13875753+LMBishop@users.noreply.github.com>2023-07-08 16:55:31 +0100
commit8a0e5d36465f4cc74615be7c9da0ef20c6a9e3df (patch)
tree329e18a74ef2cd86f613d3225fdd1808be48fc8c /bukkit/src/main/java
parent758fe7f40a9420a5c1c7653622b54376cdd042b2 (diff)
Add null check for shop and de-duplicate common code
Diffstat (limited to 'bukkit/src/main/java')
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusBuyTaskType.java99
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusInteractionTaskType.java126
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusSellTaskType.java98
3 files changed, 137 insertions, 186 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusBuyTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusBuyTaskType.java
index ad065851..4672c5d7 100644
--- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusBuyTaskType.java
+++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusBuyTaskType.java
@@ -18,103 +18,16 @@ 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 final class ShopGUIPlusBuyTaskType extends ShopGUIPlusInteractionTaskType {
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);
+ super(plugin, shopGUIPlusVersion, "shopguiplus_buy", TaskUtils.TASK_ATTRIBUTION_STRING, "Purchase a given item from a ShopGUIPlus shop", "shopguiplus_buycertain");
}
- @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
- public void onShopPostTransaction(ShopPostTransactionEvent event) {
- ShopTransactionResult result = event.getResult();
- if (result.getResult() != ShopTransactionResult.ShopTransactionResultType.SUCCESS) {
- return;
- }
-
+ @Override
+ public boolean isCorrectInteraction(ShopTransactionResult result) {
ShopAction shopAction = result.getShopAction();
- if (shopAction != ShopAction.BUY) {
- return;
- }
-
- Player player = result.getPlayer();
- QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId());
- if (qPlayer == null) {
- return;
- }
-
- 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());
-
- String taskShopId = (String) task.getConfigValue("shop-id");
- if (taskShopId == null || !taskShopId.equals(shopId)) {
- super.debug("Shop id does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId());
- continue;
- }
-
- String taskItemId = (String) task.getConfigValue("item-id");
- if (taskItemId == null || !taskItemId.equals(itemId)) {
- super.debug("Item id does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId());
- continue;
- }
-
- int amountNeeded = (int) task.getConfigValue("amount");
-
- int progress = TaskUtils.getIntegerTaskProgress(taskProgress);
- int newProgress = progress + amountBought;
- taskProgress.setProgress(newProgress);
-
- super.debug("Updating task progress (now " + newProgress + ")", quest.getId(), task.getId(), player.getUniqueId());
-
- if (newProgress >= amountNeeded) {
- super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId());
- taskProgress.setProgress(amountNeeded);
- taskProgress.setCompleted(true);
- }
- }
+ return shopAction == ShopAction.BUY;
}
+
}
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusInteractionTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusInteractionTaskType.java
new file mode 100644
index 00000000..b61abb04
--- /dev/null
+++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusInteractionTaskType.java
@@ -0,0 +1,126 @@
+package com.leonardobishop.quests.bukkit.tasktype.type.dependent;
+
+import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin;
+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.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;
+import net.brcdev.shopgui.shop.ShopTransactionResult;
+import org.bukkit.entity.Player;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.EventPriority;
+import org.jetbrains.annotations.NotNull;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public abstract class ShopGUIPlusInteractionTaskType extends BukkitTaskType {
+
+ private final BukkitQuestsPlugin plugin;
+ private Method getShopItemMethod;
+ private Method getShopMethod;
+ private Method getIdMethod;
+
+ public ShopGUIPlusInteractionTaskType(BukkitQuestsPlugin plugin, String shopGUIPlusVersion, @NotNull String type, String author, String description, String... aliases) {
+ super(type, author, description, aliases);
+ 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);
+ }
+
+ public abstract boolean isCorrectInteraction(ShopTransactionResult result);
+
+ @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
+ public void onShopPostTransaction(ShopPostTransactionEvent event) {
+ ShopTransactionResult result = event.getResult();
+ if (result.getResult() != ShopTransactionResult.ShopTransactionResultType.SUCCESS) {
+ return;
+ }
+
+ if (!isCorrectInteraction(result)) {
+ return;
+ }
+
+ Player player = result.getPlayer();
+ QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId());
+ if (qPlayer == null) {
+ return;
+ }
+
+ Shop shop;
+ String itemId;
+ String shopId;
+
+ try {
+ Object shopItem = getShopItemMethod.invoke(result);
+ shop = (Shop) getShopMethod.invoke(shopItem);
+ itemId = (String) getIdMethod.invoke(shopItem);
+ if (shop != null) {
+ shopId = shop.getId();
+ } else {
+ shopId = null;
+ }
+ } 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 traded item (shop = " + shopId + ", item id = " + itemId + ", action = " + result.getShopAction().toString() + ")", quest.getId(), task.getId(), player.getUniqueId());
+
+ String taskShopId = (String) task.getConfigValue("shop-id");
+ if (taskShopId == null || !taskShopId.equals(shopId)) {
+ super.debug("Shop id does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId());
+ continue;
+ }
+
+ String taskItemId = (String) task.getConfigValue("item-id");
+ if (taskItemId == null || !taskItemId.equals(itemId)) {
+ super.debug("Item id does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId());
+ continue;
+ }
+
+ int amountNeeded = (int) task.getConfigValue("amount");
+
+ int progress = TaskUtils.getIntegerTaskProgress(taskProgress);
+ int newProgress = progress + amountBought;
+ taskProgress.setProgress(newProgress);
+
+ super.debug("Updating task progress (now " + newProgress + ")", quest.getId(), task.getId(), player.getUniqueId());
+
+ if (newProgress >= amountNeeded) {
+ super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId());
+ taskProgress.setProgress(amountNeeded);
+ taskProgress.setCompleted(true);
+ }
+ }
+ }
+}
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusSellTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusSellTaskType.java
index e07eed98..b97e508e 100644
--- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusSellTaskType.java
+++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusSellTaskType.java
@@ -18,103 +18,15 @@ 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 final class ShopGUIPlusSellTaskType extends ShopGUIPlusInteractionTaskType {
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);
+ super(plugin, shopGUIPlusVersion, "shopguiplus_sell", TaskUtils.TASK_ATTRIBUTION_STRING, "Sell a given item to a ShopGUIPlus shop", "shopguiplus_sellcertain");
}
- @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
- public void onShopPostTransaction(ShopPostTransactionEvent event) {
- ShopTransactionResult result = event.getResult();
- if (result.getResult() != ShopTransactionResult.ShopTransactionResultType.SUCCESS) {
- return;
- }
-
+ @Override
+ public boolean isCorrectInteraction(ShopTransactionResult result) {
ShopAction shopAction = result.getShopAction();
- if (shopAction != ShopAction.SELL && shopAction != ShopAction.SELL_ALL) {
- return;
- }
-
- Player player = result.getPlayer();
- QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId());
- if (qPlayer == null) {
- return;
- }
-
- 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 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)) {
- super.debug("Shop id does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId());
- continue;
- }
-
- String taskItemId = (String) task.getConfigValue("item-id");
- if (taskItemId == null || !taskItemId.equals(itemId)) {
- super.debug("Item id does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId());
- continue;
- }
-
- int amountNeeded = (int) task.getConfigValue("amount");
-
- int progress = TaskUtils.getIntegerTaskProgress(taskProgress);
- int newProgress = progress + amountBought;
- taskProgress.setProgress(newProgress);
-
- super.debug("Updating task progress (now " + newProgress + ")", quest.getId(), task.getId(), player.getUniqueId());
-
- if (newProgress >= amountNeeded) {
- super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId());
- taskProgress.setProgress(amountNeeded);
- taskProgress.setCompleted(true);
- }
- }
+ return shopAction == ShopAction.SELL || shopAction == ShopAction.SELL_ALL;
}
}