forked from Geoffrey/Geoffrey-MC-Plugin
99 lines
3.3 KiB
Java
99 lines
3.3 KiB
Java
package com.zerohighdef.geoffrey.Commands;
|
|
|
|
import com.google.gson.JsonElement;
|
|
import com.zerohighdef.geoffrey.GeoffreyMC;
|
|
import com.zerohighdef.geoffrey.Models.GeoffreyItemListing;
|
|
import com.zerohighdef.geoffrey.Models.GeoffreyLocation;
|
|
import com.zerohighdef.geoffrey.Objects.GeoffreyAPICallback;
|
|
import com.zerohighdef.geoffrey.Objects.GeoffreyCommandError;
|
|
import org.apache.commons.lang.NumberUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class AddItemCommand extends GeoffreyCommand{
|
|
public AddItemCommand(GeoffreyMC plugin) {
|
|
super(plugin);
|
|
}
|
|
|
|
|
|
@Override
|
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
|
|
|
if ((sender instanceof Player)) {
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
if (args.length > 3) {
|
|
String name[] = Arrays.copyOfRange(args, 3, args.length-1);
|
|
params.put("shop_name", StringUtils.join(name, " "));
|
|
}
|
|
else if (args.length < 3) {
|
|
sender.sendMessage(ChatColor.RED + "Too few parameters given.");
|
|
return false;
|
|
}
|
|
|
|
Player player = ((Player) sender).getPlayer();
|
|
|
|
String quantity = args[1];
|
|
String diamondPrice = args[2];
|
|
|
|
try {
|
|
Integer.parseInt(quantity);
|
|
}
|
|
catch (NumberFormatException e) {
|
|
sender.sendMessage(ChatColor.RED + "Quantity must be an integer value!");
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
Integer.parseInt(diamondPrice);
|
|
}
|
|
catch (NumberFormatException e) {
|
|
sender.sendMessage(ChatColor.RED + "Price must be an integer value!");
|
|
return false;
|
|
}
|
|
|
|
params.put("mc_uuid", player.getUniqueId().toString().replace("-", ""));
|
|
params.put("item_name", args[0]);
|
|
params.put("quantity", quantity);
|
|
params.put("diamond_price", diamondPrice);
|
|
RunCommand("add_item", params, Method.POST , new CommandCallback(sender));
|
|
|
|
}
|
|
else {
|
|
sender.sendMessage(ChatColor.RED + "Console can not add items!");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private class CommandCallback extends GeoffreyAPICallback {
|
|
|
|
CommandCallback(CommandSender sender) {
|
|
super(sender);
|
|
errors.put("LocationLookUpError", "You do not have a shop by that name, goober.");
|
|
errors.put("EntryNameNotUniqueError", "You have more than one location. Please specify a name, dingus.");
|
|
}
|
|
|
|
@Override
|
|
public void invoke(String s) {
|
|
try {
|
|
JsonElement json = parseJSON(s);
|
|
|
|
GeoffreyItemListing itemListing = new GeoffreyItemListing(json.getAsJsonObject());
|
|
String msg = ChatColor.GREEN + itemListing.getItemName() + " has been added to your shop.";
|
|
commandSender.sendMessage(msg);
|
|
}
|
|
catch (GeoffreyCommandError e) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|