<commit_before>package org.gestern.gringotts;

import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
 * Central handler for player and console commands.
 * 
 * @author jast
 *
 */
public class Commands implements CommandExecutor {
    Logger log = Bukkit.getServer().getLogger();

    private Gringotts plugin;
    private Configuration conf = Configuration.config;
    
    public Commands(Gringotts plugin) {
        this.plugin = plugin;
    }

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {

        Player player;
        if (sender instanceof Player) {
            player = (Player)sender;
        } else {
            sender.sendMessage("This command can only be run by a player.");
            return false; // for now, no console commands
            // TODO allow console commands
            // TODO actually, refactor the whole thing already!
        }

        AccountHolder accountOwner = new PlayerAccountHolder(player);
        
        Accounting accounting = plugin.accounting;
        Account account = accounting.getAccount(accountOwner);

        if(cmd.getName().equalsIgnoreCase("balance")){
            balanceMessage(account, accountOwner);
            return true;
        } else if(cmd.getName().equalsIgnoreCase("money")){
            if (args.length == 0) {
                // same as balance
                balanceMessage(account, accountOwner);
                return true;
            } 

            String command = "";
            if (args.length >= 1) {
                command = args[0];
            }

            double value = 0;
            if (args.length >= 2) {
                try { value = Double.parseDouble(args[1]); } 
                catch (NumberFormatException e) { return false; }
            } 

            if(args.length == 3) {
                // /money pay <amount> <player>
                if (command.equals("pay")) {
                    String recipientName = args[2];
                    AccountHolder recipient = new PlayerAccountHolder(recipientName);
                    Account recipientAccount = accounting.getAccount(recipient);

                    double tax = conf.transactionTaxFlat + value * conf.transactionTaxRate;

                    double balance = account.balance();
                    double valueAdded = value + tax;
                    if (balance < valueAdded) {
                        accountOwner.sendMessage(
                                "Your account has insufficient balance. Current balance: " + balance + " " + numName(balance) 
                                + ". Required: " + (valueAdded) + " " + numName(valueAdded));
                        return true;
                    }
                    if (recipientAccount.capacity() < value) {
                        accountOwner.sendMessage(recipientName + " has insufficient storage space for this amount");
                        return true;
                    } else if (account.remove(value)) {
                        if (recipientAccount.add(value)) {
                            account.remove(tax);
                            String currencyName = numName(balance);
                            String taxMessage = "Transaction tax deducted from your account: " + tax + " " + numName(tax);
                            accountOwner.sendMessage("Sent " + value + " " + currencyName + " to " + recipientName +". " + (tax>0? taxMessage : ""));
                            recipient.sendMessage("Received " + value + " " + currencyName + " from " + accountOwner.getName() +".");
                            return true;
                        }
                    }
                }
            }
        } else if (cmd.getName().equalsIgnoreCase("moneyadmin")) {
        	
        	AccountHolderFactory ahf = new AccountHolderFactory();

        	String command;
            if (args.length >= 2) {
                command = args[0];
            } else return false;
            
            // admin command: balance of player / faction
            if (args.length == 2 && command.equalsIgnoreCase("b")) {
            	String targetAccountHolderStr = args[1];
            	AccountHolder targetAccountHolder = ahf.get(targetAccountHolderStr);
            	Account targetAccount = accounting.getAccount(targetAccountHolder);
            	sender.sendMessage("Balance of account " + targetAccountHolder.getName() + ": " + targetAccount.balance());
            }
            
            // moneyadmin add/remove
            if (args.length == 3) {
            	String amountStr = args[2];
            	double value;
            	try { value = Double.parseDouble(amountStr);} 
            	catch(NumberFormatException x) { return false; }
            	
            	String targetAccountHolderStr = args[3];
            	AccountHolder targetAccountHolder = ahf.get(targetAccountHolderStr);
            	Account targetAccount = accounting.getAccount(targetAccountHolder);
            	if (command.equalsIgnoreCase("add")) {
                    if (targetAccount.add(value)) {
                    	sender.sendMessage("Added " + value + " to account " + targetAccountHolder.getName());
                    	targetAccountHolder.sendMessage("Added to your account: " + value);
                    } else {
                    	sender.sendMessage("Could not add " + value + " to account " + targetAccountHolder.getName());
                    }
                    
                    return true;
                    <commit_msg>fixed arguments to /moneyadmin<commit_after>package org.gestern.gringotts;

import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
 * Central handler for player and console commands.
 * 
 * @author jast
 *
 */
public class Commands implements CommandExecutor {
    Logger log = Bukkit.getServer().getLogger();

    private Gringotts plugin;
    private Configuration conf = Configuration.config;
    
    public Commands(Gringotts plugin) {
        this.plugin = plugin;
    }

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {

        Player player;
        if (sender instanceof Player) {
            player = (Player)sender;
        } else {
            sender.sendMessage("This command can only be run by a player.");
            return false; // for now, no console commands
            // TODO allow console commands
            // TODO actually, refactor the whole thing already!
        }

        AccountHolder accountOwner = new PlayerAccountHolder(player);
        
        Accounting accounting = plugin.accounting;
        Account account = accounting.getAccount(accountOwner);

        if(cmd.getName().equalsIgnoreCase("balance")){
            balanceMessage(account, accountOwner);
            return true;
        } else if(cmd.getName().equalsIgnoreCase("money")){
            if (args.length == 0) {
                // same as balance
                balanceMessage(account, accountOwner);
                return true;
            } 

            String command = "";
            if (args.length >= 1) {
                command = args[0];
            }

            double value = 0;
            if (args.length >= 2) {
                try { value = Double.parseDouble(args[1]); } 
                catch (NumberFormatException e) { return false; }
            } 

            if(args.length == 3) {
                // /money pay <amount> <player>
                if (command.equals("pay")) {
                    String recipientName = args[2];
                    AccountHolder recipient = new PlayerAccountHolder(recipientName);
                    Account recipientAccount = accounting.getAccount(recipient);

                    double tax = conf.transactionTaxFlat + value * conf.transactionTaxRate;

                    double balance = account.balance();
                    double valueAdded = value + tax;
                    if (balance < valueAdded) {
                        accountOwner.sendMessage(
                                "Your account has insufficient balance. Current balance: " + balance + " " + numName(balance) 
                                + ". Required: " + (valueAdded) + " " + numName(valueAdded));
                        return true;
                    }
                    if (recipientAccount.capacity() < value) {
                        accountOwner.sendMessage(recipientName + " has insufficient storage space for this amount");
                        return true;
                    } else if (account.remove(value)) {
                        if (recipientAccount.add(value)) {
                            account.remove(tax);
                            String currencyName = numName(balance);
                            String taxMessage = "Transaction tax deducted from your account: " + tax + " " + numName(tax);
                            accountOwner.sendMessage("Sent " + value + " " + currencyName + " to " + recipientName +". " + (tax>0? taxMessage : ""));
                            recipient.sendMessage("Received " + value + " " + currencyName + " from " + accountOwner.getName() +".");
                            return true;
                        }
                    }
                }
            }
        } else if (cmd.getName().equalsIgnoreCase("moneyadmin")) {
        	
        	AccountHolderFactory ahf = new AccountHolderFactory();

        	String command;
            if (args.length >= 2) {
                command = args[0];
            } else return false;
            
            // admin command: balance of player / faction
            if (args.length == 2 && command.equalsIgnoreCase("b")) {
            	String targetAccountHolderStr = args[1];
            	AccountHolder targetAccountHolder = ahf.get(targetAccountHolderStr);
            	Account targetAccount = accounting.getAccount(targetAccountHolder);
            	sender.sendMessage("Balance of account " + targetAccountHolder.getName() + ": " + targetAccount.balance());
            	return true;
            }
            
            // moneyadmin add/remove
            if (args.length == 3) {
            	String amountStr = args[1];
            	double value;
            	try { value = Double.parseDouble(amountStr);} 
            	catch(NumberFormatException x) { return false; }
            	
            	String targetAccountHolderStr = args[2];
            	AccountHolder targetAccountHolder = ahf.get(targetAccountHolderStr);
            	Account targetAccount = accounting.getAccount(targetAccountHolder);
            	if (command.equalsIgnoreCase("add")) {
                    if (targetAccount.add(value)) {
                    	sender.sendMessage("Added " + value + " to account " + targetAccountHolder.getName());
                    	targetAccountHolder.sendMessage("Added to your account: " + value);
                    } else {
                    	sender.sendMessage("Could not add " + value + " to account " + targetAccountHolder.getName());
                    }
                    
                    return true;
                    
{'fraction_non_alphanumeric': 0.09563773057085409, 'fraction_numerical': 0.002797447329312003, 'mean_word_length': 1.598818718764198, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5005632', 'n_tokens_mistral': 2745, 'n_tokens_neox': 2581, 'n_words': 649}