From 13652e485f0e79bb03feb0a2b735d644df06107e Mon Sep 17 00:00:00 2001 From: Kiran Hart Date: Fri, 25 Nov 2022 14:16:56 -0500 Subject: [PATCH] extract some bundle stuff into its own util class Took 24 minutes --- .../ahv3/api/AuctionHouseAPI.java | 2 + .../auctionhouse/ahv3/model/BundleUtil.java | 47 +++++++++++++++++++ .../listeners/PlayerListeners.java | 11 ++--- 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 src/main/java/ca/tweetzy/auctionhouse/ahv3/model/BundleUtil.java diff --git a/src/main/java/ca/tweetzy/auctionhouse/ahv3/api/AuctionHouseAPI.java b/src/main/java/ca/tweetzy/auctionhouse/ahv3/api/AuctionHouseAPI.java index d1f2bb3..d9e32b1 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/ahv3/api/AuctionHouseAPI.java +++ b/src/main/java/ca/tweetzy/auctionhouse/ahv3/api/AuctionHouseAPI.java @@ -19,4 +19,6 @@ package ca.tweetzy.auctionhouse.ahv3.api; public interface AuctionHouseAPI { + + } diff --git a/src/main/java/ca/tweetzy/auctionhouse/ahv3/model/BundleUtil.java b/src/main/java/ca/tweetzy/auctionhouse/ahv3/model/BundleUtil.java new file mode 100644 index 0000000..9ea2ade --- /dev/null +++ b/src/main/java/ca/tweetzy/auctionhouse/ahv3/model/BundleUtil.java @@ -0,0 +1,47 @@ +/* + * Auction House + * Copyright 2022 Kiran Hart + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package ca.tweetzy.auctionhouse.ahv3.model; + +import ca.tweetzy.auctionhouse.api.AuctionAPI; +import ca.tweetzy.core.compatibility.XMaterial; +import ca.tweetzy.flight.comp.NBTEditor; +import lombok.NonNull; +import lombok.experimental.UtilityClass; +import org.bukkit.inventory.ItemStack; + +import java.util.ArrayList; +import java.util.List; + +@UtilityClass +public final class BundleUtil { + + public boolean isBundledItem(@NonNull final ItemStack itemStack) { + return itemStack.getType() != XMaterial.AIR.parseMaterial() && NBTEditor.contains(itemStack, "AuctionBundleItem"); + } + + public List extractBundleItems(@NonNull final ItemStack itemStack) { + final List items = new ArrayList<>(); + + for (int i = 0; i < NBTEditor.getInt(itemStack, "AuctionBundleItem"); i++) { + items.add(AuctionAPI.getInstance().deserializeItem(NBTEditor.getByteArray(itemStack, "AuctionBundleItem-" + i))); + } + + return items; + } +} diff --git a/src/main/java/ca/tweetzy/auctionhouse/listeners/PlayerListeners.java b/src/main/java/ca/tweetzy/auctionhouse/listeners/PlayerListeners.java index 355b318..3bb704c 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/listeners/PlayerListeners.java +++ b/src/main/java/ca/tweetzy/auctionhouse/listeners/PlayerListeners.java @@ -19,7 +19,7 @@ package ca.tweetzy.auctionhouse.listeners; import ca.tweetzy.auctionhouse.AuctionHouse; -import ca.tweetzy.auctionhouse.api.AuctionAPI; +import ca.tweetzy.auctionhouse.ahv3.model.BundleUtil; import ca.tweetzy.auctionhouse.api.UpdateChecker; import ca.tweetzy.auctionhouse.api.hook.PlaceholderAPIHook; import ca.tweetzy.auctionhouse.auction.AuctionPlayer; @@ -52,7 +52,6 @@ import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; -import java.util.ArrayList; import java.util.List; /** @@ -149,14 +148,10 @@ public class PlayerListeners implements Listener { if (heldItem == null || (e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK)) return; if (heldItem.getType() == XMaterial.AIR.parseMaterial()) return; - if (!NBTEditor.contains(heldItem, "AuctionBundleItem")) return; + if (!BundleUtil.isBundledItem(heldItem)) return; e.setCancelled(true); - final List items = new ArrayList<>(); - - for (int i = 0; i < NBTEditor.getInt(heldItem, "AuctionBundleItem"); i++) { - items.add(AuctionAPI.getInstance().deserializeItem(NBTEditor.getByteArray(heldItem, "AuctionBundleItem-" + i))); - } + final List items = BundleUtil.extractBundleItems(heldItem); if (heldItem.getAmount() >= 2) { heldItem.setAmount(heldItem.getAmount() - 1);