From 5cfd48300eabf9c15edf4cd90caf3fd39b1245f8 Mon Sep 17 00:00:00 2001 From: Thijs Alders Date: Wed, 28 Jul 2021 00:19:39 +0200 Subject: [PATCH] Add virtual currency. --- assembly/pom.xml | 4 + bukkit/pom.xml | 10 ++ .../bukkit/BukkitPluginConfiguration.java | 5 + .../bukkit/CraftingStoreBukkit.java | 20 +++ .../bukkit/commands/BuyCommand.java | 7 +- .../craftingstore/bukkit/hooks/VaultHook.java | 27 ++++ .../bukkit/inventory/BuyInventoryBuilder.java | 79 ++++++++++ .../inventory/BuyMenuInventoryHolder.java | 22 +++ .../bukkit/inventory/InventoryBuilder.java | 20 ++- .../inventory/handlers/BackButtonHandler.java | 6 +- .../inventory/handlers/BuyButtonHandler.java | 139 ++++++++++++++++++ .../handlers/BuyablePackageHandler.java | 57 +++++++ .../handlers/CategoryItemHandler.java | 2 +- .../bukkit/listeners/InventoryListener.java | 25 ++-- bukkit/src/main/resources/plugin.yml | 2 +- .../bungee/BungeePluginConfiguration.java | 5 + core/pom.xml | 5 + .../craftingstore/core/CraftingStoreAPI.java | 26 ++-- .../core/PluginConfiguration.java | 2 + .../core/http/CraftingStoreAPIImpl.java | 88 ++++++++--- .../core/http/CraftingStoreCachedAPI.java | 8 +- .../core/http/util/InventoryAdapter.java | 2 +- .../core/http/util/JsonResponseHandlerV7.java | 45 ++++++ .../core/models/api/ApiInventory.java | 26 ++++ .../models/api/ApiPackageInformation.java | 19 +++ .../craftingstore/core/models/api/RootV7.java | 17 +++ .../models/api/inventory/InventoryItem.java | 22 ++- .../api/inventory/InventoryItemIcon.java | 10 ++ .../api/inventory/InventoryItemType.java | 17 ++- .../types/InventoryItemBackButton.java | 13 ++ .../types/InventoryItemBuyButton.java | 29 ++++ .../types/InventoryItemBuyDetails.java | 19 +++ .../types/InventoryItemBuyablePackage.java | 30 ++++ .../inventory/types/InventoryItemMessage.java | 18 +++ .../request/PackageInformationRequest.java | 17 +++ .../api/request/PaymentCreateRequest.java | 29 ++++ .../core/provider/SocketProvider.java | 2 +- pom.xml | 2 +- .../sponge/SpongePluginConfiguration.java | 5 + .../sponge/commands/BuyCommand.java | 6 +- .../velocity/VelocityPluginConfiguration.java | 5 + 41 files changed, 817 insertions(+), 75 deletions(-) create mode 100644 bukkit/src/main/java/net/craftingstore/bukkit/hooks/VaultHook.java create mode 100644 bukkit/src/main/java/net/craftingstore/bukkit/inventory/BuyInventoryBuilder.java create mode 100644 bukkit/src/main/java/net/craftingstore/bukkit/inventory/BuyMenuInventoryHolder.java create mode 100644 bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/BuyButtonHandler.java create mode 100644 bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/BuyablePackageHandler.java create mode 100644 core/src/main/java/net/craftingstore/core/http/util/JsonResponseHandlerV7.java create mode 100644 core/src/main/java/net/craftingstore/core/models/api/ApiInventory.java create mode 100644 core/src/main/java/net/craftingstore/core/models/api/ApiPackageInformation.java create mode 100644 core/src/main/java/net/craftingstore/core/models/api/RootV7.java create mode 100644 core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyButton.java create mode 100644 core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyDetails.java create mode 100644 core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyablePackage.java create mode 100644 core/src/main/java/net/craftingstore/core/models/api/request/PackageInformationRequest.java create mode 100644 core/src/main/java/net/craftingstore/core/models/api/request/PaymentCreateRequest.java diff --git a/assembly/pom.xml b/assembly/pom.xml index 14ea400..324a619 100644 --- a/assembly/pom.xml +++ b/assembly/pom.xml @@ -56,6 +56,10 @@ org.apache.commons.codec net.craftingstore.libraries.apache.commons.codec + + org.apache.commons.text + net.craftingstore.libraries.apache.commons.text + org.json net.craftingstore.libraries.json diff --git a/bukkit/pom.xml b/bukkit/pom.xml index 97a3c97..39760dc 100644 --- a/bukkit/pom.xml +++ b/bukkit/pom.xml @@ -49,6 +49,10 @@ https://repo.codemc.org/repository/maven-public + + jitpack.io + https://jitpack.io + @@ -80,5 +84,11 @@ XSeries 7.9.1.1 + + com.github.MilkBowl + VaultAPI + 1.7 + provided + \ No newline at end of file diff --git a/bukkit/src/main/java/net/craftingstore/bukkit/BukkitPluginConfiguration.java b/bukkit/src/main/java/net/craftingstore/bukkit/BukkitPluginConfiguration.java index f30cc7c..0f5e796 100644 --- a/bukkit/src/main/java/net/craftingstore/bukkit/BukkitPluginConfiguration.java +++ b/bukkit/src/main/java/net/craftingstore/bukkit/BukkitPluginConfiguration.java @@ -39,4 +39,9 @@ public class BukkitPluginConfiguration implements PluginConfiguration { public int getTimeBetweenCommands() { return plugin.getConfig().getInt("time-between-commands", 200); } + + @Override + public String getNotEnoughBalanceMessage() { + return plugin.getConfig().getString("not-enough-balance-message", "&4You do not have enough in-game money in your account! You can also buy this package on the website:"); + } } diff --git a/bukkit/src/main/java/net/craftingstore/bukkit/CraftingStoreBukkit.java b/bukkit/src/main/java/net/craftingstore/bukkit/CraftingStoreBukkit.java index ee13087..abf801f 100644 --- a/bukkit/src/main/java/net/craftingstore/bukkit/CraftingStoreBukkit.java +++ b/bukkit/src/main/java/net/craftingstore/bukkit/CraftingStoreBukkit.java @@ -4,6 +4,7 @@ import net.craftingstore.bukkit.commands.BuyCommand; import net.craftingstore.bukkit.commands.CraftingStoreCommand; import net.craftingstore.bukkit.config.Config; import net.craftingstore.bukkit.hooks.PlaceholderAPIHook; +import net.craftingstore.bukkit.hooks.VaultHook; import net.craftingstore.bukkit.listeners.InventoryListener; import net.craftingstore.bukkit.listeners.AdminJoinListener; import net.craftingstore.bukkit.listeners.PendingDonationJoinListener; @@ -18,6 +19,7 @@ public class CraftingStoreBukkit extends JavaPlugin { private CraftingStore craftingStore; private Config config; private String prefix = ChatColor.GRAY + "[" + ChatColor.RED + "CraftingStore" + ChatColor.GRAY + "] " + ChatColor.WHITE; + private VaultHook vaultHook; @Override public void onEnable() { @@ -38,6 +40,16 @@ public class CraftingStoreBukkit extends JavaPlugin { new PlaceholderAPIHook(craftingStore); craftingStore.getLogger().info("Hooked with PlaceholderAPI"); } + // Vault hook + if (Bukkit.getPluginManager().isPluginEnabled("Vault")) { + this.vaultHook = new VaultHook(this); + if (this.vaultHook.register()) { + craftingStore.getLogger().info("Hooked with Vault"); + } else { + craftingStore.getLogger().info("There was a problem hooking with Vault"); + this.vaultHook = null; + } + } } @Override @@ -62,4 +74,12 @@ public class CraftingStoreBukkit extends JavaPlugin { public String getPrefix() { return prefix; } + + public VaultHook getVaultHook() { + return vaultHook; + } + + public boolean isHookedWithVault() { + return vaultHook != null; + } } diff --git a/bukkit/src/main/java/net/craftingstore/bukkit/commands/BuyCommand.java b/bukkit/src/main/java/net/craftingstore/bukkit/commands/BuyCommand.java index e432a04..9cd40ba 100644 --- a/bukkit/src/main/java/net/craftingstore/bukkit/commands/BuyCommand.java +++ b/bukkit/src/main/java/net/craftingstore/bukkit/commands/BuyCommand.java @@ -3,6 +3,7 @@ package net.craftingstore.bukkit.commands; import net.craftingstore.bukkit.CraftingStoreBukkit; import net.craftingstore.bukkit.inventory.InventoryBuilder; import net.craftingstore.core.exceptions.CraftingStoreApiException; +import net.craftingstore.core.models.api.ApiInventory; import net.craftingstore.core.models.api.inventory.CraftingStoreInventory; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -34,8 +35,10 @@ public class BuyCommand implements CommandExecutor { Bukkit.getScheduler().runTaskAsynchronously(instance, () -> { try { InventoryBuilder builder = new InventoryBuilder(this.instance); - CraftingStoreInventory gui = instance.getCraftingStore().getApi().getGUI().get(); - Inventory inventory = builder.buildInventory(gui); + ApiInventory gui = instance.getCraftingStore().getApi().getGUI().get(); + Inventory inventory = builder.buildInventory( + new CraftingStoreInventory(gui.getTitle(), gui.getContent(), gui.getSize()) + ); Bukkit.getScheduler().runTask(instance, () -> { if (p.isOnline()) { p.openInventory(inventory); diff --git a/bukkit/src/main/java/net/craftingstore/bukkit/hooks/VaultHook.java b/bukkit/src/main/java/net/craftingstore/bukkit/hooks/VaultHook.java new file mode 100644 index 0000000..93a3f03 --- /dev/null +++ b/bukkit/src/main/java/net/craftingstore/bukkit/hooks/VaultHook.java @@ -0,0 +1,27 @@ +package net.craftingstore.bukkit.hooks; + +import net.craftingstore.bukkit.CraftingStoreBukkit; +import net.milkbowl.vault.economy.Economy; +import org.bukkit.plugin.RegisteredServiceProvider; + +public class VaultHook { + private final CraftingStoreBukkit instance; + private Economy economy; + + public VaultHook(CraftingStoreBukkit instance) { + this.instance = instance; + } + + public boolean register() { + RegisteredServiceProvider rsp = instance.getServer().getServicesManager().getRegistration(Economy.class); + if (rsp == null) { + return false; + } + this.economy = rsp.getProvider(); + return true; + } + + public Economy getEconomy() { + return economy; + } +} diff --git a/bukkit/src/main/java/net/craftingstore/bukkit/inventory/BuyInventoryBuilder.java b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/BuyInventoryBuilder.java new file mode 100644 index 0000000..84672f4 --- /dev/null +++ b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/BuyInventoryBuilder.java @@ -0,0 +1,79 @@ +package net.craftingstore.bukkit.inventory; + +import net.craftingstore.bukkit.CraftingStoreBukkit; +import net.craftingstore.bukkit.util.ChatColorUtil; +import net.craftingstore.core.exceptions.CraftingStoreApiException; +import net.craftingstore.core.models.api.ApiInventory; +import net.craftingstore.core.models.api.ApiPackageInformation; +import net.craftingstore.core.models.api.inventory.CraftingStoreInventory; +import net.craftingstore.core.models.api.inventory.InventoryItem; +import net.craftingstore.core.models.api.inventory.InventoryItemIcon; +import net.craftingstore.core.models.api.inventory.InventoryItemType; +import net.craftingstore.core.models.api.inventory.types.InventoryItemBuyablePackage; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutionException; + +public class BuyInventoryBuilder { + + private final CraftingStoreBukkit instance; + private final InventoryBuilder inventoryBuilder; + + public BuyInventoryBuilder(CraftingStoreBukkit instance, InventoryBuilder inventoryBuilder) { + this.instance = instance; + this.inventoryBuilder = inventoryBuilder; + } + + public Inventory build(Player p, InventoryItemBuyablePackage item, CraftingStoreInventoryHolder holder) throws CraftingStoreApiException, ExecutionException, InterruptedException { + ApiInventory buyMenu = this.instance.getCraftingStore().getApi().getGUI().get().getBuyMenu(); + CraftingStoreInventory craftingStoreInventory = new CraftingStoreInventory(buyMenu.getTitle(), buyMenu.getContent(), buyMenu.getSize()); + + String ip = p.getAddress().getAddress().getHostAddress(); + ApiPackageInformation packageInformation = this.instance.getCraftingStore().getApi().getPackageInformation( + p.getName(), + p.getUniqueId(), + ip, + item.getPackageId() + ).get(); + if (!packageInformation.isAllowedToBuy()) { + p.sendMessage(ChatColorUtil.translate(packageInformation.getMessage())); + p.closeInventory(); + return null; + } + if (packageInformation.getPrice() != item.getPrice()) { + p.sendMessage(this.instance.getPrefix() + "There was a problem with the payment data. Please try again later."); + p.closeInventory(); + return null; + } + + Map placeholders = new HashMap<>(); + placeholders.put("package_name", item.getName()); + placeholders.put("package_price_ingame", packageInformation.getPrice()); + return this.inventoryBuilder.buildInventory( + craftingStoreInventory, + new BuyMenuInventoryHolder(craftingStoreInventory, holder, item), + placeholders + ); + } + + public Inventory buildLoadInventory() { + InventoryItemIcon icon = new InventoryItemIcon("LIGHT_BLUE_STAINED_GLASS_PANE", 1, 0, null); + InventoryItem[] content = new InventoryItem[9]; + for (int i = 0; i < 9; i++) { + content[i] = new InventoryItem("Loading...", null, InventoryItemType.NULL, icon, i); + } + + CraftingStoreInventory craftingStoreInventory = new CraftingStoreInventory( + "Loading...", + content, + 9 + ); + return this.inventoryBuilder.buildInventory( + craftingStoreInventory, + new CraftingStoreInventoryHolder(craftingStoreInventory, null) + ); + } +} diff --git a/bukkit/src/main/java/net/craftingstore/bukkit/inventory/BuyMenuInventoryHolder.java b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/BuyMenuInventoryHolder.java new file mode 100644 index 0000000..436eb78 --- /dev/null +++ b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/BuyMenuInventoryHolder.java @@ -0,0 +1,22 @@ +package net.craftingstore.bukkit.inventory; + +import net.craftingstore.core.models.api.inventory.CraftingStoreInventory; +import net.craftingstore.core.models.api.inventory.types.InventoryItemBuyablePackage; + +public class BuyMenuInventoryHolder extends CraftingStoreInventoryHolder { + + private final InventoryItemBuyablePackage itemBuyablePackage; + + public BuyMenuInventoryHolder( + CraftingStoreInventory csInventory, + CraftingStoreInventoryHolder parentInventory, + InventoryItemBuyablePackage itemBuyablePackage + ) { + super(csInventory, parentInventory); + this.itemBuyablePackage = itemBuyablePackage; + } + + public InventoryItemBuyablePackage getItemBuyablePackage() { + return itemBuyablePackage; + } +} diff --git a/bukkit/src/main/java/net/craftingstore/bukkit/inventory/InventoryBuilder.java b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/InventoryBuilder.java index 2ed598d..0406d46 100644 --- a/bukkit/src/main/java/net/craftingstore/bukkit/inventory/InventoryBuilder.java +++ b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/InventoryBuilder.java @@ -5,12 +5,15 @@ import net.craftingstore.bukkit.util.ChatColorUtil; import net.craftingstore.bukkit.util.VersionUtil; import net.craftingstore.core.models.api.inventory.CraftingStoreInventory; import net.craftingstore.core.models.api.inventory.InventoryItem; +import org.apache.commons.text.StringSubstitutor; import org.bukkit.Bukkit; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import java.util.stream.Collectors; public class InventoryBuilder { @@ -24,24 +27,31 @@ public class InventoryBuilder { } public Inventory buildInventory(CraftingStoreInventory csInventory) { - return buildInventory(csInventory, null); + return buildInventory(csInventory, new CraftingStoreInventoryHolder(csInventory, null)); } - public Inventory buildInventory(CraftingStoreInventory csInventory, CraftingStoreInventoryHolder parent) { + public Inventory buildInventory(CraftingStoreInventory csInventory, CraftingStoreInventoryHolder holder) { + return buildInventory(csInventory, holder, new HashMap<>()); + } + + public Inventory buildInventory(CraftingStoreInventory csInventory, CraftingStoreInventoryHolder holder, Map placeholders) { String title = csInventory.getTitle(); if (title == null || title.isEmpty()) { title = "CraftingStore"; } - title = ChatColorUtil.translate(title); - Inventory inventory = Bukkit.createInventory(new CraftingStoreInventoryHolder(csInventory, parent), csInventory.getSize(), title); + + StringSubstitutor stringSubstitutor = new StringSubstitutor(placeholders, "{", "}"); + title = ChatColorUtil.translate(stringSubstitutor.replace(title)); + Inventory inventory = Bukkit.createInventory(holder, csInventory.getSize(), title); for (InventoryItem inventoryItem : csInventory.getContent()) { ItemStack itemStack = this.inventoryItemBuilder.getItemStack(inventoryItem.getIcon().getMaterial(), inventoryItem.getIcon().getAmount()); ItemMeta meta = itemStack.getItemMeta(); if (meta != null) { - meta.setDisplayName(ChatColorUtil.translate(inventoryItem.getName())); + meta.setDisplayName(ChatColorUtil.translate(stringSubstitutor.replace(inventoryItem.getName()))); if (inventoryItem.getDescription() != null && inventoryItem.getDescription().length != 0) { meta.setLore(Arrays.stream(inventoryItem.getDescription()) + .map(stringSubstitutor::replace) .map(ChatColorUtil::translate) .collect(Collectors.toList()) ); diff --git a/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/BackButtonHandler.java b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/BackButtonHandler.java index 1f97135..ec83be2 100644 --- a/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/BackButtonHandler.java +++ b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/BackButtonHandler.java @@ -18,7 +18,11 @@ public class BackButtonHandler implements InventoryItemHandler { + + private final CraftingStoreBukkit instance; + private final BuyInventoryBuilder buyInventoryBuilder; + + public BuyButtonHandler(CraftingStoreBukkit instance, BuyInventoryBuilder buyInventoryBuilder) { + this.instance = instance; + this.buyInventoryBuilder = buyInventoryBuilder; + } + + @Override + public void handle(Player p, InventoryItemBuyButton item, CraftingStoreInventoryHolder holder) { + if (!(holder instanceof BuyMenuInventoryHolder)) { + throw new RuntimeException("BuyButtonHandler only works in a buy menu"); + } + InventoryItemBuyablePackage itemBuyablePackage = ((BuyMenuInventoryHolder) holder).getItemBuyablePackage(); + + InventoryItemBuyButton.InventoryItemBuyButtonAction action = item.getAction(); + if (action.equals(InventoryItemBuyButton.InventoryItemBuyButtonAction.ONLINE)) { + for (String message : itemBuyablePackage.getMessages()) { + p.sendMessage(ChatColorUtil.translate(message)); + } + p.closeInventory(); + return; + } + + if (action.equals(InventoryItemBuyButton.InventoryItemBuyButtonAction.IN_GAME)) { + Inventory loading = this.buyInventoryBuilder.buildLoadInventory(); + p.openInventory(loading); + Bukkit.getScheduler().runTaskAsynchronously(this.instance, () -> { + try { + String ip = p.getAddress().getAddress().getHostAddress(); + ApiPackageInformation packageInformation = this.instance.getCraftingStore().getApi().getPackageInformation( + p.getName(), + p.getUniqueId(), + ip, + itemBuyablePackage.getPackageId() + ).get(); + if (!packageInformation.isAllowedToBuy()) { + p.sendMessage(ChatColorUtil.translate(packageInformation.getMessage())); + for (String message : itemBuyablePackage.getMessages()) { + p.sendMessage(ChatColorUtil.translate(message)); + } + closeInventory(p); + return; + } + if (packageInformation.getPrice() != itemBuyablePackage.getPrice()) { + p.sendMessage(this.instance.getPrefix() + "There was a problem with the payment data. Please try again later."); + closeInventory(p); + return; + } + if (!this.instance.getVaultHook().getEconomy().has(p, packageInformation.getPrice())) { + p.sendMessage(ChatColorUtil.translate( + this.instance.getCraftingStore().getImplementation().getConfiguration().getNotEnoughBalanceMessage() + )); + closeInventory(p); + return; + } + EconomyResponse response = this.instance.getVaultHook().getEconomy().withdrawPlayer( + p, + packageInformation.getPrice() + ); + if (!response.transactionSuccess()) { + p.sendMessage(this.instance.getPrefix() + "There was a problem with the transaction. Please try again later"); + closeInventory(p); + return; + } + + try { + boolean success = this.instance.getCraftingStore().getApi().createPayment( + p.getName(), + packageInformation.getPrice(), + new int[]{itemBuyablePackage.getPackageId()} + ).get(); + if (success) { + p.sendMessage(ChatColorUtil.translate(itemBuyablePackage.getSuccessMessage())); + closeInventory(p); + return; + } + throw new RuntimeException("Failed to create a transaction at CraftingStore. Giving money back."); + } catch (Exception exception) { + exception.printStackTrace(); + this.instance.getCraftingStore().getLogger().error(String.format( + "Unable to create a transaction for player %s with package %s and cost %s. Giving the player's money back...", + p.getName(), + itemBuyablePackage.getPackageId(), + packageInformation.getPrice() + )); + EconomyResponse rollbackResponse = this.instance.getVaultHook().getEconomy().depositPlayer( + p, + packageInformation.getPrice() + ); + if (!rollbackResponse.transactionSuccess()) { + p.sendMessage(this.instance.getPrefix() + "There was a problem with the transaction, and we were unable to give you back your money. Please contact an administrator"); + this.instance.getCraftingStore().getLogger().error(String.format( + "Unable to give the player's money back. Player: %s, cost: %s.", + p.getName(), + packageInformation.getPrice() + )); + closeInventory(p); + return; + } + + p.sendMessage(this.instance.getPrefix() + "A problem occurred while creating your payment. Your money has been given back. Please try again later"); + closeInventory(p); + } + } catch (Exception exception) { + exception.printStackTrace(); + closeInventory(p); + p.sendMessage(this.instance.getPrefix() + "A problem occurred. Please try again later."); + } + }); + } + } + + private void closeInventory(Player p) { + Bukkit.getScheduler().runTask(instance, () -> { + if (p.isOnline()) { + p.closeInventory(); + } + }); + } +} diff --git a/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/BuyablePackageHandler.java b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/BuyablePackageHandler.java new file mode 100644 index 0000000..b7af53f --- /dev/null +++ b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/BuyablePackageHandler.java @@ -0,0 +1,57 @@ +package net.craftingstore.bukkit.inventory.handlers; + +import net.craftingstore.bukkit.CraftingStoreBukkit; +import net.craftingstore.bukkit.inventory.*; +import net.craftingstore.bukkit.util.ChatColorUtil; +import net.craftingstore.core.models.api.inventory.types.InventoryItemBuyablePackage; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +public class BuyablePackageHandler implements InventoryItemHandler { + + private final CraftingStoreBukkit instance; + private final BuyInventoryBuilder buyInventoryBuilder; + + public BuyablePackageHandler(CraftingStoreBukkit instance, BuyInventoryBuilder buyInventoryBuilder) { + this.instance = instance; + this.buyInventoryBuilder = buyInventoryBuilder; + } + + @Override + public void handle(Player p, InventoryItemBuyablePackage item, CraftingStoreInventoryHolder holder) { + if (!this.instance.isHookedWithVault()) { + p.sendMessage(instance.getPrefix() + "CraftingStore is not hooked with Vault!"); + p.closeInventory(); + return; + } + + if (!this.instance.getVaultHook().getEconomy().has(p, item.getPrice())) { + p.sendMessage(ChatColorUtil.translate( + this.instance.getCraftingStore().getImplementation().getConfiguration().getNotEnoughBalanceMessage() + )); + for (String message : item.getMessages()) { + p.sendMessage(ChatColorUtil.translate(message)); + } + p.closeInventory(); + return; + } + + Inventory loading = this.buyInventoryBuilder.buildLoadInventory(); + p.openInventory(loading); + Bukkit.getScheduler().runTaskAsynchronously(this.instance, () -> { + try { + Inventory inventory = this.buyInventoryBuilder.build(p, item, holder); + if (inventory != null) { + Bukkit.getScheduler().runTask(instance, () -> { + if (p.isOnline()) { + p.openInventory(inventory); + } + }); + } + } catch (Exception exception) { + exception.printStackTrace(); + } + }); + } +} diff --git a/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/CategoryItemHandler.java b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/CategoryItemHandler.java index 6391921..16f7b33 100644 --- a/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/CategoryItemHandler.java +++ b/bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/CategoryItemHandler.java @@ -19,7 +19,7 @@ public class CategoryItemHandler implements InventoryItemHandler, InventoryItemHandler> handlers = new HashMap<>(); + private final HashMap, InventoryItemHandler> handlers = new HashMap<>(); - private CraftingStoreBukkit instance; - private InventoryBuilder inventoryBuilder; + private final CraftingStoreBukkit instance; + private final InventoryBuilder inventoryBuilder; public InventoryListener(CraftingStoreBukkit instance) { this.instance = instance; this.inventoryBuilder = new InventoryBuilder(instance); + BuyInventoryBuilder buyInventoryBuilder = new BuyInventoryBuilder(instance, this.inventoryBuilder); this.handlers.put(InventoryItemBackButton.class, new BackButtonHandler(this.inventoryBuilder)); this.handlers.put(InventoryItemCategory.class, new CategoryItemHandler(this.inventoryBuilder)); this.handlers.put(InventoryItemCloseButton.class, new CloseButtonHandler()); this.handlers.put(InventoryItemMessage.class, new MessageButtonHandler()); + this.handlers.put(InventoryItemBuyablePackage.class, new BuyablePackageHandler( + instance, + buyInventoryBuilder + )); + this.handlers.put(InventoryItemBuyButton.class, new BuyButtonHandler(instance, buyInventoryBuilder)); } @EventHandler diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index ad8e421..863a12d 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ main: net.craftingstore.bukkit.CraftingStoreBukkit version: ${craftingstore-version} authors: [Tim_kwakman, thijs_a] prefix: CraftingStore -softdepend: [PlaceholderAPI] +softdepend: [PlaceholderAPI, Vault] api-version: "1.13" commands: craftingstore: diff --git a/bungee/src/main/java/net/craftingstore/bungee/BungeePluginConfiguration.java b/bungee/src/main/java/net/craftingstore/bungee/BungeePluginConfiguration.java index ff9beaa..c93f96c 100644 --- a/bungee/src/main/java/net/craftingstore/bungee/BungeePluginConfiguration.java +++ b/bungee/src/main/java/net/craftingstore/bungee/BungeePluginConfiguration.java @@ -39,4 +39,9 @@ public class BungeePluginConfiguration implements PluginConfiguration { public int getTimeBetweenCommands() { return plugin.getConfig().getInt("time-between-commands", 200); } + + @Override + public String getNotEnoughBalanceMessage() { + return null; + } } diff --git a/core/pom.xml b/core/pom.xml index dec6b28..d03a415 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -59,5 +59,10 @@ commons-logging 1.2 + + org.apache.commons + commons-text + 1.9 + \ No newline at end of file diff --git a/core/src/main/java/net/craftingstore/core/CraftingStoreAPI.java b/core/src/main/java/net/craftingstore/core/CraftingStoreAPI.java index 1387c7b..d6efb77 100644 --- a/core/src/main/java/net/craftingstore/core/CraftingStoreAPI.java +++ b/core/src/main/java/net/craftingstore/core/CraftingStoreAPI.java @@ -1,14 +1,11 @@ package net.craftingstore.core; import net.craftingstore.core.exceptions.CraftingStoreApiException; -import net.craftingstore.core.models.api.ApiCategory; -import net.craftingstore.core.models.api.ApiPayment; -import net.craftingstore.core.models.api.ApiTopDonator; -import net.craftingstore.core.models.api.Root; -import net.craftingstore.core.models.api.inventory.CraftingStoreInventory; +import net.craftingstore.core.models.api.*; import net.craftingstore.core.models.api.misc.CraftingStoreInformation; import net.craftingstore.core.models.donation.Donation; +import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -20,7 +17,7 @@ public abstract class CraftingStoreAPI { public abstract Future getInformation() throws CraftingStoreApiException; - public abstract Future checkKey() throws CraftingStoreApiException; + public abstract Future> checkKey() throws CraftingStoreApiException; public abstract Future getDonationQueue() throws CraftingStoreApiException; @@ -28,12 +25,23 @@ public abstract class CraftingStoreAPI { public abstract Future getPayments() throws CraftingStoreApiException; - public abstract Future getCategories() throws CraftingStoreApiException; - - public abstract Future getGUI() throws CraftingStoreApiException; + public abstract Future getGUI() throws CraftingStoreApiException; public abstract Future getTopDonators() throws CraftingStoreApiException; + public abstract Future getPackageInformation( + String inGameName, + UUID uuid, + String ip, + int packageId + ) throws CraftingStoreApiException; + + public abstract Future createPayment( + String inGameName, + int price, + int[] packages + ) throws CraftingStoreApiException; + public String getToken() { return this.token; } diff --git a/core/src/main/java/net/craftingstore/core/PluginConfiguration.java b/core/src/main/java/net/craftingstore/core/PluginConfiguration.java index 987b87a..9376d3e 100644 --- a/core/src/main/java/net/craftingstore/core/PluginConfiguration.java +++ b/core/src/main/java/net/craftingstore/core/PluginConfiguration.java @@ -12,4 +12,6 @@ public interface PluginConfiguration { boolean isBuyCommandEnabled(); int getTimeBetweenCommands(); + + String getNotEnoughBalanceMessage(); } diff --git a/core/src/main/java/net/craftingstore/core/http/CraftingStoreAPIImpl.java b/core/src/main/java/net/craftingstore/core/http/CraftingStoreAPIImpl.java index 9135961..e617c1e 100644 --- a/core/src/main/java/net/craftingstore/core/http/CraftingStoreAPIImpl.java +++ b/core/src/main/java/net/craftingstore/core/http/CraftingStoreAPIImpl.java @@ -8,11 +8,13 @@ import net.craftingstore.core.exceptions.CraftingStoreApiException; import net.craftingstore.core.http.util.InformationAdapter; import net.craftingstore.core.http.util.InventoryAdapter; import net.craftingstore.core.http.util.JsonResponseHandler; +import net.craftingstore.core.http.util.JsonResponseHandlerV7; import net.craftingstore.core.models.api.*; -import net.craftingstore.core.models.api.inventory.CraftingStoreInventory; import net.craftingstore.core.models.api.inventory.InventoryItem; import net.craftingstore.core.models.api.misc.CraftingStoreInformation; import net.craftingstore.core.models.api.provider.ProviderInformation; +import net.craftingstore.core.models.api.request.PackageInformationRequest; +import net.craftingstore.core.models.api.request.PaymentCreateRequest; import net.craftingstore.core.models.donation.Donation; import net.craftingstore.core.models.donation.DonationPackage; import net.craftingstore.core.models.donation.DonationPlayer; @@ -24,6 +26,8 @@ import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; @@ -35,6 +39,7 @@ import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.UUID; import java.util.concurrent.Future; public class CraftingStoreAPIImpl extends CraftingStoreAPI { @@ -64,7 +69,7 @@ public class CraftingStoreAPIImpl extends CraftingStoreAPI { public Future getInformation() throws CraftingStoreApiException { return executor.submit(() -> { try { - HttpPost request = post("info"); + HttpPost request = post("v4/info"); List params = new ArrayList<>(); params.add(new BasicNameValuePair("version", instance.getImplementation().getConfiguration().getVersion())); params.add(new BasicNameValuePair("platform", instance.getImplementation().getConfiguration().getPlatform())); @@ -76,10 +81,10 @@ public class CraftingStoreAPIImpl extends CraftingStoreAPI { }); } - public Future checkKey() throws CraftingStoreApiException { + public Future> checkKey() throws CraftingStoreApiException { return executor.submit(() -> { try { - return httpClient.execute(get("validateToken"), new JsonResponseHandler<>(gson, Root.class)); + return httpClient.execute(get("v4/validateToken"), new JsonResponseHandler<>(gson, Root.class)); } catch (IOException e) { throw new CraftingStoreApiException("Check Key call failed", e); } @@ -89,7 +94,7 @@ public class CraftingStoreAPIImpl extends CraftingStoreAPI { public Future getDonationQueue() throws CraftingStoreApiException { return executor.submit(() -> { try { - ApiDonation[] apiDonations = httpClient.execute(get("queue"), new JsonResponseHandler<>(gson, ApiDonation[].class)); + ApiDonation[] apiDonations = httpClient.execute(get("v4/queue"), new JsonResponseHandler<>(gson, ApiDonation[].class)); return Arrays.stream(apiDonations).map(apiDonation -> { DonationPlayer player = new DonationPlayer(apiDonation.getMcName(), apiDonation.getUuid(), apiDonation.getRequireOnline()); DonationPackage donationPackage = new DonationPackage(apiDonation.getPackageName(), apiDonation.getPackagePriceCents()); @@ -111,7 +116,7 @@ public class CraftingStoreAPIImpl extends CraftingStoreAPI { public void completeDonations(int[] ids) throws CraftingStoreApiException { executor.submit(() -> { try { - HttpPost request = post("queue/markComplete"); + HttpPost request = post("v4/queue/markComplete"); List params = new ArrayList<>(); params.add(new BasicNameValuePair("removeIds", gson.toJson(ids))); request.setEntity(new UrlEncodedFormEntity(params)); @@ -127,28 +132,17 @@ public class CraftingStoreAPIImpl extends CraftingStoreAPI { public Future getPayments() throws CraftingStoreApiException { return executor.submit(() -> { try { - return httpClient.execute(get("buyers/recent"), new JsonResponseHandler<>(gson, ApiPayment[].class)); + return httpClient.execute(get("v4/buyers/recent"), new JsonResponseHandler<>(gson, ApiPayment[].class)); } catch (IOException e) { throw new CraftingStoreApiException("Payments call failed", e); } }); } - @Deprecated - public Future getCategories() throws CraftingStoreApiException { + public Future getGUI() throws CraftingStoreApiException { return executor.submit(() -> { try { - return httpClient.execute(get("plugin/inventory"), new JsonResponseHandler<>(gson, ApiCategory[].class)); - } catch (IOException e) { - throw new CraftingStoreApiException("Categories call failed", e); - } - }); - } - - public Future getGUI() throws CraftingStoreApiException { - return executor.submit(() -> { - try { - return httpClient.execute(get("plugin/gui"), new JsonResponseHandler<>(gson, CraftingStoreInventory.class)); + return httpClient.execute(get("v4/plugin/gui"), new JsonResponseHandler<>(gson, ApiInventory.class)); } catch (IOException e) { throw new CraftingStoreApiException("Inventory call failed", e); } @@ -158,13 +152,65 @@ public class CraftingStoreAPIImpl extends CraftingStoreAPI { public Future getTopDonators() throws CraftingStoreApiException { return executor.submit(() -> { try { - return httpClient.execute(get("buyers/top"), new JsonResponseHandler<>(gson, ApiTopDonator[].class)); + return httpClient.execute(get("v4/buyers/top"), new JsonResponseHandler<>(gson, ApiTopDonator[].class)); } catch (IOException e) { throw new CraftingStoreApiException("Top Donators call failed", e); } }); } + public Future getPackageInformation( + String inGameName, + UUID uuid, + String ip, + int packageId + ) throws CraftingStoreApiException { + return executor.submit(() -> { + try { + HttpPost request = post("v7/package/ingame/information"); + PackageInformationRequest packageInformationRequest = new PackageInformationRequest( + inGameName, + uuid.toString(), + ip, + packageId + ); + request.setEntity(new StringEntity(gson.toJson(packageInformationRequest), ContentType.APPLICATION_JSON)); + return httpClient.execute(request, new JsonResponseHandlerV7<>(gson, ApiPackageInformation.class)); + } catch (IOException e) { + throw new CraftingStoreApiException("Get package information call failed", e); + } + }); + } + + public Future createPayment( + String inGameName, + int price, + int[] packages + ) throws CraftingStoreApiException { + return executor.submit(() -> { + try { + HttpPost request = post("v7/payments"); + PaymentCreateRequest paymentCreateRequest = new PaymentCreateRequest( + inGameName, + "Paid with in-game currency", + "ingame", + price, + null, + true, + packages + ); + request.setEntity(new StringEntity(gson.toJson(paymentCreateRequest), ContentType.APPLICATION_JSON)); + HttpResponse response = httpClient.execute(request); + EntityUtils.consumeQuietly(response.getEntity()); + return true; + } catch (IOException e) { + instance.getLogger().error("Payment create call failed"); + e.printStackTrace(); + return false; + } + }); + } + private HttpGet get(String endpoint) { HttpGet request = new HttpGet(BASE_URL + endpoint); request.setConfig(RequestConfig.custom() diff --git a/core/src/main/java/net/craftingstore/core/http/CraftingStoreCachedAPI.java b/core/src/main/java/net/craftingstore/core/http/CraftingStoreCachedAPI.java index 0ef2229..da93bdf 100644 --- a/core/src/main/java/net/craftingstore/core/http/CraftingStoreCachedAPI.java +++ b/core/src/main/java/net/craftingstore/core/http/CraftingStoreCachedAPI.java @@ -2,9 +2,9 @@ package net.craftingstore.core.http; import net.craftingstore.core.CraftingStore; import net.craftingstore.core.exceptions.CraftingStoreApiException; +import net.craftingstore.core.models.api.ApiInventory; import net.craftingstore.core.models.api.ApiPayment; import net.craftingstore.core.models.api.ApiTopDonator; -import net.craftingstore.core.models.api.inventory.CraftingStoreInventory; import java.util.HashMap; import java.util.concurrent.ExecutionException; @@ -12,20 +12,20 @@ import java.util.concurrent.Future; public class CraftingStoreCachedAPI extends CraftingStoreAPIImpl { - private HashMap cache = new HashMap<>(); + private final HashMap cache = new HashMap<>(); public CraftingStoreCachedAPI(CraftingStore instance) { super(instance); } @Override - public Future getGUI() throws CraftingStoreApiException { + public Future getGUI() throws CraftingStoreApiException { return executor.submit(() -> { String key = "plugin/inventory"; if (!cache.containsKey(key)) { cache.put(key, super.getGUI().get()); } - return (CraftingStoreInventory) cache.get(key); + return (ApiInventory) cache.get(key); }); } diff --git a/core/src/main/java/net/craftingstore/core/http/util/InventoryAdapter.java b/core/src/main/java/net/craftingstore/core/http/util/InventoryAdapter.java index 8de479d..2460201 100644 --- a/core/src/main/java/net/craftingstore/core/http/util/InventoryAdapter.java +++ b/core/src/main/java/net/craftingstore/core/http/util/InventoryAdapter.java @@ -13,7 +13,7 @@ public class InventoryAdapter implements JsonDeserializer { JsonPrimitive prim = (JsonPrimitive) jsonObject.get("type"); InventoryItemType itemType = InventoryItemType.valueOf(prim.getAsString()); - Class klass = itemType.getActualClass(); + Class klass = itemType.getActualClass(); return context.deserialize(jsonObject, klass); } } diff --git a/core/src/main/java/net/craftingstore/core/http/util/JsonResponseHandlerV7.java b/core/src/main/java/net/craftingstore/core/http/util/JsonResponseHandlerV7.java new file mode 100644 index 0000000..19b0f36 --- /dev/null +++ b/core/src/main/java/net/craftingstore/core/http/util/JsonResponseHandlerV7.java @@ -0,0 +1,45 @@ +package net.craftingstore.core.http.util; + +import com.google.gson.Gson; +import net.craftingstore.core.models.api.RootV7; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.StatusLine; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpResponseException; +import org.apache.http.client.ResponseHandler; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; + +public class JsonResponseHandlerV7 implements ResponseHandler { + + private Gson gson; + private Class aClass; + + public JsonResponseHandlerV7(Gson gson, Class aClass) { + this.gson = gson; + this.aClass = aClass; + } + + public T handleResponse(HttpResponse response) throws IOException { + StatusLine statusLine = response.getStatusLine(); + HttpEntity entity = response.getEntity(); + + if (statusLine.getStatusCode() >= 300) { + throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); + } + + if (entity == null) { + throw new ClientProtocolException("Response contains no content"); + } + + String value = EntityUtils.toString(entity); + + if (aClass == RootV7.class) { + return gson.fromJson(value, aClass); + } + RootV7 result = gson.fromJson(value, new GenericOf<>(RootV7.class, aClass)); + return result.getData(); + } +} diff --git a/core/src/main/java/net/craftingstore/core/models/api/ApiInventory.java b/core/src/main/java/net/craftingstore/core/models/api/ApiInventory.java new file mode 100644 index 0000000..fff3c0f --- /dev/null +++ b/core/src/main/java/net/craftingstore/core/models/api/ApiInventory.java @@ -0,0 +1,26 @@ +package net.craftingstore.core.models.api; + +import net.craftingstore.core.models.api.inventory.InventoryItem; + +public class ApiInventory { + private String title; + private InventoryItem[] content; + private int size; + private ApiInventory buyMenu; + + public String getTitle() { + return title; + } + + public InventoryItem[] getContent() { + return content; + } + + public int getSize() { + return size; + } + + public ApiInventory getBuyMenu() { + return buyMenu; + } +} diff --git a/core/src/main/java/net/craftingstore/core/models/api/ApiPackageInformation.java b/core/src/main/java/net/craftingstore/core/models/api/ApiPackageInformation.java new file mode 100644 index 0000000..a8e512b --- /dev/null +++ b/core/src/main/java/net/craftingstore/core/models/api/ApiPackageInformation.java @@ -0,0 +1,19 @@ +package net.craftingstore.core.models.api; + +public class ApiPackageInformation { + private boolean mayBuy; + private int price; + private String message; + + public boolean isAllowedToBuy() { + return mayBuy; + } + + public int getPrice() { + return price; + } + + public String getMessage() { + return message; + } +} diff --git a/core/src/main/java/net/craftingstore/core/models/api/RootV7.java b/core/src/main/java/net/craftingstore/core/models/api/RootV7.java new file mode 100644 index 0000000..2826961 --- /dev/null +++ b/core/src/main/java/net/craftingstore/core/models/api/RootV7.java @@ -0,0 +1,17 @@ +package net.craftingstore.core.models.api; + +public class RootV7 { + + private T data; + private boolean success; + private String message; + + public T getData() { + return data; + } + + public boolean isSuccess() { + return success; + } + +} diff --git a/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItem.java b/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItem.java index 9579e9c..a8de1c0 100644 --- a/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItem.java +++ b/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItem.java @@ -1,11 +1,23 @@ package net.craftingstore.core.models.api.inventory; public class InventoryItem { - private String name; - private String[] description; - private InventoryItemType type; - private InventoryItemIcon icon; - private int index; + protected String name; + protected String[] description; + protected InventoryItemType type; + protected InventoryItemIcon icon; + protected int index; + + public InventoryItem() { + + } + + public InventoryItem(String name, String[] description, InventoryItemType type, InventoryItemIcon icon, int index) { + this.name = name; + this.description = description; + this.type = type; + this.icon = icon; + this.index = index; + } public String getName() { return name; diff --git a/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItemIcon.java b/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItemIcon.java index 6b75e09..fb4e3a5 100644 --- a/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItemIcon.java +++ b/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItemIcon.java @@ -6,6 +6,16 @@ public class InventoryItemIcon { private int customModelData; private InventoryItemEnhancement[] enhancements; + public InventoryItemIcon() { + } + + public InventoryItemIcon(String material, Integer amount, int customModelData, InventoryItemEnhancement[] enhancements) { + this.material = material; + this.amount = amount; + this.customModelData = customModelData; + this.enhancements = enhancements; + } + public String getMaterial() { return material; } diff --git a/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItemType.java b/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItemType.java index 6255476..3e2391b 100644 --- a/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItemType.java +++ b/core/src/main/java/net/craftingstore/core/models/api/inventory/InventoryItemType.java @@ -1,23 +1,24 @@ package net.craftingstore.core.models.api.inventory; -import net.craftingstore.core.models.api.inventory.types.InventoryItemBackButton; -import net.craftingstore.core.models.api.inventory.types.InventoryItemCategory; -import net.craftingstore.core.models.api.inventory.types.InventoryItemCloseButton; -import net.craftingstore.core.models.api.inventory.types.InventoryItemMessage; +import net.craftingstore.core.models.api.inventory.types.*; public enum InventoryItemType { + NULL(null), CATEGORY(InventoryItemCategory.class), MESSAGE(InventoryItemMessage.class), BACK(InventoryItemBackButton.class), - CLOSE(InventoryItemCloseButton.class); + CLOSE(InventoryItemCloseButton.class), + BUYABLE_PACKAGE(InventoryItemBuyablePackage.class), + BUY_DETAILS(InventoryItemBuyDetails.class), + BUY_BUTTON(InventoryItemBuyButton.class); - private Class c; + private final Class c; - InventoryItemType(Class c) { + InventoryItemType(Class c) { this.c = c; } - public Class getActualClass() { + public Class getActualClass() { return c; } } diff --git a/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBackButton.java b/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBackButton.java index 30f5d85..7dd4317 100644 --- a/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBackButton.java +++ b/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBackButton.java @@ -1,6 +1,19 @@ package net.craftingstore.core.models.api.inventory.types; import net.craftingstore.core.models.api.inventory.InventoryItem; +import net.craftingstore.core.models.api.inventory.InventoryItemIcon; +import net.craftingstore.core.models.api.inventory.InventoryItemType; public class InventoryItemBackButton extends InventoryItem { + + public InventoryItemBackButton() {} + + public InventoryItemBackButton( + String name, + String[] description, + InventoryItemIcon icon, + int index + ) { + super(name, description, InventoryItemType.BACK, icon, index); + } } diff --git a/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyButton.java b/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyButton.java new file mode 100644 index 0000000..7ed5f27 --- /dev/null +++ b/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyButton.java @@ -0,0 +1,29 @@ +package net.craftingstore.core.models.api.inventory.types; + +import net.craftingstore.core.models.api.inventory.InventoryItem; +import net.craftingstore.core.models.api.inventory.InventoryItemIcon; +import net.craftingstore.core.models.api.inventory.InventoryItemType; + +public class InventoryItemBuyButton extends InventoryItem { + private final InventoryItemBuyButtonAction action; + + public InventoryItemBuyButton( + String name, + String[] description, + InventoryItemIcon icon, + int index, + InventoryItemBuyButtonAction action + ) { + super(name, description, InventoryItemType.BUY_BUTTON, icon, index); + this.action = action; + } + + public InventoryItemBuyButtonAction getAction() { + return action; + } + + public enum InventoryItemBuyButtonAction { + IN_GAME, + ONLINE, + } +} diff --git a/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyDetails.java b/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyDetails.java new file mode 100644 index 0000000..74ad78f --- /dev/null +++ b/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyDetails.java @@ -0,0 +1,19 @@ +package net.craftingstore.core.models.api.inventory.types; + +import net.craftingstore.core.models.api.inventory.InventoryItem; +import net.craftingstore.core.models.api.inventory.InventoryItemIcon; +import net.craftingstore.core.models.api.inventory.InventoryItemType; + +public class InventoryItemBuyDetails extends InventoryItem { + + public InventoryItemBuyDetails() {} + + public InventoryItemBuyDetails( + String name, + String[] description, + InventoryItemIcon icon, + int index + ) { + super(name, description, InventoryItemType.BUY_DETAILS, icon, index); + } +} diff --git a/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyablePackage.java b/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyablePackage.java new file mode 100644 index 0000000..539b60e --- /dev/null +++ b/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemBuyablePackage.java @@ -0,0 +1,30 @@ +package net.craftingstore.core.models.api.inventory.types; + +import net.craftingstore.core.models.api.inventory.InventoryItem; + +public class InventoryItemBuyablePackage extends InventoryItem { + private int price; + private int packageId; + private String[] messages; + private String successMessage; + + public InventoryItemBuyablePackage() { + + } + + public int getPrice() { + return price; + } + + public int getPackageId() { + return packageId; + } + + public String[] getMessages() { + return messages; + } + + public String getSuccessMessage() { + return successMessage; + } +} diff --git a/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemMessage.java b/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemMessage.java index 6b495a1..9ce250c 100644 --- a/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemMessage.java +++ b/core/src/main/java/net/craftingstore/core/models/api/inventory/types/InventoryItemMessage.java @@ -1,11 +1,29 @@ package net.craftingstore.core.models.api.inventory.types; import net.craftingstore.core.models.api.inventory.InventoryItem; +import net.craftingstore.core.models.api.inventory.InventoryItemIcon; +import net.craftingstore.core.models.api.inventory.InventoryItemType; public class InventoryItemMessage extends InventoryItem { private String[] messages; private boolean close; + public InventoryItemMessage() { + } + + public InventoryItemMessage( + String name, + String[] description, + InventoryItemIcon icon, + int index, + String[] messages, + boolean close + ) { + super(name, description, InventoryItemType.MESSAGE, icon, index); + this.messages = messages; + this.close = close; + } + public String[] getMessages() { return messages; } diff --git a/core/src/main/java/net/craftingstore/core/models/api/request/PackageInformationRequest.java b/core/src/main/java/net/craftingstore/core/models/api/request/PackageInformationRequest.java new file mode 100644 index 0000000..981136d --- /dev/null +++ b/core/src/main/java/net/craftingstore/core/models/api/request/PackageInformationRequest.java @@ -0,0 +1,17 @@ +package net.craftingstore.core.models.api.request; + +import java.io.Serializable; + +public class PackageInformationRequest implements Serializable { + String inGameName; + String uuid; + String ip; + int packageId; + + public PackageInformationRequest(String inGameName, String uuid, String ip, int packageId) { + this.inGameName = inGameName; + this.uuid = uuid; + this.ip = ip; + this.packageId = packageId; + } +} diff --git a/core/src/main/java/net/craftingstore/core/models/api/request/PaymentCreateRequest.java b/core/src/main/java/net/craftingstore/core/models/api/request/PaymentCreateRequest.java new file mode 100644 index 0000000..ae7fa65 --- /dev/null +++ b/core/src/main/java/net/craftingstore/core/models/api/request/PaymentCreateRequest.java @@ -0,0 +1,29 @@ +package net.craftingstore.core.models.api.request; + +public class PaymentCreateRequest { + String inGameName; + String notes; + String gateway; + int inGamePricePaid; + Integer pricePaid; + boolean executeCommands; + int[] packages; + + public PaymentCreateRequest( + String inGameName, + String notes, + String gateway, + int inGamePricePaid, + Integer pricePaid, + boolean executeCommands, + int[] packages + ) { + this.inGameName = inGameName; + this.notes = notes; + this.gateway = gateway; + this.inGamePricePaid = inGamePricePaid; + this.pricePaid = pricePaid; + this.executeCommands = executeCommands; + this.packages = packages; + } +} diff --git a/core/src/main/java/net/craftingstore/core/provider/SocketProvider.java b/core/src/main/java/net/craftingstore/core/provider/SocketProvider.java index f7a2f55..e3d2e4e 100644 --- a/core/src/main/java/net/craftingstore/core/provider/SocketProvider.java +++ b/core/src/main/java/net/craftingstore/core/provider/SocketProvider.java @@ -36,7 +36,7 @@ public class SocketProvider extends CraftingStoreProvider { } } // Wait some time on SocketIO - Thread.sleep(150); + Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/pom.xml b/pom.xml index 55b8719..d311777 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ ${craftingstore-version}-RELEASE - 2.7.0 + 2.8.0 diff --git a/sponge/src/main/java/net/craftingstore/sponge/SpongePluginConfiguration.java b/sponge/src/main/java/net/craftingstore/sponge/SpongePluginConfiguration.java index db71c59..40ee579 100644 --- a/sponge/src/main/java/net/craftingstore/sponge/SpongePluginConfiguration.java +++ b/sponge/src/main/java/net/craftingstore/sponge/SpongePluginConfiguration.java @@ -48,4 +48,9 @@ public class SpongePluginConfiguration implements PluginConfiguration { public int getTimeBetweenCommands() { return craftingStoreSponge.getConfigWrapper().getConfig().getNode("time-between-commands").getInt(200); } + + @Override + public String getNotEnoughBalanceMessage() { + return null; + } } diff --git a/sponge/src/main/java/net/craftingstore/sponge/commands/BuyCommand.java b/sponge/src/main/java/net/craftingstore/sponge/commands/BuyCommand.java index c876c19..26455e2 100644 --- a/sponge/src/main/java/net/craftingstore/sponge/commands/BuyCommand.java +++ b/sponge/src/main/java/net/craftingstore/sponge/commands/BuyCommand.java @@ -3,6 +3,7 @@ package net.craftingstore.sponge.commands; import com.google.inject.Inject; import net.craftingstore.core.CraftingStore; import net.craftingstore.core.exceptions.CraftingStoreApiException; +import net.craftingstore.core.models.api.ApiInventory; import net.craftingstore.core.models.api.inventory.CraftingStoreInventory; import net.craftingstore.sponge.CraftingStoreSponge; import net.craftingstore.sponge.inventory.InventoryBuilder; @@ -43,8 +44,9 @@ public class BuyCommand implements CommandExecutor { game.getScheduler().createTaskBuilder() .execute(() -> { try { - CraftingStoreInventory gui = craftingStore.getApi().getGUI().get(); - Inventory inventory = this.inventoryBuilder.buildInventory(gui); + ApiInventory gui = craftingStore.getApi().getGUI().get(); + CraftingStoreInventory craftingStoreInventory = new CraftingStoreInventory(gui.getTitle(), gui.getContent(), gui.getSize()); + Inventory inventory = this.inventoryBuilder.buildInventory(craftingStoreInventory); game.getScheduler().createTaskBuilder() .execute(() -> { if (p.isOnline()) { diff --git a/velocity/src/main/java/net/craftingstore/velocity/VelocityPluginConfiguration.java b/velocity/src/main/java/net/craftingstore/velocity/VelocityPluginConfiguration.java index 6338cbb..ab85ceb 100644 --- a/velocity/src/main/java/net/craftingstore/velocity/VelocityPluginConfiguration.java +++ b/velocity/src/main/java/net/craftingstore/velocity/VelocityPluginConfiguration.java @@ -53,4 +53,9 @@ public class VelocityPluginConfiguration implements PluginConfiguration { } throw new RuntimeException("Invalid integer value in time-between-commands"); } + + @Override + public String getNotEnoughBalanceMessage() { + return null; + } }