|
| 1 | +package net.craftingstore.bukkit.inventory; |
| 2 | + |
| 3 | +import net.craftingstore.bukkit.CraftingStoreBukkit; |
| 4 | +import net.craftingstore.bukkit.util.ChatColorUtil; |
| 5 | +import net.craftingstore.core.exceptions.CraftingStoreApiException; |
| 6 | +import net.craftingstore.core.models.api.ApiInventory; |
| 7 | +import net.craftingstore.core.models.api.ApiPackageInformation; |
| 8 | +import net.craftingstore.core.models.api.inventory.CraftingStoreInventory; |
| 9 | +import net.craftingstore.core.models.api.inventory.InventoryItem; |
| 10 | +import net.craftingstore.core.models.api.inventory.InventoryItemIcon; |
| 11 | +import net.craftingstore.core.models.api.inventory.InventoryItemType; |
| 12 | +import net.craftingstore.core.models.api.inventory.types.InventoryItemBuyablePackage; |
| 13 | +import org.bukkit.entity.Player; |
| 14 | +import org.bukkit.inventory.Inventory; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.concurrent.ExecutionException; |
| 19 | + |
| 20 | +public class BuyInventoryBuilder { |
| 21 | + |
| 22 | + private final CraftingStoreBukkit instance; |
| 23 | + private final InventoryBuilder inventoryBuilder; |
| 24 | + |
| 25 | + public BuyInventoryBuilder(CraftingStoreBukkit instance, InventoryBuilder inventoryBuilder) { |
| 26 | + this.instance = instance; |
| 27 | + this.inventoryBuilder = inventoryBuilder; |
| 28 | + } |
| 29 | + |
| 30 | + public Inventory build(Player p, InventoryItemBuyablePackage item, CraftingStoreInventoryHolder holder) throws CraftingStoreApiException, ExecutionException, InterruptedException { |
| 31 | + ApiInventory buyMenu = this.instance.getCraftingStore().getApi().getGUI().get().getBuyMenu(); |
| 32 | + CraftingStoreInventory craftingStoreInventory = new CraftingStoreInventory(buyMenu.getTitle(), buyMenu.getContent(), buyMenu.getSize()); |
| 33 | + |
| 34 | + String ip = p.getAddress().getAddress().getHostAddress(); |
| 35 | + ApiPackageInformation packageInformation = this.instance.getCraftingStore().getApi().getPackageInformation( |
| 36 | + p.getName(), |
| 37 | + p.getUniqueId(), |
| 38 | + ip, |
| 39 | + item.getPackageId() |
| 40 | + ).get(); |
| 41 | + if (!packageInformation.isAllowedToBuy()) { |
| 42 | + p.sendMessage(ChatColorUtil.translate(packageInformation.getMessage())); |
| 43 | + p.closeInventory(); |
| 44 | + return null; |
| 45 | + } |
| 46 | + if (packageInformation.getPrice() != item.getPrice()) { |
| 47 | + p.sendMessage(this.instance.getPrefix() + "There was a problem with the payment data. Please try again later."); |
| 48 | + p.closeInventory(); |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + Map placeholders = new HashMap<>(); |
| 53 | + placeholders.put("package_name", item.getName()); |
| 54 | + placeholders.put("package_price_ingame", packageInformation.getPrice()); |
| 55 | + return this.inventoryBuilder.buildInventory( |
| 56 | + craftingStoreInventory, |
| 57 | + new BuyMenuInventoryHolder(craftingStoreInventory, holder, item), |
| 58 | + placeholders |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + public Inventory buildLoadInventory() { |
| 63 | + InventoryItemIcon icon = new InventoryItemIcon("LIGHT_BLUE_STAINED_GLASS_PANE", 1, 0, null); |
| 64 | + InventoryItem[] content = new InventoryItem[9]; |
| 65 | + for (int i = 0; i < 9; i++) { |
| 66 | + content[i] = new InventoryItem("Loading...", null, InventoryItemType.NULL, icon, i); |
| 67 | + } |
| 68 | + |
| 69 | + CraftingStoreInventory craftingStoreInventory = new CraftingStoreInventory( |
| 70 | + "Loading...", |
| 71 | + content, |
| 72 | + 9 |
| 73 | + ); |
| 74 | + return this.inventoryBuilder.buildInventory( |
| 75 | + craftingStoreInventory, |
| 76 | + new CraftingStoreInventoryHolder(craftingStoreInventory, null) |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
0 commit comments