2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2011-07-06 00:58:59 +00:00
|
|
|
import com.earth2me.essentials.Trade;
|
2011-03-19 22:39:51 +00:00
|
|
|
import com.earth2me.essentials.User;
|
2011-05-06 19:50:14 +00:00
|
|
|
import com.earth2me.essentials.Util;
|
2011-11-21 01:55:26 +00:00
|
|
|
import java.util.Locale;
|
2011-05-09 09:31:36 +00:00
|
|
|
import java.util.logging.Level;
|
2011-03-19 22:39:51 +00:00
|
|
|
import org.bukkit.Material;
|
2011-11-18 17:42:26 +00:00
|
|
|
import org.bukkit.Server;
|
2011-03-19 22:39:51 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
|
|
|
|
public class Commandsell extends EssentialsCommand
|
|
|
|
{
|
|
|
|
public Commandsell()
|
|
|
|
{
|
|
|
|
super("sell");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-12-01 13:42:39 +00:00
|
|
|
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2012-12-05 14:40:17 +00:00
|
|
|
double totalWorth = 0.0;
|
|
|
|
String type = "";
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
if (args.length < 1)
|
|
|
|
{
|
|
|
|
throw new NotEnoughArgumentsException();
|
2011-04-16 15:11:20 +00:00
|
|
|
}
|
2011-05-04 23:33:32 +00:00
|
|
|
ItemStack is = null;
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
if (args[0].equalsIgnoreCase("hand"))
|
|
|
|
{
|
2011-04-16 15:39:58 +00:00
|
|
|
is = user.getItemInHand();
|
2011-05-04 16:22:38 +00:00
|
|
|
}
|
2011-10-26 02:32:38 +00:00
|
|
|
else if (args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all"))
|
2011-05-04 16:22:38 +00:00
|
|
|
{
|
|
|
|
for (ItemStack stack : user.getInventory().getContents())
|
|
|
|
{
|
2011-05-14 10:57:55 +00:00
|
|
|
if (stack == null || stack.getType() == Material.AIR)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
2013-01-29 10:25:01 +00:00
|
|
|
totalWorth += sellItem(user, stack, args, true);
|
2011-05-14 10:57:55 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
}
|
2011-05-04 16:22:38 +00:00
|
|
|
}
|
2012-12-05 14:40:17 +00:00
|
|
|
if (totalWorth > 0)
|
|
|
|
{
|
|
|
|
user.sendMessage(_("totalWorthAll", type, Util.displayCurrency(totalWorth, ess)));
|
|
|
|
}
|
2011-05-04 16:22:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-08-20 19:49:17 +00:00
|
|
|
else if (args[0].equalsIgnoreCase("blocks"))
|
2011-05-04 16:22:38 +00:00
|
|
|
{
|
|
|
|
for (ItemStack stack : user.getInventory().getContents())
|
|
|
|
{
|
2011-05-14 10:57:55 +00:00
|
|
|
if (stack == null || stack.getTypeId() > 255 || stack.getType() == Material.AIR)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
2012-12-05 14:40:17 +00:00
|
|
|
totalWorth += sellItem(user, stack, args, true);
|
2011-05-14 10:57:55 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
}
|
2011-05-04 16:22:38 +00:00
|
|
|
}
|
2012-12-05 14:40:17 +00:00
|
|
|
if (totalWorth > 0)
|
|
|
|
{
|
|
|
|
user.sendMessage(_("totalWorthBlocks", type, Util.displayCurrency(totalWorth, ess)));
|
|
|
|
}
|
2011-05-04 16:22:38 +00:00
|
|
|
return;
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
}
|
2011-05-04 23:33:32 +00:00
|
|
|
if (is == null)
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
{
|
2011-07-15 23:33:22 +00:00
|
|
|
is = ess.getItemDb().get(args[0]);
|
2011-04-16 15:39:58 +00:00
|
|
|
}
|
2011-05-07 12:11:56 +00:00
|
|
|
sellItem(user, is, args, false);
|
2011-05-04 16:22:38 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 14:40:17 +00:00
|
|
|
private double sellItem(User user, ItemStack is, String[] args, boolean isBulkSell) throws Exception
|
2011-05-04 16:22:38 +00:00
|
|
|
{
|
2011-05-02 02:15:19 +00:00
|
|
|
if (is == null || is.getType() == Material.AIR)
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
throw new Exception(_("itemSellAir"));
|
2011-04-16 15:11:20 +00:00
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
int id = is.getTypeId();
|
|
|
|
int amount = 0;
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
if (args.length > 1)
|
|
|
|
{
|
2011-04-16 15:11:44 +00:00
|
|
|
amount = Integer.parseInt(args[1].replaceAll("[^0-9]", ""));
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
if (args[1].startsWith("-"))
|
|
|
|
{
|
2011-04-16 15:33:52 +00:00
|
|
|
amount = -amount;
|
|
|
|
}
|
2011-04-16 15:11:20 +00:00
|
|
|
}
|
2011-06-01 10:40:12 +00:00
|
|
|
double worth = ess.getWorth().getPrice(is);
|
2011-04-16 15:11:20 +00:00
|
|
|
boolean stack = args.length > 1 && args[1].endsWith("s");
|
2011-06-01 10:40:12 +00:00
|
|
|
boolean requireStack = ess.getSettings().isTradeInStacks(id);
|
2011-03-19 22:39:51 +00:00
|
|
|
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
if (Double.isNaN(worth))
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
throw new Exception(_("itemCannotBeSold"));
|
2011-04-16 15:11:20 +00:00
|
|
|
}
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
if (requireStack && !stack)
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
throw new Exception(_("itemMustBeStacked"));
|
2011-04-16 15:11:20 +00:00
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2011-05-07 12:11:56 +00:00
|
|
|
|
2011-03-19 22:39:51 +00:00
|
|
|
int max = 0;
|
2011-10-26 02:05:09 +00:00
|
|
|
for (ItemStack s : user.getInventory().getContents())
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2013-01-29 19:29:03 +00:00
|
|
|
if (s == null || !s.isSimilar(is))
|
2011-11-28 18:55:51 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-26 02:05:09 +00:00
|
|
|
max += s.getAmount();
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
if (stack)
|
|
|
|
{
|
2011-11-28 18:55:51 +00:00
|
|
|
amount *= is.getType().getMaxStackSize();
|
2011-04-16 15:11:20 +00:00
|
|
|
}
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
if (amount < 1)
|
|
|
|
{
|
2011-04-16 15:11:20 +00:00
|
|
|
amount += max;
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
|
|
|
|
if (requireStack)
|
|
|
|
{
|
2011-11-28 18:55:51 +00:00
|
|
|
amount -= amount % is.getType().getMaxStackSize();
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
if (amount > max || amount < 1)
|
|
|
|
{
|
2011-10-26 02:05:09 +00:00
|
|
|
if (!isBulkSell)
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
user.sendMessage(_("itemNotEnough1"));
|
|
|
|
user.sendMessage(_("itemNotEnough2"));
|
|
|
|
throw new Exception(_("itemNotEnough3"));
|
2011-10-26 02:05:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-05 14:40:17 +00:00
|
|
|
return worth * amount;
|
2011-10-26 02:05:09 +00:00
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
|
2011-11-27 05:00:58 +00:00
|
|
|
//TODO: Prices for Enchantments
|
2013-01-29 10:25:01 +00:00
|
|
|
final ItemStack ris = is.clone();
|
|
|
|
ris.setAmount(amount);
|
2013-01-29 19:29:03 +00:00
|
|
|
if (!user.getInventory().containsAtLeast(ris, amount)) {
|
|
|
|
// This should never happen.
|
|
|
|
throw new IllegalStateException("Trying to remove more items than are available.");
|
|
|
|
}
|
2013-02-17 14:17:47 +00:00
|
|
|
user.getInventory().removeItem(ris);
|
2011-03-19 22:39:51 +00:00
|
|
|
user.updateInventory();
|
2013-01-29 10:25:01 +00:00
|
|
|
Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(worth * amount, ess), user.getLocation(), ess);
|
2011-03-19 22:39:51 +00:00
|
|
|
user.giveMoney(worth * amount);
|
2012-03-04 10:11:58 +00:00
|
|
|
user.sendMessage(_("itemSold", Util.displayCurrency(worth * amount, ess), amount, is.getType().toString().toLowerCase(Locale.ENGLISH), Util.displayCurrency(worth, ess)));
|
|
|
|
logger.log(Level.INFO, _("itemSoldConsole", user.getDisplayName(), is.getType().toString().toLowerCase(Locale.ENGLISH), Util.displayCurrency(worth * amount, ess), amount, Util.displayCurrency(worth, ess)));
|
2012-12-05 14:40:17 +00:00
|
|
|
return worth * amount;
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
}
|