2011-03-19 23:39:51 +01:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2013-09-28 18:21:16 +02:00
|
|
|
import com.earth2me.essentials.ChargeException;
|
2013-10-16 21:59:39 +02:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
2012-02-26 05:15:14 +01:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2011-11-18 18:42:26 +01:00
|
|
|
import com.earth2me.essentials.User;
|
2013-06-08 23:31:19 +02:00
|
|
|
import com.earth2me.essentials.utils.NumberUtil;
|
2013-05-05 05:13:17 +02:00
|
|
|
import java.math.BigDecimal;
|
2011-11-27 06:23:07 +01:00
|
|
|
import java.util.Locale;
|
2011-03-19 23:39:51 +01:00
|
|
|
import org.bukkit.Server;
|
|
|
|
|
|
|
|
|
2013-09-28 18:21:16 +02:00
|
|
|
public class Commandeco extends EssentialsLoopCommand
|
2011-03-19 23:39:51 +01:00
|
|
|
{
|
2013-09-28 18:21:16 +02:00
|
|
|
Commandeco.EcoCommands cmd;
|
|
|
|
BigDecimal amount;
|
|
|
|
|
2011-03-19 23:39:51 +01:00
|
|
|
public Commandeco()
|
|
|
|
{
|
|
|
|
super("eco");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-10-16 21:59:39 +02:00
|
|
|
public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
|
2011-03-19 23:39:51 +01: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 23:07:30 +02:00
|
|
|
if (args.length < 2)
|
|
|
|
{
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
2013-04-30 01:03:59 +02:00
|
|
|
|
2013-05-05 11:41:19 +02:00
|
|
|
BigDecimal startingBalance = ess.getSettings().getStartingBalance();
|
2013-09-28 18:21:16 +02:00
|
|
|
|
2011-03-19 23:39:51 +01:00
|
|
|
try
|
|
|
|
{
|
2013-04-28 05:44:23 +02:00
|
|
|
cmd = Commandeco.EcoCommands.valueOf(args[0].toUpperCase(Locale.ENGLISH));
|
2013-05-05 05:13:17 +02:00
|
|
|
amount = (cmd == Commandeco.EcoCommands.RESET) ? startingBalance : new BigDecimal(args[2].replaceAll("[^0-9\\.]", ""));
|
2011-03-19 23:39:51 +01:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2011-06-01 12:40:12 +02:00
|
|
|
throw new NotEnoughArgumentsException(ex);
|
2011-03-19 23:39:51 +01:00
|
|
|
}
|
|
|
|
|
2013-09-28 18:21:16 +02:00
|
|
|
loopOfflinePlayers(server, sender, false, args[1], args);
|
|
|
|
|
|
|
|
if (cmd == Commandeco.EcoCommands.RESET || cmd == Commandeco.EcoCommands.SET)
|
2012-03-04 10:20:03 +01:00
|
|
|
{
|
2013-09-28 18:21:16 +02:00
|
|
|
if (args[1].contentEquals("**"))
|
2012-03-04 10:20:03 +01:00
|
|
|
{
|
2013-09-28 18:21:16 +02:00
|
|
|
server.broadcastMessage(_("resetBalAll", NumberUtil.displayCurrency(amount, ess)));
|
2012-03-04 10:20:03 +01:00
|
|
|
}
|
2013-09-28 18:21:16 +02:00
|
|
|
else if (args[1].contentEquals("*"))
|
2011-03-19 23:39:51 +01:00
|
|
|
{
|
2013-09-28 18:21:16 +02:00
|
|
|
server.broadcastMessage(_("resetBal", NumberUtil.displayCurrency(amount, ess)));
|
2011-03-19 23:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
2013-09-28 18:21:16 +02:00
|
|
|
}
|
2013-01-20 02:19:52 +01:00
|
|
|
|
2013-09-28 18:21:16 +02:00
|
|
|
@Override
|
2013-10-16 21:59:39 +02:00
|
|
|
protected void updatePlayer(final Server server, final CommandSource sender, final User player, final String[] args) throws NotEnoughArgumentsException, ChargeException
|
2013-09-28 18:21:16 +02:00
|
|
|
{
|
|
|
|
switch (cmd)
|
2013-01-20 00:58:12 +01:00
|
|
|
{
|
2013-09-28 18:21:16 +02:00
|
|
|
case GIVE:
|
|
|
|
player.giveMoney(amount, sender);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TAKE:
|
|
|
|
take(amount, player, sender);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RESET:
|
|
|
|
case SET:
|
|
|
|
set(amount, player, sender);
|
|
|
|
break;
|
2013-01-20 00:58:12 +01:00
|
|
|
}
|
2011-03-19 23:39:51 +01:00
|
|
|
}
|
|
|
|
|
2013-10-16 21:59:39 +02:00
|
|
|
private void take(BigDecimal amount, final User player, final CommandSource sender) throws ChargeException
|
2013-04-28 05:44:23 +02:00
|
|
|
{
|
2013-05-05 05:13:17 +02:00
|
|
|
BigDecimal money = player.getMoney();
|
|
|
|
BigDecimal minBalance = ess.getSettings().getMinMoney();
|
|
|
|
if (money.subtract(amount).compareTo(minBalance) > 0)
|
2013-04-28 05:44:23 +02:00
|
|
|
{
|
|
|
|
player.takeMoney(amount, sender);
|
|
|
|
}
|
2013-04-30 01:03:59 +02:00
|
|
|
else if (sender == null)
|
2013-04-28 05:44:23 +02:00
|
|
|
{
|
|
|
|
player.setMoney(minBalance);
|
2013-06-08 23:31:19 +02:00
|
|
|
player.sendMessage(_("takenFromAccount", NumberUtil.displayCurrency(player.getMoney(), ess)));
|
2013-04-30 01:03:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-28 18:21:16 +02:00
|
|
|
throw new ChargeException(_("insufficientFunds"));
|
2013-04-28 05:44:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 21:59:39 +02:00
|
|
|
private void set(BigDecimal amount, final User player, final CommandSource sender)
|
2013-04-28 05:44:23 +02:00
|
|
|
{
|
2013-05-05 05:13:17 +02:00
|
|
|
BigDecimal minBalance = ess.getSettings().getMinMoney();
|
|
|
|
boolean underMinimum = (amount.compareTo(minBalance) < 0);
|
2013-04-28 05:44:23 +02:00
|
|
|
player.setMoney(underMinimum ? minBalance : amount);
|
2013-06-08 23:31:19 +02:00
|
|
|
player.sendMessage(_("setBal", NumberUtil.displayCurrency(player.getMoney(), ess)));
|
2013-04-28 05:44:23 +02:00
|
|
|
if (sender != null)
|
|
|
|
{
|
2013-06-08 23:31:19 +02:00
|
|
|
sender.sendMessage(_("setBalOthers", player.getDisplayName(), NumberUtil.displayCurrency(player.getMoney(), ess)));
|
2013-04-28 05:44:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 13:06:59 +01:00
|
|
|
|
2011-03-19 23:39:51 +01:00
|
|
|
private enum EcoCommands
|
|
|
|
{
|
2013-04-28 05:44:23 +02:00
|
|
|
GIVE, TAKE, SET, RESET
|
2011-03-19 23:39:51 +01:00
|
|
|
}
|
2013-04-28 05:44:23 +02:00
|
|
|
}
|