Search in sources :

Example 21 with LocationFullException

use of megamek.common.LocationFullException in project megameklab by MegaMek.

the class BayWeaponCriticalTree method addToNewBay.

/**
 * Adds a new bay of the appropriate type to the unit and adds the equipment to the bay.
 *
 * @param bayType The type of bay to be added.
 * @param eq      The equipment to be added.
 */
public void addToNewBay(EquipmentType bayType, Mounted eq) {
    try {
        Mounted bay = eSource.getEntity().addEquipment(bayType, location, facing == AFT);
        BayNode bayNode = new BayNode(bay);
        model.insertNodeInto(bayNode, (MutableTreeNode) model.getRoot(), ((TreeNode) model.getRoot()).getChildCount());
        bayNode.setParent((MutableTreeNode) model.getRoot());
        if (isRootVisible()) {
            expandRow(0);
            setRootVisible(false);
        }
        addToBay(bay, eq);
    } catch (LocationFullException ex) {
    // should not happen
    }
    refresh.refreshEquipment();
    refresh.refreshBuild();
    refresh.refreshPreview();
    refresh.refreshStatus();
    refresh.refreshSummary();
}
Also used : LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted)

Example 22 with LocationFullException

use of megamek.common.LocationFullException in project megameklab by MegaMek.

the class BayWeaponCriticalTree method addAmmoToBay.

public void addAmmoToBay(Mounted bay, Mounted eq, int shots) {
    AmmoType at = (AmmoType) eq.getType();
    eq.setShotsLeft(eq.getBaseShotsLeft() - shots);
    if (eq.getBaseShotsLeft() <= 0) {
        UnitUtil.removeMounted(eSource.getEntity(), eq);
    }
    Optional<Mounted> addMount = bay.getBayAmmo().stream().map(n -> eSource.getEntity().getEquipment(n)).filter(m -> m.getType() == at).findFirst();
    if (addMount.isPresent()) {
        addMount.get().setShotsLeft(addMount.get().getBaseShotsLeft() + shots);
        refresh.refreshEquipment();
        refresh.refreshBuild();
        refresh.refreshPreview();
        refresh.refreshStatus();
        refresh.refreshSummary();
    } else {
        try {
            Mounted m = eSource.getEntity().addEquipment(at, bay.getLocation());
            m.setShotsLeft(shots);
            addToBay(bay, m);
        } catch (LocationFullException e) {
        }
    }
}
Also used : AmmoType(megamek.common.AmmoType) Color(java.awt.Color) Enumeration(java.util.Enumeration) EntitySource(megameklab.com.ui.EntitySource) Vector(java.util.Vector) MouseAdapter(java.awt.event.MouseAdapter) SmallCraft(megamek.common.SmallCraft) TestAero(megamek.common.verifier.TestAero) MouseListener(java.awt.event.MouseListener) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) ToolTipManager(javax.swing.ToolTipManager) TreePath(javax.swing.tree.TreePath) LocationFullException(megamek.common.LocationFullException) Set(java.util.Set) JMenu(javax.swing.JMenu) Component(java.awt.Component) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Dimension(java.awt.Dimension) List(java.util.List) WeaponType(megamek.common.WeaponType) Graphics(java.awt.Graphics) MiscType(megamek.common.MiscType) Optional(java.util.Optional) Entity(megamek.common.Entity) RefreshListener(megameklab.com.util.RefreshListener) InputEvent(java.awt.event.InputEvent) Rectangle(java.awt.Rectangle) LogLevel(megamek.common.logging.LogLevel) TreeNode(javax.swing.tree.TreeNode) ArrayList(java.util.ArrayList) PPCBayWeapon(megamek.common.weapons.bayweapons.PPCBayWeapon) HashSet(java.util.HashSet) EquipmentType(megamek.common.EquipmentType) JMenuItem(javax.swing.JMenuItem) MegaMekLab(megameklab.com.MegaMekLab) CConfig(megameklab.com.util.CConfig) Mounted(megamek.common.Mounted) UnitUtil(megameklab.com.util.UnitUtil) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) BayWeapon(megamek.common.weapons.bayweapons.BayWeapon) AmmoType(megamek.common.AmmoType) JPopupMenu(javax.swing.JPopupMenu) Nullable(megamek.common.annotations.Nullable) TreeSelectionModel(javax.swing.tree.TreeSelectionModel) JTree(javax.swing.JTree) MouseEvent(java.awt.event.MouseEvent) JLabel(javax.swing.JLabel) StringJoiner(java.util.StringJoiner) PPCWeapon(megamek.common.weapons.ppc.PPCWeapon) Collections(java.util.Collections) LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted)

Example 23 with LocationFullException

use of megamek.common.LocationFullException in project megameklab by MegaMek.

the class BayWeaponCriticalTree method removeAmmo.

/**
 * Moves one or more shots of ammo from this location to unallocated. This should not be used if
 * there is only one slot of ammo left in the location;
 * use {@link #removeEquipment(EquipmentNode) removeEquipment}
 * instead.
 *
 * @param ammo  The allocated ammo to remove.
 * @param shots The number of shots to remove.
 */
private void removeAmmo(final Mounted ammo, int shots) {
    AmmoType at = (AmmoType) ammo.getType();
    ammo.setShotsLeft(ammo.getBaseShotsLeft() - shots);
    Mounted unallocated = UnitUtil.findUnallocatedAmmo(eSource.getEntity(), at);
    for (Mounted m : eSource.getEntity().getAmmo()) {
        if ((m.getLocation() == Entity.LOC_NONE) && (m.getType() == at)) {
            unallocated = m;
            break;
        }
    }
    if (null != unallocated) {
        unallocated.setShotsLeft(ammo.getBaseShotsLeft() + shots);
    } else {
        unallocated = new Mounted(eSource.getEntity(), at);
        unallocated.setShotsLeft(shots);
        try {
            eSource.getEntity().addEquipment(unallocated, Entity.LOC_NONE, false);
        } catch (LocationFullException e) {
        }
    }
    refresh.refreshEquipment();
    refresh.refreshBuild();
    refresh.refreshPreview();
    refresh.refreshStatus();
    refresh.refreshSummary();
}
Also used : AmmoType(megamek.common.AmmoType) LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted)

Aggregations

LocationFullException (megamek.common.LocationFullException)23 Mounted (megamek.common.Mounted)21 MiscType (megamek.common.MiscType)9 AmmoType (megamek.common.AmmoType)5 CriticalSlot (megamek.common.CriticalSlot)5 EquipmentType (megamek.common.EquipmentType)5 Dimension (java.awt.Dimension)3 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JComboBox (javax.swing.JComboBox)3 JLabel (javax.swing.JLabel)3 BattleArmor (megamek.common.BattleArmor)3 WeaponType (megamek.common.WeaponType)3 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 BorderFactory (javax.swing.BorderFactory)2