com.callidusrobotics.command.AbstractCommandPerformer.java Source code

Java tutorial

Introduction

Here is the source code for com.callidusrobotics.command.AbstractCommandPerformer.java

Source

/**
 * Copyright (C) 2013 Rusty Gerard
 *
 * 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 3 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.callidusrobotics.command;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import com.callidusrobotics.GameMediator;
import com.callidusrobotics.Message;
import com.callidusrobotics.attribute.Inventoriable;
import com.callidusrobotics.object.AbstractObject;
import com.callidusrobotics.object.EquipmentSlot;
import com.callidusrobotics.object.actor.AbstractActor;
import com.callidusrobotics.object.actor.PlayerCharacter;

abstract class AbstractCommandPerformer implements CommandPerformer {
    protected GameMediator gameMediator;
    protected PlayerCharacter player;

    public void setGameMediator(final GameMediator gameMediator) {
        this.gameMediator = gameMediator;
        player = gameMediator.getPlayer();

        Validate.notNull(player);
    }

    protected int selectItem(final String message, final AbstractObject source) {
        final List<String> items = new LinkedList<String>();
        for (final Inventoriable item : source.getInventory()) {
            items.add(item.toString());
        }

        return gameMediator.getConsoleColleague().select(message, items, false, false);
    }

    protected EquipmentSlot selectEquipment(final String message, final AbstractActor source) {
        int padding = 0;
        for (final EquipmentSlot slot : EquipmentSlot.values()) {
            if (slot.toString().length() > padding) {
                padding = slot.toString().length();
            }
        }

        final List<String> items = new LinkedList<String>();
        for (final EquipmentSlot slot : EquipmentSlot.values()) {
            if (source.getEquippedItem(slot) == null) {
                items.add(StringUtils.rightPad(slot.toString(), padding + 1) + ": EMPTY");
            } else {
                items.add(StringUtils.rightPad(slot.toString(), padding + 1) + ":"
                        + source.getEquippedItem(slot).toString());
            }
        }

        final int index = gameMediator.getConsoleColleague().select(message, items, false, true);
        if (index != -1) {
            return EquipmentSlot.values()[index];
        }

        return null;
    }

    protected Message transferInventoryItem(final Command command, final String message, final boolean transferAll,
            final AbstractObject source, final AbstractObject destination) {
        final int index = selectItem(message, source);
        if (index != -1) {
            final Inventoriable item = source.getItemFromInventory(index);
            if (destination.canAddItemToInventory(item)) {
                final int maxVal = item.getQuantity();
                final int quantity = transferAll || maxVal == 1 ? maxVal
                        : Math.min(Math.max(gameMediator.getConsoleColleague().getQuantity(maxVal, 4), 0), maxVal);
                if (quantity > 0) {
                    destination.transferItemToInventory(source, index, quantity);
                    return new Message(command, quantity + ": " + item.getName(), null, null);
                }

                return new Message(Command.UNKNOWN, null, null, null);
            } else {
                return new Message(Command.UNKNOWN, "There is no room to put that there.", null, null);
            }
        }

        return new Message(Command.UNKNOWN, null, null, null);
    }
}