Create order command.

This commit is contained in:
Jesse Prescott 2018-06-04 14:47:52 +01:00
parent 761d65320f
commit f92c0215f0
6 changed files with 403 additions and 9 deletions

View File

@ -17,6 +17,7 @@ package com.gmail.bleedobsidian.itemcase;
import com.gmail.bleedobsidian.itemcase.commands.CreateCommand;
import com.gmail.bleedobsidian.itemcase.commands.ModifyCommand;
import com.gmail.bleedobsidian.itemcase.commands.DestroyCommand;
import com.gmail.bleedobsidian.itemcase.commands.OrderCommand;
import com.gmail.bleedobsidian.itemcase.commands.StorageCommand;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -50,6 +51,9 @@ public final class CommandHandler implements CommandExecutor {
case "create":
new CreateCommand().execute(sender, label, args);
break;
case "order":
new OrderCommand().execute(sender, label, args);
break;
case "destroy":
new DestroyCommand().execute(sender, label, args);
break;
@ -78,9 +82,14 @@ public final class CommandHandler implements CommandExecutor {
// Get translator.
LanguageTranslator translator = ItemCaseCore.instance.getTranslator();
// Set placeholder to blank.
translator.setPlaceholder("%COMMAND%", "");
// Set label placeholder.
translator.setPlaceholder("%LABEL%", label);
// Set placeholder.
translator.setPlaceholder("%COMMAND%",
"/" + label + " [create/destroy/modify/storage]");
String command = "/" + label + " [create/order/destroy/modify/storage]";
// Check if sender is a player or console.
if(sender instanceof Player) {
@ -94,6 +103,12 @@ public final class CommandHandler implements CommandExecutor {
// Send message to player.
logger.message(player, "command.itemcase-help");
// Send message to player.
logger.message(player, command);
// Send message to player.
logger.message(player, "command.command-help");
} else {
// Get console logger.
@ -101,6 +116,12 @@ public final class CommandHandler implements CommandExecutor {
// Send console message.
logger.message(sender, "command.itemcase-help");
// Send console message.
logger.message(sender, command);
// Send console message.
logger.message(sender, "command.command-help");
}
}
}

View File

@ -18,6 +18,7 @@ package com.gmail.bleedobsidian.itemcase;
import com.gmail.bleedobsidian.itemcase.Itemcase.ItemcaseListener;
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 org.bukkit.plugin.java.JavaPlugin;
@ -64,6 +65,11 @@ public final class ItemCaseCore extends JavaPlugin {
* ItemcaseManager.
*/
private final ItemcaseManager itemcaseManager = new ItemcaseManager();
/**
* OrderManager.
*/
private final OrderManager orderManager = new OrderManager();
@Override
public void onEnable() {
@ -178,4 +184,11 @@ public final class ItemCaseCore extends JavaPlugin {
public ItemcaseManager getItemcaseManager() {
return this.itemcaseManager;
}
/**
* @return OrderManager.
*/
public OrderManager getOrderManager() {
return this.orderManager;
}
}

View File

@ -29,10 +29,13 @@ import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.ItemDespawnEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@ -145,12 +148,6 @@ public final class Itemcase {
this.itemStack = itemStack.clone();
this.itemStack.setAmount(1);
// Set display name to random UUID to prevent graphical item stacking.
ItemMeta metadata = this.itemStack.getItemMeta();
metadata.setDisplayName("com.gmail.bleedobsidian.itemcase:" +
UUID.randomUUID().toString());
this.itemStack.setItemMeta(metadata);
// Set location.
this.location = location;
@ -201,9 +198,18 @@ public final class Itemcase {
// Get the location to spawn the display item at.
Location displayItemSpawnLocation = this.getDisplayItemSpawnLocation();
// Get itemstack.
ItemStack itemStack = this.itemStack.clone();
// Set display name to random UUID to prevent graphical item stacking.
ItemMeta metadata = itemStack.getItemMeta();
metadata.setDisplayName("com.gmail.bleedobsidian.itemcase:" +
UUID.randomUUID().toString());
itemStack.setItemMeta(metadata);
// Spawn the item.
this.displayItem = this.location.getWorld().dropItem(
displayItemSpawnLocation, this.itemStack);
displayItemSpawnLocation, itemStack);
// Add custom metadata so we know that this item is a display item and
// shouldn't be picked up by players and which itemcase it belongs to.
@ -462,6 +468,54 @@ public final class Itemcase {
}
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerInteractEvent(PlayerInteractEvent event) {
// Check if block was involved.
if(!event.hasBlock()) {
// Exit.
return;
}
// Check if action was correct.
if(event.getAction() != Action.RIGHT_CLICK_BLOCK) {
// Exit.
return;
}
// If off-hand, skip.
if(event.getHand() == EquipmentSlot.OFF_HAND) {
// Exit.
return;
}
// If block is not an ItemCase.
if(!ItemCaseCore.instance.getItemcaseManager().isItemcase(
event.getClickedBlock().getLocation())) {
// Exit.
return;
}
// Get itemcase.
Itemcase itemcase = ItemCaseCore.instance.getItemcaseManager()
.getItemcase(event.getClickedBlock().getLocation());
// If itemcase is not a shop.
if(itemcase.getType() == Type.SHOWCASE) {
// Exit.
return;
}
// Create new order.
ItemCaseCore.instance.getOrderManager().createOrder(itemcase,
event.getPlayer());
}
}
/**

View File

@ -0,0 +1,114 @@
/*
* 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.commands;
import com.gmail.bleedobsidian.itemcase.ChatLogger;
import com.gmail.bleedobsidian.itemcase.ItemCaseCore;
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 org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* A command handler for the 'order' command.
*
* @author Jesse Prescott (BleedObsidian)
*/
public final class OrderCommand implements Command {
@Override
public void execute(CommandSender sender, String label,
String[] args) {
// If sender is not a player.
if(!(sender instanceof Player)) {
// Send message.
ItemCaseCore.instance.getGenericLogger().message(
sender, "command.not-player");
// Exit.
return;
}
// Cast sender to player.
Player player = (Player) sender;
// Check if player is asking for help.
if(this.isAskingForHelp(player, label, args)) {
// Exit.
return;
}
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
}
/**
* @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;
}
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
// Get translator.
LanguageTranslator translator = ItemCaseCore.instance.getTranslator();
// Set placeholder.
translator.setPlaceholder("%COMMAND%", "");
// Show command help.
chatLogger.message(player, "command.itemcase-help");
// Set placeholder.
translator.setPlaceholder("%LABEL%", label);
// Show specific help.
chatLogger.message(player, "command.order.help-amount");
// Show specific help.
chatLogger.message(player, "command.order.help-buy");
// Show specific help.
chatLogger.message(player, "command.order.help-sell");
// Return.
return true;
}
}

View File

@ -0,0 +1,177 @@
/*
* 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.managers;
import com.gmail.bleedobsidian.itemcase.ChatLogger;
import com.gmail.bleedobsidian.itemcase.ItemCaseCore;
import com.gmail.bleedobsidian.itemcase.Itemcase;
import com.gmail.bleedobsidian.itemcase.Itemcase.Type;
import com.gmail.bleedobsidian.itemcase.LanguageTranslator;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
/**
* Manages all active Itemcase orders.
*
* @author Jesse Prescott (BleedObsidian)
*/
public class OrderManager {
/**
* HashMap of players and their active orders.
*/
private final HashMap<Player, ItemcaseOrder> orders = new HashMap();
/**
* Create new order.
*
* @param itemcase Itemcase.
* @param player Player.
*/
public void createOrder(Itemcase itemcase, Player player) {
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
// Get translator.
LanguageTranslator translator =
ItemCaseCore.instance.getTranslator();
// Create new order for amount 1.
ItemcaseOrder order = new ItemcaseOrder(itemcase, 1);
// Add to list.
this.orders.put(player, order);
// Send message.
chatLogger.message(player, "---------- Order ----------");
// If item has custom display name.
if(itemcase.getItemStack().getItemMeta().hasDisplayName()) {
// Set placeholder.
translator.setPlaceholder("%ITEM_NAME%",
ChatColor.GOLD + itemcase.getItemStack().getItemMeta()
.getDisplayName());
} else {
// Set placeholder.
translator.setPlaceholder("%ITEM_NAME%",
ChatColor.GOLD + itemcase.getItemStack().getType().name());
}
// Send message.
chatLogger.message(player, "order.item");
// Set amount placeholder.
translator.setPlaceholder("%AMOUNT%", ChatColor.GOLD +
String.valueOf(1));
// Send message.
chatLogger.message(player, "order.amount");
// If shop buys products.
if(itemcase.getType() == Type.SHOP_BUY ||
itemcase.getType() == Type.SHOP_MULTI) {
// Set placeholder.
translator.setPlaceholder("%BUY_PRICE%", "" + ChatColor.GOLD +
itemcase.getBuyPrice());
// Send message.
chatLogger.message(player, "order.buy-price");
}
// If shop sells products.
if(itemcase.getType() == Type.SHOP_SELL ||
itemcase.getType() == Type.SHOP_MULTI) {
// Set placeholder.
translator.setPlaceholder("%SELL_PRICE%", "" + ChatColor.GOLD +
itemcase.getSellPrice());
// Send message.
chatLogger.message(player, "order.sell-price");
}
// Send message.
chatLogger.message(player, "---------------------------");
// Send message.
chatLogger.message(player, "order.help");
// Send message.
chatLogger.message(player, "---------------------------");
}
/**
* @param player Player.
* @return If order exists for given player.
*/
public boolean hasOrder(Player player) {
// Return if order exists for given player.
return this.orders.containsKey(player);
}
/**
* An order for a specific itemcase of some amount.
*/
private final class ItemcaseOrder {
/**
* The itemcase this order is for.
*/
private final Itemcase itemcase;
/**
* The amount.
*/
private final int amount;
/**
* Constructor.
*
* @param itemcase Itemcase.
* @param amount Amount.
*/
public ItemcaseOrder(Itemcase itemcase, int amount) {
// Set attributes.
this.itemcase = itemcase;
this.amount = amount;
}
/**
* @return Itemcase.
*/
public Itemcase getItemcase() {
// Return itemcase.
return itemcase;
}
/**
* @return Amount.
*/
public int getAmount() {
// Amount.
return amount;
}
}
}

View File

@ -110,6 +110,21 @@ command:
# Shown to the player when trying to access the storage of an infinite
# itemcase shop.
infinite: "This ItemCase has infinite items to buy/sell, therefore has no storage."
# Messages shown when using the 'order' command.
order:
# Shown to the player when asking for help.
# %LABEL% = The label used.
help-amount: "/%LABEL% order amount [amount]: Change amount."
# Shown to the player when asking for help.
# %LABEL% = The label used.
help-buy: "/%LABEL% order buy: Buy the current order."
# Shown to the player when asking for help.
# %LABEL% = The label used.
help-sell: "/%LABEL% order sell: Sell the current order."
# Messages shown when ordering.
order: