Package com.kellerkindt.scs.commands

Source Code of com.kellerkindt.scs.commands.BuyCmd

/**
* ShowCaseStandalone
* Copyright (C) 2012 Kellerkindt <copyright at kellerkindt.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.kellerkindt.scs.commands;


import java.util.UUID;

import org.bukkit.command.CommandSender;
import org.bukkit.inventory.ItemStack;

import com.kellerkindt.scs.exceptions.InsufficientPermissionException;
import com.kellerkindt.scs.exceptions.MissingOrIncorrectArgumentException;
import com.kellerkindt.scs.internals.Todo;
import com.kellerkindt.scs.internals.Todo.Type;
import com.kellerkindt.scs.shops.BuyShop;
import com.kellerkindt.scs.utilities.Properties;
import com.kellerkindt.scs.utilities.Term;
import com.kellerkindt.scs.utilities.Utilities;

/**
* @author Sorklin <sorklin at gmail.com>
*/
public class BuyCmd extends GenericCmd {
   
    public BuyCmd(CommandSender cs, String args[]){
        super(cs, args);
        this.permission = Properties.permCreateBuy;      
    }

    @Override
    public boolean execute() throws MissingOrIncorrectArgumentException, InsufficientPermissionException {
        if(errorCheck())
            return true;

        if(Properties.blacklistedWorlds.contains(player.getWorld().getName()))
            throw new InsufficientPermissionException("`rYou are not allowed to create a showcase in this world."); //msg: blacklistError
       
        //Default values:
        int     amount     = 0;
        boolean    unlimited  = false;
        double     price     = 1.0;
        ItemStack   is       = null;

        /* Using keyword "this" will attempt to sell whats in your hand. 
         *      ie. default values for material and amount are what's in your hand.
         *
         * Otherwise default value for amount is 1.
         *
         * Valid forms of command:
         * 1: scs buy  -- item in hand, amount in hand, price = 1.0
         * 2: scs buy [item/"this"]
         * 3: scs buy [item/"this"] [amount]
         * 4: scs buy [item/"this"] [amount] [price]
         */

        //We have optional number of arguments.  Lets parse through them.
        try {
       
            switch (args.length){
                case 0:
                case 1:
                    is = Utilities.getItemStack(player, "this");
                    break;
                   
                case 2:
                    is = Utilities.getItemStack(player, args[1]);
                    break;
                   
                case 3:
                    is = Utilities.getItemStack(player, args[1]);
                    if (args[2].equalsIgnoreCase("unlimited"))
                        unlimited = true;
                    else
                        amount = Integer.parseInt(args[2]);
                    break;
                   
                case 4:
                    is = Utilities.getItemStack(player, args[1]);
                    if (args[2].equalsIgnoreCase("unlimited"))
                        unlimited = true;
                    else
                        amount = Integer.parseInt (args[2]);
                    price = Double.parseDouble(args[3]);
                    break;
            }
           
        } catch (Exception e) {
            throw new MissingOrIncorrectArgumentException ();
        }

        if (is == null || is.getTypeId() == 0) {
          throw new MissingOrIncorrectArgumentException (Term.ITEM_MISSING.get());
        }

        // if the id already exists, the ShopHandler will set a new one
        BuyShop  shop  = new BuyShop(UUID.randomUUID(), player.getName(), null, is.clone());
       
        shop.setMaxAmount  (amount);
        shop.setUnlimited  (unlimited);
        shop.setPrice    (price);
       
        scs.msgPlayer(player, next);
        scs.addTodo(player, new Todo (player, Type.CREATE, shop, 0, null));

        return true;
    }
}
TOP

Related Classes of com.kellerkindt.scs.commands.BuyCmd

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.