extract some bundle stuff into its own util class

Took 24 minutes
This commit is contained in:
Kiran Hart 2022-11-25 14:16:56 -05:00
parent 4990d28ae3
commit 13652e485f
No known key found for this signature in database
GPG Key ID: 5F36C7BC79D3EBC3
3 changed files with 52 additions and 8 deletions

View File

@ -19,4 +19,6 @@
package ca.tweetzy.auctionhouse.ahv3.api; package ca.tweetzy.auctionhouse.ahv3.api;
public interface AuctionHouseAPI { public interface AuctionHouseAPI {
} }

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<ItemStack> extractBundleItems(@NonNull final ItemStack itemStack) {
final List<ItemStack> 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;
}
}

View File

@ -19,7 +19,7 @@
package ca.tweetzy.auctionhouse.listeners; package ca.tweetzy.auctionhouse.listeners;
import ca.tweetzy.auctionhouse.AuctionHouse; 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.UpdateChecker;
import ca.tweetzy.auctionhouse.api.hook.PlaceholderAPIHook; import ca.tweetzy.auctionhouse.api.hook.PlaceholderAPIHook;
import ca.tweetzy.auctionhouse.auction.AuctionPlayer; import ca.tweetzy.auctionhouse.auction.AuctionPlayer;
@ -52,7 +52,6 @@ import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.List; 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)) if (heldItem == null || (e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK))
return; return;
if (heldItem.getType() == XMaterial.AIR.parseMaterial()) return; if (heldItem.getType() == XMaterial.AIR.parseMaterial()) return;
if (!NBTEditor.contains(heldItem, "AuctionBundleItem")) return; if (!BundleUtil.isBundledItem(heldItem)) return;
e.setCancelled(true); e.setCancelled(true);
final List<ItemStack> items = new ArrayList<>(); final List<ItemStack> items = BundleUtil.extractBundleItems(heldItem);
for (int i = 0; i < NBTEditor.getInt(heldItem, "AuctionBundleItem"); i++) {
items.add(AuctionAPI.getInstance().deserializeItem(NBTEditor.getByteArray(heldItem, "AuctionBundleItem-" + i)));
}
if (heldItem.getAmount() >= 2) { if (heldItem.getAmount() >= 2) {
heldItem.setAmount(heldItem.getAmount() - 1); heldItem.setAmount(heldItem.getAmount() - 1);