[trunk] /sell itemname amount

New syntax for sell command, proposed by KimKandor
It won't sell the items in hand anymore.

git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1219 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
snowleo 2011-04-16 15:11:20 +00:00
parent 4c245dd25b
commit 137bafd0a8

View file

@ -3,6 +3,7 @@ package com.earth2me.essentials.commands;
import org.bukkit.Server; import org.bukkit.Server;
import com.earth2me.essentials.Essentials; import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.InventoryWorkaround; import com.earth2me.essentials.InventoryWorkaround;
import com.earth2me.essentials.ItemDb;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -18,30 +19,49 @@ public class Commandsell extends EssentialsCommand
@Override @Override
public void run(Server server, Essentials parent, User user, String commandLabel, String[] args) throws Exception public void run(Server server, Essentials parent, User user, String commandLabel, String[] args) throws Exception
{ {
ItemStack is = user.getInventory().getItemInHand(); if (args.length < 1) {
if(is.getType() == Material.AIR) user.sendMessage("§cUsage: /sell <itemname> [amount]");
return;
}
ItemStack is = ItemDb.get(args[0]);
if(is.getType() == Material.AIR) {
throw new Exception("You really tried to sell Air? Put an item in your hand."); throw new Exception("You really tried to sell Air? Put an item in your hand.");
}
int id = is.getTypeId(); int id = is.getTypeId();
int amount = 0; int amount = 0;
if (args.length > 0) amount = Integer.parseInt(args[0].replaceAll("[^0-9]", "")); if (args.length > 1) {
amount = Integer.parseInt(args[0].replaceAll("[^0-9]", ""));
}
double worth = Essentials.getWorth().getPrice(is); double worth = Essentials.getWorth().getPrice(is);
boolean stack = args.length > 0 && args[0].endsWith("s"); boolean stack = args.length > 1 && args[1].endsWith("s");
boolean requireStack = parent.getConfiguration().getBoolean("trade-in-stacks-" + id, false); boolean requireStack = parent.getConfiguration().getBoolean("trade-in-stacks-" + id, false);
if (worth < 1) throw new Exception("That item cannot be sold to the server."); if (worth < 1) {
if (requireStack && !stack) throw new Exception("Item must be traded in stacks. A quantity of 2s would be two stacks, etc."); throw new Exception("That item cannot be sold to the server.");
}
if (requireStack && !stack) {
throw new Exception("Item must be traded in stacks. A quantity of 2s would be two stacks, etc.");
}
int max = 0; int max = 0;
for (ItemStack s : user.getInventory().all(is).values()) for (ItemStack s : user.getInventory().getContents())
{ {
if (s.getDurability() != is.getDurability()) if (s.getTypeId() != is.getTypeId()) {
continue; continue;
}
if (s.getDurability() != is.getDurability()) {
continue;
}
max += s.getAmount(); max += s.getAmount();
} }
if (stack) amount *= 64; if (stack) {
if (amount < 1) amount += max; amount *= 64;
}
if (amount < 1) {
amount += max;
}
if (requireStack) if (requireStack)
{ {