Implement order command.

Implement Vault support.
This commit is contained in:
Jesse Prescott 2018-06-06 02:23:50 +01:00
parent f92c0215f0
commit c3ba5aa50b
9 changed files with 874 additions and 22 deletions

10
pom.xml
View File

@ -24,6 +24,10 @@
<id>bstats-repo</id>
<url>http://repo.bstats.org/content/repositories/releases/</url>
</repository>
<repository>
<id>vault-repo</id>
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
</repository>
</repositories>
<dependencies>
@ -55,6 +59,12 @@
<artifactId>Multiverse-Core</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,58 @@
/*
* ItemCase 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 <http://www.gnu.org/licenses/gpl.html>.
*/
package com.gmail.bleedobsidian.itemcase;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
/**
* Static utility helper methods for inventories.
*
* @author Jesse Prescott (BleedObsidian).
*/
public final class InventoryUtils {
/**
* Count the number of given ItemStacks exist in the given Inventory.
*
* @param inventory Inventory.
* @param itemstack ItemStack to count.
* @return Count.
*/
public static int count(Inventory inventory, ItemStack itemstack) {
// Define stock.
int stock = 0;
// For every itemstack.
for(ItemStack content : inventory.getContents()) {
// If content is null, skip.
if(content == null)
continue;
// If itemstacks match.
if(content.isSimilar(itemstack)) {
// Add amount to stock.
stock += content.getAmount();
}
}
// Return stock.
return stock;
}
}

View File

