Search in sources :

Example 1 with TileComponentUpgrade

use of mekanism.common.tile.component.TileComponentUpgrade in project Mekanism by mekanism.

the class ItemUpgrade method useOn.

@Nonnull
@Override
public ActionResultType useOn(ItemUseContext context) {
    PlayerEntity player = context.getPlayer();
    if (player != null && player.isShiftKeyDown()) {
        World world = context.getLevel();
        TileEntity tile = WorldUtils.getTileEntity(world, context.getClickedPos());
        if (tile instanceof IUpgradeTile) {
            IUpgradeTile upgradeTile = (IUpgradeTile) tile;
            if (upgradeTile.supportsUpgrades()) {
                TileComponentUpgrade component = upgradeTile.getComponent();
                ItemStack stack = context.getItemInHand();
                Upgrade type = getUpgradeType(stack);
                if (component.supports(type)) {
                    if (!world.isClientSide) {
                        int added = component.addUpgrades(type, stack.getCount());
                        if (added > 0) {
                            stack.shrink(added);
                        }
                    }
                    return ActionResultType.SUCCESS;
                }
            }
        }
    }
    return ActionResultType.PASS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileComponentUpgrade(mekanism.common.tile.component.TileComponentUpgrade) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Upgrade(mekanism.api.Upgrade) TileComponentUpgrade(mekanism.common.tile.component.TileComponentUpgrade) IUpgradeTile(mekanism.common.tile.interfaces.IUpgradeTile) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)1 Upgrade (mekanism.api.Upgrade)1 TileComponentUpgrade (mekanism.common.tile.component.TileComponentUpgrade)1 IUpgradeTile (mekanism.common.tile.interfaces.IUpgradeTile)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 ItemStack (net.minecraft.item.ItemStack)1 TileEntity (net.minecraft.tileentity.TileEntity)1 World (net.minecraft.world.World)1