@ -20,6 +20,8 @@ import com.gmail.bleedobsidian.itemcase.configurations.ConfigFile;
import com.gmail.bleedobsidian.itemcase.managers.ItemcaseManager;
import com.gmail.bleedobsidian.itemcase.managers.OrderManager;
import java.io.IOException;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
/**
@ -70,6 +72,16 @@ public final class ItemCaseCore extends JavaPlugin {
* OrderManager.
*/
private final OrderManager orderManager = new OrderManager();
/**
* If the server has Vault.
*/
private boolean hasVault;
/**
* The economy provider if there is one.
*/
private Economy economyProvider;
@Override
public void onEnable() {
@ -126,6 +138,9 @@ public final class ItemCaseCore extends JavaPlugin {
// Set command executor.
this.getCommand("itemcase").setExecutor(new CommandHandler());
// Attempt to load Vault.
this.loadVault();
// Set version placeholder and log.
this.translator.setPlaceholder("%VERSION%",
this.getDescription().getVersion());
@ -142,6 +157,42 @@ public final class ItemCaseCore extends JavaPlugin {
this.consoleLogger.info("console.info.unloaded");
}
/**
* Attempt to load Vault.
*/
private void loadVault() {
// Check if this server has Vault installed.
if(getServer().getPluginManager().getPlugin("Vault") == null) {
// Set false.
this.hasVault = false;
// Exit.
return;
}
// Get server provider of economy class.
RegisteredServiceProvider<Economy> rsp =
getServer().getServicesManager().getRegistration(Economy.class);
// If could not find economy service provider.
if(rsp == null) {
// Set false.
this.hasVault = false;
// Exit.
return;
}
// Set economy provider.
this.economyProvider = rsp.getProvider();
// Set true.
this.hasVault = true;
}
/**
* @return Main ItemCase configuration file.
@ -191,4 +242,18 @@ public final class ItemCaseCore extends JavaPlugin {
public OrderManager getOrderManager() {
return this.orderManager;
}
/**
* @return If vault is setup on this server.
*/
public boolean hasVault() {
return this.hasVault;
}
/**
* @return EconomyProvider.
*/
public Economy getEconomyProvider() {
return this.economyProvider;
}
}

View File

@ -19,6 +19,7 @@ import com.gmail.bleedobsidian.itemcase.managers.ItemcaseManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
@ -260,6 +261,82 @@ public final class Itemcase {
return displayNameParts[0].equals("com.gmail.bleedobsidian.itemcase");
}
/**
* Take stock from storage.
*
* @param amount Amount to take.
*/
public void takeStock(int amount) {
// If storage is infinite.
if(this.storageType == StorageType.INFINITE) {
// Exit.
return;
}
// Get items.
ItemStack items = this.itemStack.clone();
items.setAmount(amount);
// Remove from storage.
this.storage.removeItem(items);
// Save.
ItemCaseCore.instance.getItemcaseManager().saveItemcases(this);
}
/**
* Add stock to storage.
*
* @param amount Amount to add.
*/
public void addStock(int amount) {
// If storage is infinite.
if(this.storageType == StorageType.INFINITE) {
// Exit.
return;
}
// Get items.
ItemStack items = this.itemStack.clone();
items.setAmount(amount);
// Add to storage.
this.storage.addItem(items);
// Save.
ItemCaseCore.instance.getItemcaseManager().saveItemcases(this);
}
/**
* @param amount The amount of items needed.
* @return If the itemcase has enough in stock.
*/
public boolean hasEnough(int amount) {
// If storage is infinite.
if(this.storageType == StorageType.INFINITE) {
// Return true.
return true;
}
// Return if storage contains enough.
return this.storage.containsAtLeast(this.itemStack, amount);
}
/**
* @return The amount of stock this itemcase has.
*/
public int getStockLevel() {
// Return count.
return InventoryUtils.count(this.storage, this.itemStack);
}
/**
* @return The ItemStack that this itemcase is showing.
*/
@ -319,6 +396,15 @@ public final class Itemcase {
*/
public void setStorageType(StorageType storageType) {
// If toggling from finite to infinite.
if(this.storageType == StorageType.FINITE &&
storageType == StorageType.INFINITE) {
// Set storage.
this.storage =
Bukkit.createInventory(null, 54, Itemcase.INVENTORY_NAME);
}
// Set storage type.
this.storageType= storageType;
}

View File

@ -58,6 +58,16 @@ public final class ModifyCommand implements Command {
return;
}
// If incorrect number of args.
if(args.length != 3) {
// Show help.
this.showHelp(player, label);
// Exit.
return;
}
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();

View File

@ -20,10 +20,14 @@ import com.gmail.bleedobsidian.itemcase.Itemcase;
import com.gmail.bleedobsidian.itemcase.Itemcase.StorageType;
import com.gmail.bleedobsidian.itemcase.Itemcase.Type;
import com.gmail.bleedobsidian.itemcase.LanguageTranslator;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import com.gmail.bleedobsidian.itemcase.managers.OrderManager.ItemcaseOrder;
import net.md_5.bungee.api.ChatColor;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/**
* A command handler for the 'order' command.
@ -60,29 +64,56 @@ public final class OrderCommand implements Command {
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
// If not enough arguments.
if(args.length < 2) {
// Show help.
this.showHelp(player, label);
// Exit.
return;
}
// Check if player has active order.
if(!ItemCaseCore.instance.getOrderManager().hasOrder(player)) {
// Send message
chatLogger.message(player, "command.order.no-order");
// Exit.
return;
}
// Get sub command.
String subCommand = args[1];
// Switch sub command.
switch(subCommand) {
case "buy":
this.executeBuy(player);
break;
case "sell":
this.executeSell(player);
break;
case "amount":
this.executeAmount(player, label, args);
break;
default:
this.showHelp(player, label);
return;
}
// Exit.
return;
}
/**
* @return If the command sender is asking for help about this command.
* Show help to given player.
*
* @param player Player.
* @param label Label used.
*/
public boolean isAskingForHelp(Player player, String label, String[] args) {
// If args length equals 2.
if(args.length != 2) {
// False.
return false;
}
// Get argument.
String argument = args[1];
// If not equal to help.
if(!argument.equalsIgnoreCase("help")) {
return false;
}
public void showHelp(Player player, String label) {
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
@ -107,8 +138,512 @@ public final class OrderCommand implements Command {
// Show specific help.
chatLogger.message(player, "command.order.help-sell");
}
/**
* @return If the command sender is asking for help about this command.
*/
public boolean isAskingForHelp(Player player, String label, String[] args) {
// If args length equals 2.
if(args.length != 2) {
// False.
return false;
}
// Get argument.
String argument = args[1];
// If not equal to help.
if(!argument.equalsIgnoreCase("help")) {
return false;
}
// Show help.
this.showHelp(player, label);
// Return.
return true;
}
/**
* Execute buy command.
*
* @param player Player.
*/
private void executeBuy(Player player) {
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
// Get translator.
LanguageTranslator translator = ItemCaseCore.instance.getTranslator();
// Check if player has permission.
if(!player.hasPermission("itemcase.buy")) {
// Send message.
chatLogger.message(player, "command.permission");
// Exit.
return;
}
// Get order.
ItemcaseOrder order =
ItemCaseCore.instance.getOrderManager().getOrder(player);
// Get itemcase.
Itemcase itemcase = order.getItemcase();
// If itemcase can not be bought from.
if(itemcase.getType() == Type.SHOP_SELL) {
// Send message.
chatLogger.message(player, "command.order.no-buy");
// Exit.
return;
}
// Check if server has vault if items are not free.
if(!ItemCaseCore.instance.hasVault() &&
order.getItemcase().getBuyPrice() > 0) {
// Show message.
chatLogger.message(player, "command.order.no-vault");
// Exit.
return;
}
// Get itemstack being bought.
ItemStack itemstack = itemcase.getItemStack().clone();
// If itemcase does not have enough items.
if(!itemcase.hasEnough(order.getAmount())) {
// If stock is 0.
if(itemcase.getStockLevel() == 0) {
// Send message.
chatLogger.message(player, "command.order.no-stock");
// Exit.
return;
}
// Set amount placeholder.
translator.setPlaceholder("%AMOUNT%",
"" + ChatColor.GOLD + itemcase.getStockLevel() +
ChatColor.RESET);
// Send message.
chatLogger.message(player, "command.order.not-enough-stock");
// Exit.
return;
}
// Calculate total price.
double total = order.getItemcase().getBuyPrice() * order.getAmount();
// Get economy provider.
Economy economyProvider = ItemCaseCore.instance.getEconomyProvider();
// If total is greator than 0, vault must be used.
if(total == 0) {
// Give player items.
itemstack.setAmount(order.getAmount());
player.getInventory().addItem(itemstack);
// Remove items from storage.
itemcase.takeStock(order.getAmount());
// Exit.
return;
}
// Format money.
String money = ChatColor.GOLD + economyProvider.format(total);
// If name exists.
if(!economyProvider.currencyNameSingular().equals("")) {
// If greator than 1.
if(total > 1) {
// Plural name.
money += " " + economyProvider.currencyNamePlural();
} else {
// Singular name.
money += " " + economyProvider.currencyNameSingular();
}
}
// Reset color.
money += ChatColor.RESET;
// Set placeholder.
translator.setPlaceholder("%MONEY%", money);
// If player does not have enough money.
if(economyProvider.getBalance(player,
player.getWorld().getName()) < total) {
// Send message.
chatLogger.message(player, "command.order.not-enough-money");
// Exit.
return;
}
// Create blank response.
EconomyResponse response1 =
new EconomyResponse(0, 0, ResponseType.SUCCESS, null);
// If itemcase is not infinite.
if(itemcase.getStorageType() == StorageType.FINITE) {
// Deposit money to owner.
response1 = economyProvider.depositPlayer(
order.getItemcase().getOwner(),
player.getWorld().getName(), total);
}
// Withdraw money from player.
EconomyResponse response2 = economyProvider.withdrawPlayer(
player, player.getWorld().getName(), total);
// If successful.
if(response1.transactionSuccess() && response2.transactionSuccess()) {
// Give player items.
itemstack.setAmount(order.getAmount());
player.getInventory().addItem(itemstack);
// Remove items from storage.
itemcase.takeStock(order.getAmount());
// Mark order as completed.
ItemCaseCore.instance.getOrderManager().completeOrder(player);
// Send message.
chatLogger.message(player, "command.order.withdrew");
// If owner is online and itemcase is not infinite.
if(itemcase.getStorageType() == StorageType.FINITE &&
order.getItemcase().getOwner().isOnline()) {
// Get owner.
Player owner = order.getItemcase().getOwner().getPlayer();
// Set placeholder.
translator.setPlaceholder("%PLAYER%", player.getDisplayName());
// If item has custom display name.
if(itemcase.getItemStack().getItemMeta().hasDisplayName()) {
// Set placeholder.
translator.setPlaceholder("%ITEM_NAME%",
ChatColor.GOLD +
itemcase.getItemStack().getItemMeta()
.getDisplayName() + ChatColor.RESET);
} else {
// Set placeholder.
translator.setPlaceholder("%ITEM_NAME%",
ChatColor.GOLD +
itemcase.getItemStack().getType().name()
+ ChatColor.RESET);
}
// Set placeholder.
translator.setPlaceholder("%AMOUNT%", "" + ChatColor.GOLD +
order.getAmount() + ChatColor.RESET);
// Send message.
chatLogger.message(owner, "command.order.bought-from");
}
} else {
// Show message.
chatLogger.message(player, "command.order.failed");
}
}
/**
* Execute sell command.
*
* @param player Player.
* @param label Label.
* @param args Arguments.
*/
private void executeSell(Player player) {
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
// Get translator.
LanguageTranslator translator = ItemCaseCore.instance.getTranslator();
// Check if player has permission.
if(!player.hasPermission("itemcase.sell")) {
// Send message.
chatLogger.message(player, "command.permission");
// Exit.
return;
}
// Get order.
ItemcaseOrder order =
ItemCaseCore.instance.getOrderManager().getOrder(player);
// Get itemcase.
Itemcase itemcase = order.getItemcase();
// If itemcase can not sell.
if(itemcase.getType() == Type.SHOP_BUY) {
// Send message.
chatLogger.message(player, "command.order.no-sell");
// Exit.
return;
}
// Check if server has no vault and players get no money for items.
if(!ItemCaseCore.instance.hasVault() &&
order.getItemcase().getSellPrice() > 0) {
// Show message.
chatLogger.message(player, "command.order.no-vault");
// Exit.
return;
}
// Get itemstack being bought.
ItemStack itemstack = itemcase.getItemStack().clone();
// If player does not have enough items.
if(!player.getInventory().containsAtLeast(itemstack,
order.getAmount())) {
// Send message.
chatLogger.message(player, "command.order.not-enough-items");
// Exit.
return;
}
// Calculate total price.
double total = order.getItemcase().getSellPrice() * order.getAmount();
// Get economy provider.
Economy economyProvider = ItemCaseCore.instance.getEconomyProvider();
// If total is greator than 0, vault must be used.
if(total == 0) {
// Remove items from player.
itemstack.setAmount(order.getAmount());
player.getInventory().removeItem(itemstack);
// Add stock to storage.
itemcase.addStock(order.getAmount());
// Exit.
return;
}
// Format money.
String money = ChatColor.GOLD + economyProvider.format(total);
// If name exists.
if(!economyProvider.currencyNameSingular().equals("")) {
// If greator than 1.
if(total > 1) {
// Plural name.
money += " " + economyProvider.currencyNamePlural();
} else {
// Singular name.
money += " " + economyProvider.currencyNameSingular();
}
}
// Reset color.
money += ChatColor.RESET;
// Set placeholder.
translator.setPlaceholder("%MONEY%", money);
// If owner does not have enough money and storage is finite.
if(order.getItemcase().getStorageType() == StorageType.FINITE &&
(economyProvider.getBalance(order.getItemcase().getOwner(),
player.getWorld().getName()) < total)) {
// Send message.
chatLogger.message(player, "command.order.owner-not-enough-money");
// Exit.
return;
}
// Create blank response.
EconomyResponse response1 =
new EconomyResponse(0, 0, ResponseType.SUCCESS, null);
// If itemcase is not infinite.
if(itemcase.getStorageType() == StorageType.FINITE) {
// Withdraw money from owner.
response1 = economyProvider.withdrawPlayer(
order.getItemcase().getOwner(),
player.getWorld().getName(), total);
}
// Deposit money to player.
EconomyResponse response2 = economyProvider.depositPlayer(
player,
player.getWorld().getName(), total);
// If successful.
if(response1.transactionSuccess() && response2.transactionSuccess()) {
// Remove items from player.
itemstack.setAmount(order.getAmount());
player.getInventory().removeItem(itemstack);
// Add stock to storage.
itemcase.addStock(order.getAmount());
// Mark order as completed.
ItemCaseCore.instance.getOrderManager().completeOrder(player);
// Send message.
chatLogger.message(player, "command.order.deposit");
// If owner is online and itemcase is not infinite.
if(itemcase.getStorageType() == StorageType.FINITE &&
order.getItemcase().getOwner().isOnline()) {
// Get owner.
Player owner = order.getItemcase().getOwner().getPlayer();
// Set placeholder.
translator.setPlaceholder("%PLAYER%", player.getDisplayName());
// If item has custom display name.
if(itemcase.getItemStack().getItemMeta().hasDisplayName()) {
// Set placeholder.
translator.setPlaceholder("%ITEM_NAME%",
ChatColor.GOLD +
itemcase.getItemStack().getItemMeta()
.getDisplayName() + ChatColor.RESET);
} else {
// Set placeholder.
translator.setPlaceholder("%ITEM_NAME%",
ChatColor.GOLD +
itemcase.getItemStack().getType().name()
+ ChatColor.RESET);
}
// Set placeholder.
translator.setPlaceholder("%AMOUNT%", "" + ChatColor.GOLD +
order.getAmount() + ChatColor.RESET);
// Send message.
chatLogger.message(owner, "command.order.sold-to");
}
} else {
// Show message.
chatLogger.message(player, "command.order.failed");
}
}
/**
* Execute amount command.
*
* @param player Player.
* @param label Label.
* @param args Arguments.
*/
private void executeAmount(Player player, String label, String[] args) {
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
// Check if player has permission.
if(!player.hasPermission("itemcase.buy") &&
!player.hasPermission("itemcase.sell")) {
// Send message.
chatLogger.message(player, "command.permission");
// Exit.
return;
}
// Check argument length.
if(args.length != 3) {
// Show help.
this.showHelp(player, label);
// Exit.
return;
}
// Define amount.
int amount = 0;
// Attempt to parse amount.
try {
// Parse amount.
amount = Integer.parseInt(args[2]);
} catch(NumberFormatException e) {
// Show message.
chatLogger.message(player, "command.order.invalid-amount");
// Exit.
return;
}
// Set amount.
ItemCaseCore.instance.getOrderManager().getOrder(player)
.setAmount(amount);
// Get translator.
LanguageTranslator translator =
ItemCaseCore.instance.getTranslator();
// Set placeholder.
translator.setPlaceholder("%AMOUNT%", "" + ChatColor.GOLD + amount +
ChatColor.RESET);
// Show message.
chatLogger.message(player, "command.order.amount-update");
// Exit.
return;
}
}

View File

@ -118,6 +118,17 @@ public class OrderManager {
chatLogger.message(player, "---------------------------");
}
/**
* Mark order as completed.
*
* @param player Player.
*/
public void completeOrder(Player player) {
// Remove oder.
this.orders.remove(player);
}
/**
* @param player Player.
* @return If order exists for given player.
@ -128,10 +139,20 @@ public class OrderManager {
return this.orders.containsKey(player);
}
/**
* @param player Player.
* @return The order linked with this player.
*/
public ItemcaseOrder getOrder(Player player) {
// Return order linked to player.
return this.orders.get(player);
}
/**
* An order for a specific itemcase of some amount.
*/
private final class ItemcaseOrder {
public final class ItemcaseOrder {
/**
* The itemcase this order is for.
@ -141,7 +162,7 @@ public class OrderManager {
/**
* The amount.
*/
private final int amount;
private int amount;
/**
* Constructor.
@ -173,5 +194,14 @@ public class OrderManager {
// Amount.
return amount;
}
/**
* @param amount Amount.
*/
public void setAmount(int amount) {
// Set amount.
this.amount = amount;
}
}
}

View File

@ -125,6 +125,63 @@ command:
# Shown to the player when asking for help.
# %LABEL% = The label used.
help-sell: "/%LABEL% order sell: Sell the current order."
# Shown when the player uses this command but don't have an active order.
no-order: "You do not have an active order."
# Shown when a player uses an order command but no vault hook is present.
no-vault: "Sorry, this server does not have Vault or an economy plugin."
# Shown when the player enters an invalid amount.
invalid-amount: "Invalid amount."
# Shown when the player uses the amount command,
# %AMOUNT% = The amount the player entered.
amount-update: "Amount updated to %AMOUNT%."
# Shown when the player does not have enough money for an order.
# %MONEY% = The formatted money and name.
not-enough-money: "You need atleast %MONEY% to complete this order."
# Shown when the owner does not have enough money for an order.
owner-not-enough-money: "Sorry, the owner of this ItemCase does not have the funds to buy these items from you."
# Shown when the player sets an amount higher than the amount an itemcase has.
# %AMOUNT% = The amount left.
not-enough-stock: "Sorry, this itemcase only has %AMOUNT% left."
# Shown when a player tries to sell more than they have.
not-enough-items: "You do not have enough of the item to sell."
# Shown when an itemcase has zero stock.
no-stock: "Sorry, this itemcase has no stock."
# Shown when a transaction failes for some reason.
failed: "Transaction failed"
# Shown when a player tries to buy items form an itemcase that only players can sell to.
no-buy: "You cannot buy items from this itemcase."
# Shown when a player tries to sell items to an itemcase that only players can buy from.
no-sell: "You cannot sell items to this itemcase."
# Shown to an owner if they are online when a player buys from one of their itemcases.
bought-from: "%PLAYER% bought %AMOUNT% x %ITEM_NAME% from you for %MONEY%."
# Shown to an owner if they are online when a player sells to one of their itemcases.
# %AMOUNT% = The amount left.
# %MONEY% = The formatted money and name.
# %ITEM_NAME% = The item being sold.
# %PLAYER% = The player's name.
sold-to: "%PLAYER% sold %AMOUNT% x %ITEM_NAME% to you for %MONEY%."
# Shown when the player buys from a shop.
# %MONEY% = The formatted money and name.
withdrew: "Withdrew %MONEY% from your account."
# Shown when the player sell to a shop.
# %MONEY% = The formatted money and name.
deposit: "Deposited %MONEY% to your account."
# Messages shown when ordering.
order:

View File

@ -2,6 +2,7 @@ name: ItemCase
version: 2.0.0
description: ItemCase is a Bukkit plugin allowing you to showcase items on slabs, that can also be used as shops.
load: POSTWORLD
softdepend: [Vault]
author: BleedObsidian
website: http://dev.bukkit.org/bukkit-plugins/itemcase/
database: false